按下通知时启动活动

时间:2013-12-23 10:45:50

标签: android android-notifications

根据http://developer.android.com/guide/topics/ui/notifiers/notifications.html#SimpleNotification

的方法
private void sendNotification(String msg) {

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("MyApp")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg).setDefaults(Notification.DEFAULT_ALL)
            .setAutoCancel(true);

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

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack
    stackBuilder.addParentStack(MainActivity.class);
    // Adds the Intent to the top of the stack
    stackBuilder.addNextIntent(resultIntent);

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

    mBuilder.setContentIntent(resultPendingIntent);

    mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

这只是启动MainActivity,但我想让它启动ActionActivity。你看到有什么错误吗?

2 个答案:

答案 0 :(得分:3)

在MainActivity的位置添加ActionActivity

 // Adds the back stack
stackBuilder.addParentStack(ActionActivity.class);

答案 1 :(得分:0)

删除 stackBuilder 并尝试这样。在我的情况下,它的工作..

Intent notificationIntent =new Intent(context,ActionActivity.class);
// set intent so it does not start a new activity
   notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
   PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
   this.notification.setLatestEventInfo(context, title, message, intent);
   this.notification.flags |= Notification.FLAG_AUTO_CANCEL;
   // Play default notification sound
      this.notification.defaults |= Notification.DEFAULT_SOUND;
   // Vibrate if vibrate is enabled
      this.notification.defaults |= Notification.DEFAULT_VIBRATE;
      notificationManager.notify(0, notification);