触摸通知时如何打开活动?

时间:2014-04-08 09:43:01

标签: java android class layout notifications

我正在为我的音乐播放器构建notification,因此它会显示正在播放的音乐。 但我想添加一个功能,当我触摸它时。它会打开我的布局player.xml。这是通过MainActivity.class部署的。

我已经在developer.android.com上研究过Notifications,并找到了一种在点击通知时进行部署和活动的方法。但它确实无效。

这是我目前的代码 -

Notification.Builder builder = new Notification.Builder(getApplicationContext());
                builder.setContentTitle(songsList.get(songIndex).get("songTitle"));
                builder.setContentText("NexPlay™");
                builder.setTicker(songsList.get(songIndex).get("songTitle"));
                builder.setSmallIcon(R.drawable.ic_launcher);
                builder.setSmallIcon(R.drawable.play_default);
                builder.setAutoCancel(true);
                builder.setPriority(0);
                builder.setOngoing(true);           
                Notification notification = builder.build();
                NotificationManager notificationManger = 
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);           
                notificationManger.notify(01, notification);

感谢大家的回答!

我添加了

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, NotificationToPlayer.class), 0);
builder.setContentIntent(pendingIntent);

我的通知,效果很好!

3 个答案:

答案 0 :(得分:0)

您还想添加意图。类似的东西:

Intent actIntent = new Intent(this, do MyActivity.class);

然后在您的构建器中:

.setContentIntent(PendingIntent.getActivity(MyActivity, 131314, actIntent,
                  PendingIntent.FLAG_UPDATE_CURRENT))

答案 1 :(得分:0)

你需要在你的代码上添加意图: -

int icon = R.drawable.app_launcher;
                long when = System.currentTimeMillis();
                NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

                Notification notification = new Notification(icon, message.split(":::")[1], when);

                String title = context.getString(R.string.app_name);
                Intent notificationIntent = null;
                    notificationIntent = new Intent(context, MessageDetail.class);

                PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                notification.setLatestEventInfo(context, title, message.split(":::")[1], intent);
                notification.flags |= Notification.FLAG_AUTO_CANCEL;

                // Play default notification sound
                notification.defaults |= Notification.DEFAULT_SOUND;
                // Vibrate if vibrate is enabled
                notification.defaults |= Notification.DEFAULT_VIBRATE;
                notificationManager.notify(0, notification);

答案 2 :(得分:0)

试试这个

 private void showNotification() {
        // In this sample, we'll use the same text for the ticker and the expanded notification
        CharSequence text = getText(R.string.service_started);

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(R.drawable.android, text,
                System.currentTimeMillis());

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, ServiceLauncher.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, getText(R.string.service_label),
                       text, contentIntent);
        notification.defaults |= Notification.DEFAULT_SOUND;
        // Send the notification.
        // We use a layout id because it is a unique number.  We use it later to cancel.
        nm.notify(R.string.service_started, notification);
    }

希望这能解决您的问题