【问题标题】:How can I open activity when notification is touched?触摸通知时如何打开活动?
【发布时间】:2014-04-08 09:43:01
【问题描述】:

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

我在 developer.android.com 上研究了通知,并找到了一种在单击通知时进行部署和活动的方法。但它没有用。

这是我当前的代码 -

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

到我的通知,效果很好!

【问题讨论】:

标签: java android class layout notifications


【解决方案1】:

试试这个

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

希望这能解决您的问题

【讨论】:

  • 谢谢,你的帮助最简单,我刚刚添加了 - PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, NotificationToPlayer.class), 0); builder.setContentIntent(pendingIntent);并且成功了:)
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    您还需要添加一个意图。比如:

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

    然后在您的构建器中:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-05
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多