2013-10-08 46 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它。 –