2013-09-24 2 views
0

я реализовали сигнал тревоги, как:отмены тревоги уведомления в андроида

 calendar.set(Calendar.HOUR_OF_DAY, hour); 
     calendar.set(Calendar.MINUTE, minute); 
     calendar.set(Calendar.SECOND, 0); 
     alarmManager.setRepeating(AlarmManager.RTC, 
       calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, 
       pendingIntent); 

и BroadcastReceiver:

public class MyReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    Intent service1 = new Intent(context, WeekTickAlarmService.class); 
    context.startService(service1); 
} 

} 

и услуги:

public class WeekTickAlarmService extends Service { 

@SuppressWarnings({ "static-access", "deprecation" }) 
@Override 
public void onStart(Intent intent, int startId) { 
    super.onStart(intent, startId); 

    mManager = (NotificationManager) this.getApplicationContext() 
      .getSystemService(
        this.getApplicationContext().NOTIFICATION_SERVICE); 
    Intent intent1 = new Intent(this.getApplicationContext(), 
      MainActivity.class); 
    intent1.putExtra("notification", true); 
    Notification notification = new Notification(R.drawable.icon, 
      Shares.getStringFromResources(this.getApplicationContext(), 
        R.string.alarm_service_notification_message), 
      System.currentTimeMillis()); 
    notification.sound = RingtoneManager 
      .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP 
      | Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
      this.getApplicationContext(), 0, intent1, 
      PendingIntent.FLAG_UPDATE_CURRENT); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notification.setLatestEventInfo(this.getApplicationContext(), Shares 
      .getStringFromResources(getApplicationContext(), 
        R.string.alarm_service_notification_bar_title), Shares 
      .getStringFromResources(getApplicationContext(), 
        R.string.alarm_service_notification_bar_description), 
      pendingNotificationIntent); 
    mManager.notify(666, notification); 
} 
} 

моя проблема, когда я нажмите уведомление, уведомление исчезнет, ​​но на некоторых устройствах система уведомляет более одного раза!

+0

, когда я нажимаю кнопку Очистить в уведомлениях я не имею никакой проблемы! но когда я нажимаю на уведомление, через короткое время он снова уведомляется! –

ответ

0

Что-то вроде этого:

public void CancelAlarm(Context context) { 

     Intent intent = new Intent(context, MyReceiver .class); 
     PendingIntent sender = PendingIntent 
       .getBroadcast(context, 0, intent, 0); 
     AlarmManager alarmManager = (AlarmManager) context 
       .getSystemService(Context.ALARM_SERVICE); 
     alarmManager.cancel(sender); 
    } 
+0

пока не работает! –

+0

После отмены сигнала тревоги работает служба? –

+0

после уведомления, если я отменяю будильник, он продолжает уведомлять! –

Смежные вопросы