[英]Stop AlarmManager by click on Notification
美好的一天,我希望有人能夠幫助我! 我的應用程序中有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();
}
NotificationHelper:
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);
}
並在第3類中設置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()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.