【问题标题】:How to launch activity from click on notification?如何通过点击通知启动活动?
【发布时间】:2012-10-18 06:43:54
【问题描述】:

当用户点击通知时如何启动活动?我经常崩溃。我无法点击通知工作。下面显示了一个 onclick 方法。当按下一个名为 ButtonOne 的按钮时,它将在屏幕的顶部菜单栏中启动一个通知。我希望用户能够按下通知并让它启动名为 MainActivity 的活动。它崩溃并且不会启动页面。我放在 MainActivity 类中的通知代码可能有问题。怎么了?

    ButtonOne.setOnClickListener(new View.OnClickListener() {

      private int mId;

    // anonymous inner class override for on click
    public void onClick(View v) {

        Intent myIntent = new Intent(MainActivity.this, CoverFlowExample.class);
         MainActivity.this.startActivity(myIntent);



         NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(MainActivity.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, MainActivity.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(MainActivity.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());


    }
});

【问题讨论】:

    标签: android notifications onclick launcher


    【解决方案1】:

    发现问题出在 AVD android 模拟器而不是代码。由于某种原因,它没有更新早期版本的代码。再次测试它,现在它运行没有错误。

    【讨论】:

      猜你喜欢
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多