停止闹钟在另一个活动中响起 - android
Posted
技术标签:
【中文标题】停止闹钟在另一个活动中响起 - android【英文标题】:stop alarm from ringing in another activity - android 【发布时间】:2012-12-04 21:02:13 【问题描述】:谁能教我如何在我的代码中阻止闹钟响起。我有什么方法可以阻止其他活动发出警报吗?因为,到目前为止,即使我按下了关闭按钮,警报也会一直响起。
alertDialogBuilder.setTitle("Alarm");
alertDialogBuilder
.setMessage("Stop Alarm")
.setCancelable(false)
.setPositiveButton("Dismiss",new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog,int id)
eReceiver = new EAlarmReceiver();
Ringtone r = eReceiver.ringAlarm(context);
r.stop();
Toast.makeText(context.getApplicationContext(), "Alarm Stopped", Toast.LENGTH_LONG).show();
Intent openInterface = new Intent("proj.receiver.RECEIVERINTERFACE");
openInterface.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(openInterface);
);
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
// end oncreate()
这里是我开始闹钟的地方
public void onReceive(Context context, Intent intent)
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
Object[] pdusObj = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdusObj.length];
for (int i = 0; i<pdusObj.length; i++)
messages[i] = SmsMessage.createFromPdu ((byte[])
pdusObj[i]);
sender = messages[i].getOriginatingAddress();
for (SmsMessage msg : messages)
if (msg.getMessageBody().contains("firealert"))
ringAlarm(context);
Intent openStopAlarm = new Intent("proj.receiver.STOPALARM");
openStopAlarm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(openStopAlarm);
//end if
//end for
// end onreceive
public Ringtone ringAlarm(Context context)
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if(alert == null)
// alert is null, using backup
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if(alert == null) // I can't see this ever being null (as always have a default notification) but just incase
// alert backup is null, using 2nd backup
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
Ringtone r = RingtoneManager.getRingtone(context.getApplicationContext(), alert);
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
int maxVolumeAlarm = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
//int maxVolumeRing = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, maxVolumeAlarm,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
//audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolumeRing,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
r.play();
Toast.makeText(context.getApplicationContext(), "alarm started", Toast.LENGTH_LONG).show();
return r;
//end ringAlarm()
【问题讨论】:
【参考方案1】:在 BroadcastReceiver 中声明的 Ringtone r
值与您在 Activity 中定义的值不同,您必须做一些事情,使 r 变量在您的应用程序中具有更大的范围:
您可以将 r 变量定义为
static Ringtone;
在您的广播接收器中,然后在 onReceive() 方法中为其赋值,然后从您的活动中调用该变量,只需将新的 r 变量声明为如下:
Ringtone r=yourpackge.Your_BroadcastReceiver_Class.r
然后调用
r.stop();
【讨论】:
以上是关于停止闹钟在另一个活动中响起 - android的主要内容,如果未能解决你的问题,请参考以下文章