[英]how to perform some action on clear all notification
我想停止媒体播放器清除通知,以及在单击通知时如何打开某些活动
这是我的闹钟功能
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);
}
这是我的通知类广播接收者
public class AlarmNotificationReceiver extends BroadcastReceiver {
static 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");
Intent stopSoundIntent = new Intent(context,
NotificationActionService.class)
.setAction("Stop");
PendingIntent stopSoundPendingIntent = PendingIntent.getService(context, 0,
stopSoundIntent, PendingIntent.FLAG_ONE_SHOT);
// builder.setAutoCancel(true)
builder.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 NotificationCompat.Action(R.mipmap.stop,
"Stop", stopSoundPendingIntent))
.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());
}
public static class NotificationActionService extends IntentService {
public NotificationActionService() {
super(NotificationActionService.class.getSimpleName());
}
@Override
protected void onHandleIntent(Intent intent) {
String action = intent.getAction();
if ("Stop".equals(action)) {
// TODO: handle action StopSound.
mMediaPlayer.stop();
// If you want to cancel the notification:
NotificationManagerCompat.from(this).cancel(Integer.valueOf(id));
// NOTIFICATION_ID : you can (set and get) notificationid (to/from) intent
}
}
}
}
请有人能帮我这个忙吗?我是新手,几乎阅读了与该主题相关的所有以前的帖子,但无法做到这一点,我无法正确使用deleteintent,以及单击通知后如何打开活动
要在点击通知时打开活动,
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
Intent notificationIntent = new Intent(context, HomeActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
要清除onClick通知,
notification.flags |= Notification.FLAG_AUTO_CANCEL;
打开MainActivity后,您可以执行自己的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.