2015-05-27 29 views
2

我的查詢是,我試圖根據不同的條件重定向通知意圖,我正在處理報警系統,我需要將通知意圖重定向到一些活動答:如果用戶在鬧鐘播放和完成時點擊該通知,則應該將通知重定向到其他活動(如果仍然可用)。這是我的代碼。任何幫助將不勝感激!如何在Android通知鬧鐘中處理多個意圖操作

private void DisplayNotification(String AlarmName, Context context) { 
    // Context context = context.getApplicationContext(); 
    NotificationManager notificationManager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    Intent mIntent; 
    SharedPreferences alarmindicator = context.getSharedPreferences(
      "notifyintent", context.MODE_PRIVATE); 
    int code; 
    SharedPreferences.Editor editor = alarmindicator.edit(); 
    if (alarmindicator.getBoolean("notifyintentPlaying", true)) { 
     mIntent = new Intent(context, AlarmAlertActivity.class); 
     Toast.makeText(context, "inalmact", 0).show(); 
     code = 1; 
    } 

    else { 
     editor.putBoolean("notifyintentPlaying", true); 
     editor.commit(); 
     mIntent = new Intent(context, MainActivity.class); 
     // AlarmAlertActivity.this.finish(); 
     code = 2; 
     Toast.makeText(context, "in main act", 0).show(); 

    } 

    Bundle bundle = new Bundle(); 

    bundle.putString("test", "test"); 
    mIntent.putExtras(bundle); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, code, 
      mIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    Resources res = context.getResources(); 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
      context); 

    builder.setContentIntent(pendingIntent) 
      .setSmallIcon(R.drawable.notifyiconlarg) 
      .setLargeIcon(
        BitmapFactory.decodeResource(res, 
          R.drawable.notifyiconlarg)) 
      // .setTicker(res.getString(R.string.notification_title)) 
      .setTicker(AlarmName).setAutoCancel(true) 
      .setContentTitle(AlarmName) 
      .setContentText("Time to offer " + AlarmName + " Prayers"); 

    notificationManager = (NotificationManager) context 
      .getSystemService(context.NOTIFICATION_SERVICE); 

    notificationManager.notify(123, builder.build()); 
} 

回答

2

你有火從通知並在BroadcastReceiveronReceive()方法,你必須決定要啓動的活動的廣播接收機....

UPDATE

寫一個BroadcastReceiver像這樣

public class MyReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     int code; 
     Intent mIntent; 
     SharedPreferences.Editor editor = alarmindicator.edit(); 
     if (alarmindicator.getBoolean("notifyintentPlaying", true)) { 
      mIntent = new Intent(context, AlarmAlertActivity.class); 
      Toast.makeText(context, "inalmact", 0).show(); 
      code = 1; 
     } else { 
      editor.putBoolean("notifyintentPlaying", true); 
      editor.commit(); 
      mIntent = new Intent(context, MainActivity.class); 
      // AlarmAlertActivity.this.finish(); 
      code = 2; 
      Toast.makeText(context, "in main act", 0).show(); 
     } 
     context.startActivity(intent); 
    } 

然後像這樣創建Pending intent

Intent myIntent=new Intent("some_action") 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(context, code, 
       myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
相關問題