单击通知时启动应用程序的活动

时间:2013-05-18 09:24:52

标签: android

我喜欢在点击通知时在我的应用程序中启动我的一项活动。 我设计了一个待定意图,如链接Notifications中所述。 但是,当我单击通知时,会启动一个活动,但这不是应该从我的应用程序启动的活动。只启动与我的活动具有相同类名的活动。我在我的活动中设置了一个断点,断点从未被击中。 怎么了? NotificationListActivity是我想要发布的。现在启动标题为NotificationListActivity的活动,但不启动我的活动。

Intent resultIntent = new Intent(thisclasscontext, NotificationListActivity.class);
resultIntent.putExtra("MOBILENUMBER", tel);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(thisclasscontext);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(NotificationListActivity.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);

1 个答案:

答案 0 :(得分:0)

您可以使用以下作为参考,

  public void notification()
  {
      int mId=1;
      NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("My notification")
                .setContentText("Hello World!");

        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(MainActivity.this, SecondActivity.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(MainActivity.this);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(SecondActivity.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());
  }