2014-06-11 15 views
0

我實際上在Android中使用notifications在Android中點擊通知後在應用中添加新視圖?

做我的應用測試,我跟着the guide on android.developer

我創建了我的通知,它效果很好。當我點擊它時,它會從通知選項卡中刪除通知,並在我進入我的活動之後。

這裏是我用來做代碼:

public void createSimpleNotification() { 
    NotificationCompat.Builder mNotifBuilder = new NotificationCompat.Builder(this.getApplicationContext()) 
    .setSmallIcon(R.drawable.ic_notif).setContentTitle("Simple notif").setContentText("Welcome on our app, click here"); 

    Intent resultIntent = new Intent(this, MainActivity.class); 

    TaskStackBuilder mStackBuilder = TaskStackBuilder.create(this.getApplicationContext()); 
    mStackBuilder.addParentStack(MainActivity.class); 
    mStackBuilder.addNextIntent(resultIntent); 

    PendingIntent resultPendingIntent = mStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

    mNotifBuilder.setContentIntent(resultPendingIntent); 

    NotificationManager mNotifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotifManager.notify(0, mNotifBuilder.build()); 
} 

我只有我的MainActivityButton。當我點擊按鈕時,通知出現,我點擊通知,我回到我的MainActivity。

這裏是我的問題:

我想點擊的通知後,更新我MainActivity UI。點擊通知後,我需要創建一個Dialog誰顯示通知的文本。

如何修改我的Activity和點擊的通知後添加新的View

回答

0

使用已經存在的視圖。所以你在你的活動中創建它,並將它的可見性設置爲FALSE

現在,如果你希望它出現,把你的通知文本中,並設置能見度到TRUE。這應該成爲你的要求;)

+0

處理它我同意你的意見,但我如何訪問我的查看onClick on通知? – Androzr

+0

您在活動中創建視圖。只要將其設爲靜態類變量即可。那麼你可以通過'ActivityWithView.myNotificationView'來訪問它,例如 – PKlumpp

+0

是的,我明白你說什麼,但我怎麼知道通知是否被點擊? – Androzr

0

您可以添加把一個額外的給你的結果意向

如:

resultIntent.putExtra(EXTRA_NAME, EXTRA_VALUE);

然後在MainActivity onNewIntent

protected void onNewIntent(Intent intent) { 
     if (intent.hasExtra(EXTRA_NAME)) { 
      // do your things here 
     } 
    } 
相關問題