我有一个使用推送通知创建例行任务的应用程序。当我添加多个任务(每个任务都有一个唯一的ID),并且出现推送通知时,它会显示上一个任务的数据。 我检查了LogCat,并确保在 TaskActivity 中每个任务都有一个唯一的ID,但在 AlarmService 中,只显示输入的最后一个任务。
我经常搜索,但我也找不到任何错误的线索。
我还将PendingIntent.FLAG_UPDATE_CURRENT
更改为FLAG_CANCEL_CURRENT
和FLAG_ONE_SHOT
,但它无效。
我考虑为每项任务添加intent
,但我找不到办法。
TaskActivity
.
.
.
public void setNotificationForNewTaskInside() {
alarmManager = (AlarmManager) getApplicationContext().getSystemService(
ALARM_SERVICE);
Intent alarmIntent = new Intent(this,AlarmReceiver.class);
Random random = new Random();
randomNum= random.nextInt(9999 - 1000) + 1000;
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
randomNum, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
if (DeviceTimeAndDate.devicetaskTimeDate.equals(newtaskTimeDate)) {
finalLong=System.currentTimeMillis();
alarmManager.setExact(AlarmManager.RTC_WAKEUP, finalLong, pendingIntent);
}
else if (DeviceTimeAndDate.devicetaskTimeDate.before(newtaskTimeDate)) {
finalLong=new_task_timeInMilliseconds;
alarmManager.setExact(AlarmManager.RTC_WAKEUP, finalLong, pendingIntent);
} else if (DeviceTimeAndDate.devicetaskTimeDate.after(newtaskTimeDate)) {
Toast.makeText(context, "زمان یا تاریخ مورد گذشنه است، لطفا زمان دیگری را انتخاب کنید", Toast.LENGTH_SHORT).show();
startDate.setText(DeviceTimeAndDate.deviceTime);
taskTime.setText(EditTaskActivity.secondMiConver);
}
AlarmReciver
public class AlarmReceiver extends BroadcastReceiver {
String TAG = "AlarmReceiver";
public RemoteViews remoteViews;
public Uri sound;
public Context context;
public Notification notification;
int finalRandumNum;
public static String getTitle,getTime;
public AlarmReceiver (){
}
@Override
public void onReceive(Context context, Intent intent) {
sound = Uri.parse("android.resource://" + context.getPackageName() + "/raw/plucky");
remoteViews= new RemoteViews(context.getPackageName(), R.layout.notification);
remoteViews.setImageViewResource(R.id.notif_image, R.mipmap.ic_launcher);
remoteViews.setTextViewText(R.id.title,getTitle);
remoteViews.setTextViewText(R.id.text,getTime);
NotificationManager notif=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
android.app.Notification notify=new NotificationCompat.Builder(context, "CHANNEL_ID")
.setContentTitle(TaskActivity.subjEntry)
.setWhen(finalLong)
.setSound(sound)
.setContentText(TaskActivity.timeEntryy)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(TaskActivity.pendingIntent)
.build();
notif.notify(TaskActivity.randomNum, notify);
答案 0 :(得分:1)
我认为您通过将任务详细信息保存在变量( TaskActivity.subjEntry和TaskActivity.timeEntryy )中会出错,这意味着每次尝试添加新任务时都会替换数据。
因此,您应该将数据保存在数据库,数组或任何其他数据结构中,并在创建通知时检索数据。
public void addData(Data data){
SQLiteDatabase database = getWritableDatabase();
ContentValues cv_data = new ContentValues();
cv_data.put("key", data.key());
cv_data.put("subjEntry", data.subjEntry());
cv_data.put("subjTime", data.subjTime());
database.insert("tablename",null,cv_data);
database.close();
}
public List<Data> getData(String key){
String columns [] = {
"key", "subjEntry", "subjTime"
};
String sortingOrder = " subjTime ASC";
List <Data> dataList = new ArrayList <Data>();
SQLiteDatabase mdDatabase = getWritableDatabase();
Cursor cursor = mdDatabase.query("tablename",
columns,
null,
null,
null,
null,
sortingOrder);
if (cursor.moveToFirst()){
do {
if(cursor.getString(cursor.getColumnIndex("subjEntry")).equals(Key)){
}
Data data = new Data();
data.setsubjEntry(cursor.getString(cursor.getColumnIndex("subjEntry")));
data.setsubjTime(cursor.getString(cursor.getColumnIndex("subjTime")));
dataList.add(data);
}while (cursor.moveToNext());
}
mdDatabase.close();
cursor.close();
return dataList;
}'