我正在尝试在Android上自定义通知的按钮点击时执行一些操作,例如暂停音乐或播放音乐。
目前我是使用以下方式实现:
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, "Custom Notification", when);
NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.layout);
contentView.setTextViewText(R.id.textView1, "Custom notification");
contentView.setOnClickPendingIntent(R.id.button1, pIntent);
notification.contentView = contentView;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
notification.defaults |= Notification.DEFAULT_SOUND; // Sound
mNotificationManager.notify(1, notification);
但是这个通知会带我进入另一个活动界面。 有没有办法在同一个活动中实现通知操作。
例如,我发送一个通知,用户点击它时,它不会跳转到其他活动界面,而是调用当前活动/服务中的一般方法。