2010-05-26 65 views
7

我一直有一个问题,通知没有打开/正确的活动,当它被点击。安卓状态栏通知 - 选择通知时打开正确的活动

我的通知代码(位于延伸服务类):

Context context = getApplicationContext(); 

    CharSequence contentTitle = "Notification"; 

    CharSequence contentText = "New Notification"; 

    final Notification notifyDetails = 
     new Notification(R.drawable.icon, "Consider yourself notified", System.currentTimeMillis()); 

    Intent notifyIntent = new Intent(context, MainActivity.class); 

    PendingIntent intent = 
      PendingIntent.getActivity(context, 0, 
      notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT | Notification.FLAG_AUTO_CANCEL); 

    notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent); 

    ((NotificationManager)getSystemService(NOTIFICATION_SERVICE)).notify(NOTIFICATION_ID, notifyDetails); 

如果我点击该通知,而其创建的服务应用程序打开,通知消失(由于FLAG_AUTO_CANCEL),但活动不会切换。

如果我点击主屏幕上的通知,通知消失,并且我的应用程序被带到最前面,但是它仍然保留在进入主屏幕前打开的活动,而不是进入主屏幕。

我在做什么错?我如何指定将被提升的活动?

回答

14

实际上可能回答了自己的问题:

Intent notifyIntent = new Intent(Intent.ACTION_MAIN); 
notifyIntent.setClass(getApplicationContext(), Main.class); 
+0

感谢您分享您的答案,请你提什么“Main.class”载? – OnlyHope 2013-01-02 08:27:17

+0

什么是getApplicationContext()? – OnlyHope 2013-01-02 08:28:34

+1

Main.class将是您想要打开/转到的活动。 和getApplicationContext是Activity类中的一个方法,它从ContextWrapper类继承。 http://developer.android.com/reference/android/content/ContextWrapper.html#getApplicationContext%28%29 – 2013-01-03 02:11:28