我正在前台服务中使用警报。停止服务后,将发出警报并发出通知。我想要当我单击通知时,警报被取消。但是我的代码不起作用,警报没有取消。我尝试了很多在google和此处找到的解决方案,但从未成功
// i在前台服务中设置警报
private void startAlarm(){
Log.i("coor_bur7","burda7");
//alarm durdurma içn gerekli olanlar sendBroadcast(intent) ve put extra alarm_off olacak
//Toast.makeText(this, String.valueOf(id_durak)+ " coordinat", Toast.LENGTH_SHORT).show();
AlarmManager alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent=new Intent(getApplicationContext(), AlarmReceiver.class);
intent.putExtra("extra","alarm_on");
intent.putExtra("id_durak",id_durak);
intent.putExtra("durak_ismi",durak_ismi);
Log.i("c_durak_Adi",durak_ismi);
PendingIntent pendingIntent=PendingIntent.getBroadcast(getApplicationContext(),1,intent,PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, 0,pendingIntent);
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,this);
}else {
alarmManager.set(AlarmManager.RTC_WAKEUP,0,pendingIntent);
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,this);
}
}
//和我要取消的主要活动中的代码根据通知中的额外内容予以取消
。 `字符串类型= getIntent()。getStringExtra(“ From”);
if (type != null) {
switch (type) {
case "from_ringtone":
Log.e("m1","m1");
final String stop_from_ntf=getIntent().getStringExtra("extra");
int id_durak=getIntent().getIntExtra("id_durak",-1);
String durak_Adi=getIntent().getStringExtra("durak_adi");
Log.i("id_main",String.valueOf(id_durak));
Log.e("ntf", stop_from_ntf);
assert notificationManager != null;
notificationManager.cancel(CoordinatesCompareService.NOTIFICATION_ID_COMPARESERVICE);
if(Utils.isMyServiceRunning(RingtoneService.class,this)){
final Intent intent_button=new Intent(getApplicationContext(), AlarmReceiver.class);
intent_button.putExtra("extra",stop_from_ntf);
final PendingIntent pendingIntent=PendingIntent.getBroadcast(getApplicationContext(),1,intent_button,PendingIntent.FLAG_UPDATE_CURRENT);
if(pendingIntent==null){
Log.e("pend","null");
}else {
Log.e("pend","not null");
}
final AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// intent_button.putExtra("id_durak",id_durak);
alarmManager.cancel(pendingIntent);
sendBroadcast(intent_button);
}
if (!Utils.isMyServiceRunning(CoordinatesCompareService.class,this)){
for(int g=0;g<durak_kayitList.size();g++){
if(durak_kayitList.get(g).getUid()==id_durak){
durak_kayitList.get(g).setAktif_mi(false);
durak_kayit_db.durak_kayit_dao().singleUpdate(durak_kayitList.get(g));
adapter.notifyDataSetChanged();
break;
}
}
}
break;
}
}
`