2015-11-06 88 views
0

我有一個android應用程序需要定期生成通知,當我們點擊通知時,通知中顯示的消息將顯示在原始應用程序的活動內的TextView中。Android Broadcast Reciever打開意圖

爲此,我創建了一個AlarmReceiver.java類

public class AlarmReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     Log.d("ME", "Notification started"); 

     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 


      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) 
                .setSmallIcon(R.drawable.ic_noticon) 
                .setContentTitle("Notification") 
                .setContentText("You have to open app to see this message in detail"); 


      NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
      mNotificationManager.notify(1, mBuilder.build()); 


      Intent resultIntent = new Intent("com.app.AlarmBack"); 
      TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
      stackBuilder.addParentStack(AlarmBack.class); 
      stackBuilder.addNextIntent(resultIntent); 
      PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_ONE_SHOT); 
      mBuilder.setContentIntent(resultPendingIntent); 


    }  

也是我創建的類名爲AlarmBack.java

public class AlarmBack extends MainActivity { 


TextView tv_alert_back_display; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 


    getLayoutInflater().inflate(R.layout.alert_back, frameLayout); 
    mDrawerList.setItemChecked(position, true); 
    setTitle(mTitle); 


    tv_alert_back_display = (TextView) findViewById(R.id.tv_alert_back_display); 


    tv_alert_back_display.setText("This is the message that needs your attension"); 
}} 

的意圖,但問題是,這是行不通的。它並沒有打開它想要打開的意圖。所以,有人可以幫助解決我的問題。

+0

要添加這個方法當通知被點擊或者你想改變t時打開你的活動他的通知的標題和內容? –

+0

@ZubairAkber我想在點擊通知時打開活動 –

+0

你有沒有試過我的答案它適用於我希望所以它也會爲你工作 –

回答

1

呼叫這一點,並在你的報警接收機

private Notification getNotification(Context context, String title, String content) { 
    Notification.Builder builder = new Notification.Builder(context); 
    builder.setContentTitle(title); 
    builder.setContentText(content); 
    builder.setSmallIcon(R.drawable.icon_64); 
    builder.setAutoCancel(true); 
    builder.setDefaults(android.app.Notification.DEFAULT_ALL); 


    Intent resultIntent = new Intent(context, SplashActivity.class); //Activity that you want to open 
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
    stackBuilder.addParentStack(SplashActivity.class);    // Activity that you want to open 
    stackBuilder.addNextIntent(resultIntent); 
    PendingIntent resultPendingIntent = 
      stackBuilder.getPendingIntent(
        0, 
        PendingIntent.FLAG_UPDATE_CURRENT 
      ); 
    builder.setContentIntent(resultPendingIntent); 
    return builder.getNotification(); 
} 
+0

答案的第一部分我到底應該叫它 –

+0

所以你試過了嗎 –

+0

對不起。是AlarmReciever類的OnRecieve void方法中的(代碼的第一部分)? –

1

在設置生成器上的意圖後,您需要調用notify(),而不是之前。否則,由builder.build()創建的通知在添加到通知管理器時不會具有這些意圖。

0

您需要setContentIntent爲您NotificationBuilder:在你的AlarmReceiver OnRecieve方法

int taskID = 1; 
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
notificationManager.notify(taskID, getNotification(context, "title", "content")); 

NotificationCompat.Builder builder = new NotificationCompat.Builder(
      context).setSmallIcon(R.drawable.app_icon) 
      .setContentTitle("Your Title").setContentText(""); 

    Intent intent = new Intent(context, YourMainActivity.class); 
    intent.setAction(Intent.ACTION_MAIN); 
    intent.addCategory(Intent.CATEGORY_LAUNCHER); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, 
      intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    builder.setContentIntent(pendingIntent); 
相關問題