The answer is quite simple. In your case, a ScheduledExecutorService
is bound to the lifetime of your process. If your process gets killed, then the ScheduledExecutorService
will get killed along with it. Instead use an AlarmManager
Here are 3 steps
- Create the Alarm
- Create the BroadcastReceiver
- Register the Receiver in the App Manifest
1. Create the Alarm
public void createAlarm() {
//System request code
int DATA_FETCHER_RC = 123;
//Create an alarm manager
AlarmManager mAlarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
//Create the time of day you would like it to go off. Use a calendar
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
//Create an intent that points to the receiver. The system will notify the app about the current time, and send a broadcast to the app
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, DATA_FETCHER_RC,intent, PendingIntent.FLAG_UPDATE_CURRENT);
//initialize the alarm by using inexactrepeating. This allows the system to scheduler your alarm at the most efficient time around your
//set time, it is usually a few seconds off your requested time.
// you can also use setExact however this is not recommended. Use this only if it must be done then.
//Also set the interval using the AlarmManager constants
mAlarmManager.setInexactRepeating(AlarmManager.RTC,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
}
2. Create the BroadcastReceiver
//This is the broadcast receiver you create where you place your logic once the alarm is run. Once the system realizes your alarm should be run, it will communicate to your app via the BroadcastReceiver. You must implement onReceive.
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Your code once the alarm is set off goes here
//You can use an intent filter to filter the specified intent
}
}
3. In your app manifest register the receiver
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package">
//This is a useful permission as it allows your apps alarm to still be active once a reboot takes place
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MainActivity$AlarmReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…