我有一个广播接收器。其中我试图在重启后重置警报。我使用IntentServices来setAlarms和创建通知。我使用游标来检索闹钟定时和描述的值,并在广播接收器的onReceive中对其进行重置。我必须使用循环来重置所有警报。但是IntentService设置了警报,但显示了错误的通知。我检查了日志,它有错误的数据。我会把代码贴出来。请给我建议。图片如下所示。Stack Trace 1Stack trace 2 Notification on phone
BroadCastReceiver类
public class Receiver extends BroadcastReceiver {
public static String TAG=BroadcastReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
Log.i(Receiver.class.getSimpleName(),"Boot Completed");
Cursor cursor=context.getContentResolver().query(DatabaseContract.content_URI,null,null,null,null);
if(cursor.getCount()>0)
{
cursor.moveToFirst();
do{
ContentValues contentValues=new ContentValues();
contentValues.put(DatabaseContract.columns._ID,cursor.getInt(0));
contentValues.put(DatabaseContract.columns.description,cursor.getString(1));
contentValues.put(DatabaseContract.columns.isCompleted,cursor.getInt(2));
contentValues.put(DatabaseContract.columns.priority,cursor.getInt(3));
contentValues.put(DatabaseContract.columns.date,cursor.getLong(4));
Log.i(Receiver.class.getSimpleName(),"Content Values: "+contentValues.getAsString(DatabaseContract.columns.description));
AlarmScheduler alarmScheduler=new AlarmScheduler();
alarmScheduler.createAlarm(context,contentValues);
contentValues.clear();
}while ( cursor.moveToNext());
// AlarmManager alarmManager=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Log.i(TAG,"Alarm reset after boot:" );
cursor.close();
}
/* Intent intent1=new Intent(context, MainActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);*/
}
}AlarmScheduler类
public class AlarmScheduler {
public static String TAG="Scheduler";//AlarmScheduler.class.getSimpleName();
public void createAlarm(Context mContext, ContentValues contentValues){
AlarmManager alarmManager=(AlarmManager) mContext.getSystemService(mContext.ALARM_SERVICE);
//NotificationManager manager=mContext.get
PendingIntent pendingIntent=ReminderAlarmService.getReminderPendingIntent(mContext,contentValues);
Log.i(TAG,"Description: "+contentValues.getAsString(DatabaseContract.columns.description)+" | Date: "+contentValues.getAsLong(DatabaseContract.columns.date));
alarmManager.setExact(AlarmManager.RTC_WAKEUP,contentValues.getAsLong(DatabaseContract.columns.date),pendingIntent);
Calendar calendar=Calendar.getInstance();
calendar.setTimeInMillis(contentValues.getAsLong(DatabaseContract.columns.date));
Log.i(AlarmScheduler.class.getSimpleName(),"Alarm set at"+ calendar.getTime());
}
}IntentServiceClass
[public class ReminderAlarmService extends IntentService {
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
public static String TAG="Intent Service";//ReminderAlarmService.class.getSimpleName();
public static String CONTENTVALUES="ContentValues";
int notificationId=001;
public ReminderAlarmService() {
super(TAG);
}
static PendingIntent getReminderPendingIntent(Context context, ContentValues contentValues){
Intent intent=new Intent(context, ReminderAlarmService.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(CONTENTVALUES,contentValues);
Log.i(TAG,"Description: "+contentValues.getAsString(DatabaseContract.columns.description));
PendingIntent pendingIntent=PendingIntent.getService(context,0,intent,0);
//
context.startService(intent);
return pendingIntent;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG,"Destroying");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
Intent intent1=new Intent(ReminderAlarmService.this, MainActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent2=PendingIntent.getActivity(ReminderAlarmService.this,0,intent1,0);
ContentValues contentValues2=intent.getParcelableExtra(CONTENTVALUES);
// notificationId=contentValues2.getAsInteger(DatabaseContract.columns._ID);
Log.i(TAG,"Description: "+contentValues2.getAsString(DatabaseContract.columns.description)+" | Date: "+contentValues2.getAsLong(DatabaseContract.columns.date));
// Log.i(TAG,"Description: "+contentValues2.getAsString(DatabaseContract.columns.description));
NotificationCompat.Builder builder=new NotificationCompat.Builder(ReminderAlarmService.this,"none").setSmallIcon(android.support.v4.R.drawable.notification_icon_background).setAutoCancel(true).setContentTitle("Task Update").setContentText(contentValues2.getAsString(DatabaseContract.columns.description)).setContentIntent(pendingIntent2);
NotificationManager manager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
manager.notify(notificationId,builder.build());
}
}][1]发布于 2018-02-05 13:27:09
修好了。下面是我所做的更改。它在IntentService类中。在PendingIntent中添加了一个标志。PendingIntent.FLAG_CANCEL_CURRENT。
static PendingIntent getReminderPendingIntent(Context context, ContentValues contentValues){
Intent intent=new Intent(context, ReminderAlarmService.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(CONTENTVALUES,contentValues);
Log.i(TAG,"Description: "+contentValues.getAsString(DatabaseContract.columns.description));
PendingIntent pendingIntent=PendingIntent.getService(context,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
//
//context.startService(intent);
return pendingIntent;
}https://stackoverflow.com/questions/48615172
复制相似问题