嗨,我知道之前已经问过这个问题,但我不认为它在这方面有。我有一个应用程序,它通过数据库运行,然后根据数据库设置每24小时重复一次警报。
警报可以正常工作,但是一旦我重新启动,我就会立即得到一个警报,然后再一次打开应用程序。
我有以下两种方法来设置闹钟。 to_reminder方法是从我的主Activity中的onCreate方法运行的。
public void to_reminder()
{
helper = new TaskDBHelper(Meds2.this);
SQLiteDatabase sqlDB = helper.getReadableDatabase();
Cursor cursor = sqlDB.query(TaskContract.TABLE,
new String[]{TaskContract.Columns._ID, TaskContract.Columns.TASK, TaskContract.Columns.DATE},
null, null, null, null, null);
cursor.moveToFirst();
//Cancel All Alarms
Intent intent=new Intent(this,BackgroundService.class);
AlarmManager manager=(AlarmManager)getSystemService(Activity.ALARM_SERVICE);
PendingIntent pendingIntent=PendingIntent.getService(this,
0,intent, 0);
try {
manager.cancel(pendingIntent);
pendingIntent.cancel();
} catch (Exception e) {
Log.e(TAG, "AlarmManager update was not canceled. " + e.toString());
}
while (!cursor.isAfterLast()) { // Loop until all vales have been seen
String time = cursor.getString(2);
String[] parts = time.split(":"); //Split String Value stored in db
String part1 = parts[0]; // hour
String part2 = parts[1]; // minute
int hr = Integer.parseInt(part1);
int min = Integer.parseInt(part2);
Random generator = new Random();
setUpAlarm(hr,min);
Log.e(TAG, "DATE VALUE TIME IS " + part1 +"--" +part2 + " Random Number is: " + generator.nextInt()); //log part one and two to make sure time is right
cursor.moveToNext();
}
}
public void setUpAlarm(int hr, int min){
Random generator = new Random();
Intent intent=new Intent(this,BackgroundService.class);
AlarmManager manager=(AlarmManager)getSystemService(Activity.ALARM_SERVICE);
PendingIntent pendingIntent=PendingIntent.getService(this,
generator.nextInt(),intent, PendingIntent.FLAG_ONE_SHOT);
long _alarm = 0;
Calendar now = Calendar.getInstance();
Calendar cal=Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, hr);
cal.set(Calendar.MINUTE,min);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
if(cal.getTimeInMillis() <= now.getTimeInMillis())
_alarm = cal.getTimeInMillis() + (AlarmManager.INTERVAL_DAY+1);
else
_alarm = cal.getTimeInMillis();
manager.setRepeating(AlarmManager.RTC_WAKEUP,_alarm,AlarmManager.INTERVAL_DAY,pendingIntent);
}
我的警报接收器类只是启动BackgroundService
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
Intent background = new Intent(context, BackgroundService.class);
context.startService(background);
}
}
我的后台服务会设置通知。
public void mainMeth(){
Intent browserIntent = new Intent(this,Meds2.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
context,
0,
browserIntent,
0);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.cast_ic_notification_2)
.setContentTitle("Medication Reminder")
.setContentInfo("Time")
.setContentIntent(pendingIntent)
.setContentText("Open Application" )
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setVibrate(new long[]{200, 200, 200, 200}) //set virbrate & color of led
.setLights(Color.BLUE, 3000, 3000);
NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(getID(), builder.build());
}
我的清单也包含以下代码
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
任何有关如何在重新启动后使其工作的帮助将不胜感激。