2013-10-08 43 views
-1

我收到了一些關於通知意圖的問題。我有2項活動(A & B)。活動A是我的應用程序的主要活動。用戶將經歷的第一個活動。至於活動B是當用戶通過點擊活動A上的按鈕進入時。通知意圖活動

事情是當我點擊我的通知時,它會引導我進入活動B.然後指引我回到活動A onBackPressed。但是,當我關閉應用程序並通過多任務選項將其打開時,它會在Activity B恢復我的應用程序。我希望應用程序在關閉它之後開始回到Activity A。

順序應是

活性A - >活動B.

通知的onClick - >活動B(onBackPressed) - >活動A - >關閉。

重新打開應用/多任務功能開 - >活動A

請不要讓我知道是否有任何其他信息,我可以給我的問題提供了一個正確的認識。

GCM_Intent.class

Notification note = new Notification(icon, msgTopic ,System.currentTimeMillis()); 
    Intent i=new Intent(this, Activity_B.class); 
    i.putExtra("topicId", topicId); 
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| 
    Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent pi=PendingIntent.getActivity(this, CommunitiesappConstant.NOTIFICATION_ID, i, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT); 
    note.setLatestEventInfo(this, topicName ,msgInfo+message, pi); 
    note.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS; 
    note.ledARGB |= 0xff0000ff; 
    note.ledOffMS |= 1000; 
    note.ledOnMS |= 300; 


    mgr.notify(CommunitiesappConstant.NOTIFICATION_ID, note); 

活動B.class

@Override 
public void onCreate(Bundle savedInstanceState){ 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_b); 
    Bundle bundle = getIntent().getExtras(); 
    topicId = bundle.getString("topicId"); 

} 

@Override 
public void onBackPressed() { 
    super.onBackPressed(); 
    Log.e(TAG, "onBackPressed"); 
    Intent i = new Intent(Activity_B.this, Activity_A.class); 
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    startActivity(i); 
    finish(); 
} 

回答

1

通知是可以顯示到你的應用程序的正常UI.This之外的用戶的消息是使用的通知完美代碼。

NotificationCompat.Builder mBuilder = 
     new NotificationCompat.Builder(this) 
     .setSmallIcon(R.drawable.notification_icon) 
     .setContentTitle("My notification") 
     .setContentText("Hello World!"); 
// Creates an explicit intent for an Activity in your app 
Intent resultIntent = new Intent(this, ResultActivity.class); 

// The stack builder object will contain an artificial back stack for the 
// started Activity. 
// This ensures that navigating backward from the Activity leads out of 
// your application to the Home screen. 
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
// Adds the back stack for the Intent (but not the Intent itself) 
stackBuilder.addParentStack(ResultActivity.class); 
// Adds the Intent that starts the Activity to the top of the stack 
stackBuilder.addNextIntent(resultIntent); 
PendingIntent resultPendingIntent = 
     stackBuilder.getPendingIntent(
      0, 
      PendingIntent.FLAG_UPDATE_CURRENT 
     ); 
mBuilder.setContentIntent(resultPendingIntent); 
NotificationManager mNotificationManager = 
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
// mId allows you to update the notification later on. 
mNotificationManager.notify(mId, mBuilder.build()); 
+0

凡正是我需要將這個代碼? 我想在我的網站上發佈任何新帖子時向用戶顯示通知..我使用WebView創建了一個Android應用程序 – Neo

0

做這樣onback按(),

Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1)); 
     current.finish() 
+0

如何獲取此* mIdList.get(length-1)*? –

+0

mIdList.get(length-1)不過是你的第一個activity.Try它。 –