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

Android 单击通知后启动应用程序的活动,android,Android,我喜欢在单击通知时启动应用程序中的一项活动。 我设计了一个链接中讨论的待定意图。 但是,当我单击通知时,会启动一个活动,但这不是应该从我的应用程序启动的活动。仅启动与我的活动具有相同类名的活动。我在我的活动中设置了一个断点,而该断点从未达到。 怎么了?NotificationListActivity是我喜欢启动的活动。现在启动了名为NotificationListActivity的活动,但没有启动我的活动 Intent resultIntent = new Intent(thisclasscon

我喜欢在单击通知时启动应用程序中的一项活动。 我设计了一个链接中讨论的待定意图。 但是,当我单击通知时,会启动一个活动,但这不是应该从我的应用程序启动的活动。仅启动与我的活动具有相同类名的活动。我在我的活动中设置了一个断点,而该断点从未达到。 怎么了?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);

您可以使用下面的内容作为参考它的工作原理

  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());
  }

单击通知时是否要启动NotificationListActivity?您的MainActivity已启动,并显示您有一个按钮,当您单击Motionization时,会显示Motionization,当您单击通知时,您需要导航到NotificationListActivity?我很困惑,你能把你的日志贴出来吗?为什么你会有两个同名活动。@Raghunandan当我从指定号码收到带有指定信息的短信时,我的应用程序会发出通知。当用户单击通知时,我想启动我的应用程序中的一个活动。此代码在BroadcastReceiver类中实现。因此MainActivity.this无法使用您可以将活动上下文传递给BroadCastReceiver类的构造函数