我尝试使用待处理意图创建多个预定的警报通知,并设置触发按钮以取消警报。 我已成功创建一个警报并取消它但是在删除当前警报之前似乎无法创建多个警报。 从那里我需要取消特定的警报。
这是我的代码: 的 TestActivity.java
public class TestActivity extends AppCompatActivity {
Button btnStart, btnCancel;
AlarmManager alarmManager;
Intent intent;
PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
btnStart = (Button) findViewById(R.id.btnStart);
btnCancel = (Button) findViewById(R.id.btnCancel);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StartAlarm();
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CancelAlarm();
}
});
}
void StartAlarm(){
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 5);
String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
GlobalVariable.Alarm_ID += 1;
intent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, GlobalVariable.Alarm_ID, intent, PendingIntent.FLAG_IMMUTABLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
Toast.makeText(this, "Alarm Set To : " + cal.getTimeInMillis(), Toast.LENGTH_SHORT).show();
}
}
void CancelAlarm(){
intent = new Intent(this, AlarmReceiver.class);
alarmManager.cancel(pendingIntent);
Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
}
}
AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
@Override
public void onReceive(Context context, Intent intent) {
String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
Intent notificationIntent = new Intent(context, InfoActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context, "Channel ID")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Order Service")
.setContentText("Alarm_ID : " + GlobalVariable.Alarm_ID + ". Time : " + timeStamp)
.setGroup("Orders")
.setAutoCancel(true)
.setSound(notificationSound)
.setContentIntent(pendingIntent)
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
GlobalVariable.Alarm_ID = 1;
notificationManager.notify(GlobalVariable.Alarm_ID, notifBuilder.build());
}
}
程序中没有错误,我只是不知道在哪里更改以创建多个警报并取消部分警报。
TIA
答案 0 :(得分:0)
通过Id取消通知的方法:
public static void cancelNotification(Context ctx, int notifyId) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
nMgr.cancel(notifyId);
}