我在列表项中有一个按钮来删除该项目。当有关于该项目的警报时,我也想删除警报。 要删除项目,我会显示一个警告对话框,并在肯定按钮中显示删除功能:
builder.setPositiveButton("DELETE", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DeleteList del = new DeleteList();
del.execute(listId);
}
});
但是我也需要删除警报,问题是警报是在另一个活动中设置的。我有PendingIntent的唯一ID,但是我无法创建PendingIntent,因为我处于静态上下文中。
如果我这样做:
AddListActivityFragment.deleteAlarmS(alarmId);
我得到的非静态方法不能从静态上下文中引用。
如果我这样做:
Intent resultIntent = new Intent(AddListActivity.this, MainActivity.class);
PendingIntent deleteIntent = PendingIntent.getBroadcast(AddListActivity.this, Integer.parseInt(alarmId), resultIntent, PendingIntent.FLAG_NO_CREATE);
if(deleteIntent != null) {
AlarmManager alarmManager = (AlarmManager) AddListActivity.this.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(deleteIntent);
deleteIntent.cancel();
}
我得到“AddListActivity不是一个封闭的类”。
我该如何解决这个问题?如何取消闹钟?我是否使用AddListActivity收到的ID发送了一些广播?
由于
感谢qbix,我设法使AddListActivityFragment中的方法成为静态。但它仍然没有取消警报。
如何在AddListActivityFragment中设置警报:
private void scheduleNotification(Notification notification, long delay, int alarmId) {
Intent notificationIntent = new Intent(mContext, NotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, alarmId);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, alarmId, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
Log.v("alarm id", "alarmid = " + alarmId);
long futureInMillis = SystemClock.elapsedRealtime() + delay;
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
}
private Notification getNotification(String content, String subText, int alarmId) {
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
long[] vib = new long[]{1000L, 1000L, 1000L, 1000L};
Intent resultIntent = new Intent(mContext, MainActivity.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
alarmId,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
builder.setContentTitle("To do!");
builder.setContentText(content);
builder.setSubText(subText);
builder.setSound(uri);
builder.setVibrate(vib);
builder.setSmallIcon(R.drawable.ic_add_white_24dp);
builder.setAutoCancel(true);
builder.setContentIntent(resultPendingIntent);
return builder.build();
}
闹钟设置:
scheduleNotification(getNotification(title, subText, alarmId), delay, alarmId);
我如何尝试取消闹钟:
public static void deleteAlarm(String alarmId){
Toast.makeText(mContext, "delete alarm active", Toast.LENGTH_SHORT).show();
Log.v("alarm id delete", "alarmid = " + alarmId);
Intent resultIntent = new Intent(mContext, NotificationPublisher.class);
PendingIntent deleteIntent = PendingIntent.getBroadcast(mContext, Integer.parseInt(alarmId), resultIntent, PendingIntent.FLAG_NO_CREATE);
if(deleteIntent != null) {
Toast.makeText(mContext, "Alarm deleted", Toast.LENGTH_SHORT).show();
AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(deleteIntent);
deleteIntent.cancel();
}
}
“删除警报激活”Toast显示,alarmId是正确的。但是Toast“警报删除”没有显示,警报仍然响起。 PendingIntent不一样吗?
由于
答案 0 :(得分:0)
在AddListActivity
中,声明类型为Context
的私有静态成员:
private static Context mContext;
在onCreate()
的{{1}}方法中,初始化它:
AddListActivity
在构建mContext = this;
并取消闹钟的代码块中,将PendingIntent
替换为AddListActivity.this
。
答案 1 :(得分:0)
我解决了。 显然PendingIntent需要相同的标志,所以当我在deleteAlarm方法中将其更改为PendingIntent.FLAG_ONE_SHOT时,它可以工作。 谢谢你的帮助。