简体   繁体   中英

Android 4.4.4 Kitkat: Broadcastreceiver not receiving on_boot_completed broadcast

My program is to listen receive_boot_complete broadcast when device completes the boot. The program is working fine as expected with android 4.0.3, but the same thing is not happening when I am running the code on Android 4.4.4 device. The broadcastreceiver is not receiving a boot completed broadcast. Is there anything new for Android 4.4.4? Running it on Moto G. My code is as below:

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.my.app"
    android:versionCode="1"
    android:installLocation="internalOnly"
    android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="15"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.INTERNET" />

<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" 
        android:excludeFromRecents="true" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service
        android:name=".MyService"
        android:label="@string/ser_name"
        android:icon="@drawable/serv_ico"
        android:enabled="true" />

    <receiver
        android:name=".BootReceiver"
        android:enabled="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            <action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    <receiver
        android:name=".AlarmReceiver"
        android:enabled="true" >
    </receiver>

</application>

BootReceiver

public class BootReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(final Context context, Intent intent) {

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                         // Start alarm manager on boot
                         // Start service
        }
    }, 60000);
}   

}

AlarmReceiver

public class AlarmReceiver extends BroadcastReceiver {
@Override
    public void onReceive(final Context context, Intent intent) {
     // Start  service
        }
}

MainActivity

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            // Run service on application start
        setContentView(R.layout.activity_main);

        // Initialize alarm manager application start


        PackageManager p = getPackageManager(); // Remove App icon from launcher
        p.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    }
}

AlarmManager

public class Alarm {
    public Alarm schedule(Context context) {

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 20);
        calendar.set(Calendar.MINUTE, 15);
        calendar.set(Calendar.SECOND, 0);

        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);

        am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi);

     return null;
     }
    }

您必须使用export = true和enabled = true定义接收器

<android:name=".BootReceiver" android:enabled="true" android:exported="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" />

This might be because of a bug in Android 4.4.4 r1/r2

See Bug in Android KitKat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM