我如何在通知操作上添加操作事件
how can i add action event on notification action
我想在点击通知的停止操作时停止媒体播放器并取消通知
这是我的闹钟功能
public void alarmstart(String idd,int alarmmonth,int alarmyear,int alarmday,int alarmhour,int alarmmin)
{
AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent myIntent;
PendingIntent pendingIntent;
myIntent = new Intent(Create.this,AlarmNotificationReceiver.class);
myIntent.putExtra("title",tii);
myIntent.putExtra("note",noo);
myIntent.putExtra("id",id);
pendingIntent =
PendingIntent.getBroadcast(this,Integer.valueOf(idd),myIntent,0);
Calendar cal1=Calendar.getInstance();
cal1.set(Calendar.MONTH,alarmmonth-1);
cal1.set(Calendar.YEAR,alarmyear);
cal1.set(Calendar.DAY_OF_MONTH,alarmday);
cal1.set(Calendar.HOUR_OF_DAY,alarmhour);
cal1.set(Calendar.MINUTE,alarmmin);
cal1.set(Calendar.SECOND,0);
manager.set(AlarmManager.RTC_WAKEUP,cal1.getTimeInMillis() ,pendingIntent);
}
这是我的通知class广播接收器
public class AlarmNotificationReceiver extends BroadcastReceiver {
MediaPlayer mMediaPlayer;
PendingIntent pintent;
static String id;
@Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
String ti,no;
ti=intent.getExtras().getString("title");
no=intent.getExtras().getString("note");
id=intent.getExtras().getString("id");
builder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(ti)
.setContentText(no)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
.addAction(new android.support.v4.app.NotificationCompat.Action(
R.mipmap.stop,
"Stop",
PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)))
.setContentInfo("Info");
mMediaPlayer = MediaPlayer.create(context, R.raw.teri);
mMediaPlayer.start();
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Integer.valueOf(id),builder.build());
}
}
谁能帮我解决这个问题我是新手,几乎阅读了与该主题相关的所有以前的帖子,但无法做到这一点
您应该在 AlarmNotificationReceiver class 中使用 IntentService 并处理 mMediaPlayer 对象以停止、暂停、播放或您想要执行的任何其他操作,与以下代码相同:
public static class NotificationActionService extends IntentService {
public NotificationActionService() {
super(NotificationActionService.class.getSimpleName());
}
@Override
protected void onHandleIntent(Intent intent) {
String action = intent.getAction();
if ("StopSound".equals(action)) {
// TODO: handle action StopSound.
mMediaPlayer.stop();
// If you want to cancel the notification:
NotificationManagerCompat.from(this).cancel(NOTIFICATION_ID);
// NOTIFICATION_ID : you can (set and get) notificationid (to/from) intent
}
}
在 AlarmNotificationReceiver 的 onReceive 方法中,您应该设置意图并调用 NotificationActionService
Intent stopSoundIntent = new Intent(context,
NotificationActionService.class)
.setAction("StopSound");
PendingIntent stopSoundPendingIntent = PendingIntent.getService(context, 0,
stopSoundIntent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Sample Notification")
.setContentText("Notification text goes here")
.addAction(new NotificationCompat.Action(R.drawable.ic_launcher,
"StopSound", stopSoundPendingIntent));
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
您可以根据需要编辑此解决方案。
我希望这个解决方案对您有所帮助。
更多细节你可以看这个link
我想在点击通知的停止操作时停止媒体播放器并取消通知
这是我的闹钟功能
public void alarmstart(String idd,int alarmmonth,int alarmyear,int alarmday,int alarmhour,int alarmmin)
{
AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent myIntent;
PendingIntent pendingIntent;
myIntent = new Intent(Create.this,AlarmNotificationReceiver.class);
myIntent.putExtra("title",tii);
myIntent.putExtra("note",noo);
myIntent.putExtra("id",id);
pendingIntent =
PendingIntent.getBroadcast(this,Integer.valueOf(idd),myIntent,0);
Calendar cal1=Calendar.getInstance();
cal1.set(Calendar.MONTH,alarmmonth-1);
cal1.set(Calendar.YEAR,alarmyear);
cal1.set(Calendar.DAY_OF_MONTH,alarmday);
cal1.set(Calendar.HOUR_OF_DAY,alarmhour);
cal1.set(Calendar.MINUTE,alarmmin);
cal1.set(Calendar.SECOND,0);
manager.set(AlarmManager.RTC_WAKEUP,cal1.getTimeInMillis() ,pendingIntent);
}
这是我的通知class广播接收器
public class AlarmNotificationReceiver extends BroadcastReceiver {
MediaPlayer mMediaPlayer;
PendingIntent pintent;
static String id;
@Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
String ti,no;
ti=intent.getExtras().getString("title");
no=intent.getExtras().getString("note");
id=intent.getExtras().getString("id");
builder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(ti)
.setContentText(no)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND)
.addAction(new android.support.v4.app.NotificationCompat.Action(
R.mipmap.stop,
"Stop",
PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)))
.setContentInfo("Info");
mMediaPlayer = MediaPlayer.create(context, R.raw.teri);
mMediaPlayer.start();
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Integer.valueOf(id),builder.build());
}
}
谁能帮我解决这个问题我是新手,几乎阅读了与该主题相关的所有以前的帖子,但无法做到这一点
您应该在 AlarmNotificationReceiver class 中使用 IntentService 并处理 mMediaPlayer 对象以停止、暂停、播放或您想要执行的任何其他操作,与以下代码相同:
public static class NotificationActionService extends IntentService {
public NotificationActionService() {
super(NotificationActionService.class.getSimpleName());
}
@Override
protected void onHandleIntent(Intent intent) {
String action = intent.getAction();
if ("StopSound".equals(action)) {
// TODO: handle action StopSound.
mMediaPlayer.stop();
// If you want to cancel the notification:
NotificationManagerCompat.from(this).cancel(NOTIFICATION_ID);
// NOTIFICATION_ID : you can (set and get) notificationid (to/from) intent
}
}
在 AlarmNotificationReceiver 的 onReceive 方法中,您应该设置意图并调用 NotificationActionService
Intent stopSoundIntent = new Intent(context,
NotificationActionService.class)
.setAction("StopSound");
PendingIntent stopSoundPendingIntent = PendingIntent.getService(context, 0,
stopSoundIntent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Sample Notification")
.setContentText("Notification text goes here")
.addAction(new NotificationCompat.Action(R.drawable.ic_launcher,
"StopSound", stopSoundPendingIntent));
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
您可以根据需要编辑此解决方案。
我希望这个解决方案对您有所帮助。
更多细节你可以看这个link