Сначала вам нужно создать BroadcastReceiver
для запуска на BOOT_COMPLETE
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent mServiceIntent = new Intent();
mServiceIntent
.setAction("com.crossfire.appname.service.BootService");
ComponentName service = context.startService(mServiceIntent);
if (null == service) {
// something really wrong here
Log.e(TAG, "Could not start service ");
}
} else {
Log.e(TAG, "Received unexpected intent " + intent.toString());
}
}
}
Затем создайте сервис для сброса всех ваших сигналов тревоги:
public class BootService extends Service {
private final String TAG = "BootService";
@Override
public IBinder onBind(final Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Trace.i(TAG, "created");
}
@Override
public void onStart(final Intent intent, final int startId) {
super.onStart(intent, startId);
Trace.i(TAG, "started");
LoadAlarmsFromDatabase();
}
private void LoadAlarmsFromDatabase() {
Cursor c = mDbHelper.getPendingAlarms();
if (c.moveToFirst()) {
do {
int id = c.getInt(mDbHelper.ID_COLUMN);
String time = c.getString(mDbHelper.TIME_COLUMN);
addNotification(id, time);
} while (c.moveToNext());
}
c.close();
}
private void addNotification(int apptId, String time) {
Trace.d(TAG, "addNotification " + apptId + ", " + time);
Intent intent = new Intent(BootService.this,
ApptBroadcastReceiver.class);
intent.putExtra("appt_id", Integer.toString(apptId));
PendingIntent mAlarmSender = PendingIntent.getBroadcast(
BootService.this, apptId, intent, 0);
long alarmTime = System.currentTimeMillis() + 60000;
Calendar c = null;
try {
c = CustomDateUtils.StringToCalendar(time);
alarmTime = c.getTimeInMillis();
} catch (ParseException e) {
Log.e(TAG, "ParseException");
}
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, alarmTime, mAlarmSender);
}
}
Наконец, добавьте разрешение иполучатель в манифест
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".receiver.BootReceiver" android:enabled="true">
<intent-filter>
<action android:name ="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>