2015-03-08 51 views
0

我有一個調用幫助器的活動,在那裏創建一個通知,在選定的時間內彈出。它效果很好。從服務中刪除待處理的警報/通知

我想在不同的活動中創建另一個功能,可以在通知彈出之前刪除未決的警報。我嘗試了很多我在網上找到的方式,但沒有成功。我究竟做錯了什麼?

// Add Alarm 
public class Helper { 
    public void SetAlarm(Activity activity, int requestCode, Calendar calendar, String title, ArrayList<PendingIntent> intentArray) { 
     AlarmManager alarmManager = (AlarmManager)activity.getSystemintent(Context.ALARM_intent); 

     Intent intent = new Intent(activity.getBaseContext(), MyReceiver.class); 
     intent.putExtra("title",title); 

     PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, requestCode, intent, 0); 
     alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 
     intentArray.add(pendingIntent); 
    } 
} 


// Delete Alarm 
public class MyActivity extends Activity { 
    ProgressDialog progress; 
    // ... 
    public void Delete(int requestCode , String title) { 
     progress = ProgressDialog.show(this, getString(R.string.title), getString(R.string.text), true); 
     new Thread(new Runnable() { 
       @Override 
       public void run() 
       { 
       // .... 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() 
        { 
         try { 
          Intent intent = new Intent(getBaseContext(), MyReceiver.class); 
          intent.putExtra("title",title); // don't know if really needed 
          AlarmManager alarmManager = (AlarmManager)getSystemintent(Context.ALARM_intent); 
          PendingIntent pi = PendingIntent.getBroadcast(this, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
          alarmManager.cancel(pi); 
          // Tried that also - didn't work: 
          // PendingIntent.getBroadcast(this, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT).cancel(); 
          // pi.cancel(); 
         } 
         catch(Exception e) { 
          // ... 
         } 
         progress.dismiss(); 
        } 
       }); 
       } 
     }).start(); 
    } 
} 





// BroadcastReceiver 
public class MyReceiver extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     Intent newIntent = new Intent(context, MyAlarmService.class); 
     newIntent.putExtra("title", intent.getExtras().getString("title")); 
     context.startService(newIntent); 
    } 
} 

public class MyAlarmService extends Service 
{ 
    // ... 

    @SuppressWarnings("static-access") 
    @Override 
    public int onStartCommand(Intent intent, int flag, int startId) 
    { 
     super.onStartCommand(intent, START_STICKY, startId); 

     mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE); 
     Intent intent1 = new Intent(this.getApplicationContext(), MainActivity.class); 
     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 notification = new Notification.Builder(this.getApplicationContext()) 
      .setContentTitle(getString(R.string.app_name)) 
      .setContentText(getString(R.string.conText)) 
      .setWhen(System.currentTimeMillis()) 
      .setContentIntent(pendingNotificationIntent) 
      .build(); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     mManager.notify(0, notification); 
     return START_STICKY; 
    } 
} 
+0

當您取消PendingIntent時,您需要提供相同的'Intent'。 – Machado 2015-03-10 19:57:07

+0

這是一個完全不同的活動。我如何使用相同的意圖?我不能重新創建它嗎? – TamarG 2015-03-10 20:08:58

+0

http://stackoverflow.com/questions/19593442/android-get-same-intent-from-different-activities – Machado 2015-03-10 20:12:00

回答

1

解除意圖必須與原始(動作,數據類型,類和類別是相同的)。你有沒有試圖調用PendingIntent.getBroadcast()沒有標誌? (0代替PendingIntent.FLAG_UPDATE_CURRENT)。並確保您使用的是相同的請求代碼。

+0

確實 - 我錯誤地使用了錯誤的requestCode,我沒有注意到! – TamarG 2015-03-12 13:21:14

0

這樣創造你的警報:

public static void createAlarm(Context context, Calendar calAlarm, String ALARM_ACTION_NAME) 
{ 
    try 
    { 
     AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 

     // Create an alarm intent 
     Intent alarmIntent = new Intent(ALARM_ACTION_NAME); 

     // Create the corresponding PendingIntent object 
     PendingIntent alarmPI = PendingIntent.getBroadcast(context, 0, alarmIntent, 0); 

     // cancel any alarms previously set 
     alarmMgr.cancel(alarmPI); 

     // Register the alarm with the alarm manager 
     alarmMgr.set(AlarmManager.RTC_WAKEUP, calAlarm.getTimeInMillis(), alarmPI); 

    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 

你可以取消這樣的:

public static void cancelAlarm(Context context, String ALARM_ACTION_NAME) 
{ 
    try 
    { 
     AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 

     // Create an alarm intent 
     Intent alarmIntent = new Intent(ALARM_ACTION_NAME); 

     // Create the corresponding PendingIntent object 
     PendingIntent alarmPI = PendingIntent.getBroadcast(context, 0, alarmIntent, 0); 

     // cancel any alarms previously set 
     alarmMgr.cancel(alarmPI); 

    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 
+0

ALARM_ACTION_NAME是什麼? – TamarG 2015-03-10 21:01:48

+0

是您用來創建鬧鐘的字符串... – Christian 2015-03-10 21:04:36

+0

您在哪裏使用MyReciever?你在哪裏創建通知? – TamarG 2015-03-10 21:22:38