有人可以教我如何阻止闹钟响起我的代码。我有停止其他活动警报的正确方法吗?因为到目前为止,即使按了关闭按钮,警报仍会持续响起。
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()
最佳答案
在BroadcastReceiver中声明的Ringtone r
值与您在Activity中定义的值不同,您必须做一些使r变量在应用程序中具有更大作用域的操作:
您可以将r变量定义为
static Ringtone;
在您的broadcastreceiver中,然后在onReceive()方法中为其赋值,然后从您的活动中调用该变量,只需将新的r变量声明为以下变量:
Ringtone r=yourpackge.Your_BroadcastReceiver_Class.r
然后打电话
r.stop();
关于android - 停止闹钟在另一个 Activity 中响起-Android,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13711856/