通知操作按钮不会停止闹钟铃声
Posted
技术标签:
【中文标题】通知操作按钮不会停止闹钟铃声【英文标题】:Notification action button dosen't stop the alarm ringtone 【发布时间】:2017-04-06 11:21:22 【问题描述】:我设置了一个闹钟,我想在不打开活动的情况下使用通知操作按钮禁用正在进行的闹钟。
我在下面附上了我的整个代码。
当用户设置闹钟时,它会调用广播接收器并从他们那里调用我的铃声服务来启动铃声。它会发出通知。在该通知中,它将有一个禁用按钮。我已经完成了我在互联网上找到的所有资源。但是
通知上的禁用按钮不起作用。我的错在哪里
onToggled 按钮从主要活动开始警报
public void OnToggleClicked(View view)
long time;
if (((ToggleButton) view).isChecked())
Toast.makeText(MainActivity.this, "ALARM ON", Toast.LENGTH_SHORT).show();
Calendar calendar = Calendar.getInstance();
if (Build.VERSION.SDK_INT >= 23)
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getMinute());
else
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
intent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// setRepeating() lets you specify a precise custom interval--in this case,
// 1 minutes.
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60, pendingIntent);
else
alarmManager.cancel(pendingIntent);
Toast.makeText(MainActivity.this, "ALARM OFF", Toast.LENGTH_SHORT).show();
我的广播接收器类
public class AlarmReceiver extends BroadcastReceiver
public static Ringtone ringtone;
@Override
public void onReceive(Context context, Intent intent)
Toast.makeText(context, "Alarm! Wake up! Wake up!", Toast.LENGTH_LONG).show();
// using service class
Intent i = new Intent(context, RingtonePlayingService.class);
context.startService(i);
createNotification(context);
public void createNotification(Context context)
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(android.R.drawable.ic_dialog_alert)
.setContentTitle("It is prayer time")
.setContentText("Prayer")
.setSmallIcon(R.mipmap.ic_launcher)
.setSubText("Tab to cancel the ringtone")
.setPriority(NotificationCompat.PRIORITY_HIGH);
//To add a dismiss button
Intent dismissIntent = new Intent(context, RingtonePlayingService.class);
dismissIntent.setAction(RingtonePlayingService.ACTION_DISMISS);
PendingIntent pendingIntent = PendingIntent.getService(context,
123, dismissIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action action = new NotificationCompat.Action
(android.R.drawable.ic_lock_idle_alarm, "DISMISS", pendingIntent);
builder.addAction(action);
// end of setting action button to notification
Intent intent1 = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 123, intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pIntent);
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(NOTIFICATION_SERVICE);
Notification notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(123, notification);
我的铃声播放服务类
public class RingtonePlayingService extends Service
private static final String TAG = RingtonePlayingService.class.getSimpleName();
private static final String URI_BASE = RingtonePlayingService.class.getName() + ".";
public static final String ACTION_DISMISS = URI_BASE + "ACTION_DISMISS";
private Ringtone ringtone;
@Override
public IBinder onBind(Intent intent)
return null;
@Override
public int onStartCommand(Intent intent, int flags, int startId)
Log.d(TAG, "onStartCommand");
if(intent == null)
Log.d(TAG, "The intent is null.");
return START_REDELIVER_INTENT;
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null)
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
ringtone = RingtoneManager.getRingtone(this, alarmUri);
ringtone.play();
String action = intent.getAction();
if(ACTION_DISMISS.equals(action))
dismissRingtone();
return START_NOT_STICKY;
public void dismissRingtone()
Intent i = new Intent(this, RingtonePlayingService.class);
stopService(i);
@Override
public void onDestroy()
ringtone.stop();
【问题讨论】:
【参考方案1】:那是我的错
当我单击禁用通知时,它再次调用接收器类,这就是铃声继续播放的原因。所以我一直在修改,效果很好。
public class RingtonePlayingService extends Service
private static final String TAG = RingtonePlayingService.class.getSimpleName();
private static final String URI_BASE = RingtonePlayingService.class.getName() + ".";
public static final String ACTION_DISMISS = URI_BASE + "ACTION_DISMISS";
private Ringtone ringtone;
@Override
public IBinder onBind(Intent intent)
return null;
@Override
public int onStartCommand(Intent intent, int flags, int startId)
Log.d(TAG, "onStartCommand");
if (intent == null)
Log.d(TAG, "The intent is null.");
return START_REDELIVER_INTENT;
String action = intent.getAction();
if (ACTION_DISMISS.equals(action))
dismissRingtone();
else
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null)
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
ringtone = RingtoneManager.getRingtone(this, alarmUri);
ringtone.play();
return START_NOT_STICKY;
public void dismissRingtone()
// stop the alarm rigntone
Intent i = new Intent(this, RingtonePlayingService.class);
stopService(i);
// also dismiss the alarm to ring again or trigger again
AlarmManager aManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
aManager.cancel(pendingIntent);
// Canceling the current notification
NotificationManager notificationManager =
(NotificationManager)getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
notificationManager.cancel(321);
@Override
public void onDestroy()
ringtone.stop();
【讨论】:
以上是关于通知操作按钮不会停止闹钟铃声的主要内容,如果未能解决你的问题,请参考以下文章