手机重启时java错误通知?它具有最新通知的时间权限,但文本是数据库中的第一个结果
我有一个广播接收器。在其中,我试图在重新启动后重置警报。我使用IntentService设置警报和创建通知。我正在使用游标检索报警计时和描述的值,并在广播接收器的onReceive中重置它。我必须使用循环来重置所有警报。但是IntentService设置了警报,但显示了错误的通知。我检查了日志,它有错误的数据。我会把密码寄出去。请告知。图片如下Stack Trace 1Stack trace 2 Notification on phone
广播接收机类
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());
}
}
IntentService类
[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(安卓.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]
# 1 楼答案
修好了。下面是我做的改变。它在IntentService类中。在Pending帐篷中添加了一面旗帜。悬挂式帐篷。标记\u取消\u当前