Android 4.0 enables broadcast acceptance

1. Problems

    When testing Android to enable broadcast acceptance, permissions have been added after Android 3.0, and the specified App that can receive broadcasts and open during simulator testing. However, when debugging on the real machine, it has been unacceptable, and the boot broadcast is unacceptable. I have also read many articles on the Internet, but there is no solution.

2. Solve

    My mobile phone is Android 4.0. When debugging the real machine, I also tried the online solution, increased the permissions, ran it once after installation, and set the self-starting item. But it didn't work. It turned out that the apps I installed were all installed on the SDCard, and the SDCard had to be loaded when the device was just turned on. Maybe the app would not receive the boot broadcast after the app was loaded. Finally, I moved the app to the system space to solve the problem.

 

 

public class BootReceiver extends BroadcastReceiver {

	private static final String TAG = "BootReceiver";

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Log.i(TAG, "Broadcast accepted");
		Intent activityIntent = new Intent(context, MainActivity.class);
		activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(activityIntent);
	}

}

 

<!-- Permissions are to be added after 3.0 -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    
    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />
<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <receiver android:name="com.pt.receiver.BootReceiver">
            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>

 

 

 

  

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326686522&siteId=291194637