java - 通过单击通知停止 AlarmManager
问题描述
美好的一天,我希望有人能够帮助我!我有AlarmManager
并且NotificationCompat
在我的应用程序中。我想在单击通知时停止播放警报。这是我的代码:
public class MyAlarm extends BroadcastReceiver {
private static final int NOTIFICATION_ID = 101;
@Override
public void onReceive(final Context context, Intent intent) {
NotificationHelper notificationHelper = new NotificationHelper(context);
MediaPlayer mediaPlayer = MediaPlayer.create(context, Settings.System.DEFAULT_RINGTONE_URI);
Uri uri = intent.getData();
Intent newIntent = new Intent(context, AddNoteActivity.class);
newIntent.setData(uri);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, newIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder nb = notificationHelper.getChannel1Notification(pendingIntent);
notificationHelper.getManager().notify(NOTIFICATION_ID, nb.build());
mediaPlayer.start();
}
通知助手:
public class NotificationHelper extends ContextWrapper {
public static final String chanel1ID = "chanel1ID";
public static final String chanel1Name = "chanel 1";
private NotificationManager notificationManager;
public NotificationHelper(Context base) {
super(base);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
createChannels();
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
public void createChannels(){
NotificationChannel channel1 = new NotificationChannel(chanel1ID, chanel1Name, NotificationManager.IMPORTANCE_DEFAULT);
channel1.enableLights(true);
channel1.enableVibration(true);
channel1.setLightColor(R.color.colorPrimary);
channel1.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
getManager().createNotificationChannel(channel1);
}
public NotificationManager getManager() {
if (notificationManager == null) {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return notificationManager;
}
public NotificationCompat.Builder getChannel1Notification(PendingIntent intent){
return new NotificationCompat.Builder(getApplicationContext(), chanel1ID)
.setContentTitle("asda")
.setContentIntent(intent)
.setSmallIcon(R.drawable.ic_notifications_black_24dp)
.setAutoCancel(true);
}
和第三类的 setAlarm:
public void setAlarm(long timeInMillis){
if(Build.VERSION.SDK_INT >= 23){
mCalendar.set(
mCalendar.get(Calendar.MONTH),
mCalendar.get(Calendar.YEAR),
mCalendar.get(Calendar.DAY_OF_YEAR),
mCalendar.get(Calendar.HOUR_OF_DAY),
mCalendar.get(Calendar.MINUTE)
);
}
final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, MyAlarm.class);
intent.setData(currentUri);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.RTC, timeInMillis, pendingIntent);
希望有人会在这里帮助我,因为我几乎整天都在寻找解决方案。感谢您的时间!
解决方案
NotificationCompat.Builder.setContentIntent
提供一个 PendingIntent 以在单击通知时发送。
你正在使用
Intent newIntent = new Intent(context, AddNoteActivity.class);
因此,在您的 AddNoteActivity 中,您需要获取 MediaPlayer 的实例并调用 stop()。
推荐阅读
- json - How to access JSON object key and value in ReactJS
- apache-kafka - 为什么kafka消费者消费进程3次后不消费?
- azure - Azure 自动化/o365 Exchange 连接拒绝访问
- ms-access - 从 MS Access 升级到 SQL Server 后多值字段消失
- amazon-web-services - 无法 Ping 同一安全组下的 EC2 实例
- clojure - 从向量向量中获取字符串值
- java - Apache poi, cells missing when loading
- pine-script - 如何找到第二高的酒吧?
- ios - Add multiple values to .compactMap (Swift 5)
- ruby-on-rails - 如何获得每个演示文稿中总时间的总和?