从另一项活动振铃停止报警器 - 机器人 [英] stop alarm from ringing in another activity - android
问题描述
谁能教我如何从我的code振铃停止报警。就是我从另一个活动停止报警的正确方法吗?因为,截至目前,警报只是不断即使我已经pressed的释放按钮振铃。
alertDialogBuilder.setTitle(报警); alertDialogBuilder
.setMessage(停止报警)
.setCancelable(假)
.setPositiveButton(辞退,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释的id){
eReceiver =新EAlarmReceiver();
铃声R = eReceiver.ringAlarm(背景);
r.stop();
Toast.makeText(context.getApplicationContext(),报警停止,Toast.LENGTH_LONG).show();
意图openInterface =新意图(proj.receiver.RECEIVERINTERFACE);
openInterface.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(openInterface);
}
});
//创建警报对话框
AlertDialog alertDialog = alertDialogBuilder.create();
// 展示下
alertDialog.show();
} //结束的OnCreate()
下面是我启动报警
公共无效的onReceive(上下文的背景下,意图意图){
// TODO自动生成方法存根
捆绑包= intent.getExtras();
[对象] pdusObj =(对象[])bundle.get(的PDU);
SmsMessage [] =邮件新SmsMessage [pdusObj.length]
的for(int i = 0; I< pdusObj.length;我++)
{
消息[I] = SmsMessage.createFromPdu((字节[])
pdusObj [I]);
发送邮件= [I] .getOriginatingAddress();
} 对于(SmsMessage消息:消息){
如果(msg.getMessageBody()。包含(firealert)){
ringAlarm(上下文);
意图openStopAlarm =新意图(proj.receiver.STOPALARM);
openStopAlarm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(openStopAlarm); }//万一
} //结束了
} //结束的onReceive公共铃声ringAlarm(上下文的背景下)
{
乌里警报= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
如果(警告== NULL){
//警报为null,使用备份
警报= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
如果(警告== NULL){//我看不出这个不断被空(总是有一个默认的通知),但只是柜面
//警报备份为空,用第二个备份
警报= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
铃声R = RingtoneManager.getRingtone(context.getApplicationContext(),警告);
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(),报警开始,Toast.LENGTH_LONG).show();
返回ř;
}
//结束ringAlarm()
这在的BroadcastReceiver声明的铃声 - [R
价值是不一样的,你在定义的你的活动,你必须做,使R参数有更多的范围,在应用程序中的内容:
您可以定义的R参数为
静态铃声;
在你的广播接收器,然后在的onReceive()方法将值分配给它,然后调用该变量从您的活动刚刚宣布新的R变量波纹管:
铃声R = yourpackge.Your_BroadcastReceiver_Class.r
然后调用
r.stop();
Can someone teach me on how to stop the alarm from ringing in my code. Is what I have the right way of stopping the alarm from another activity? Because, as of now, the alarm just keeps on ringing even after I've pressed the dismiss button.
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()
Here is where I start the alarm
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()
the Ringtone r
value that declared in the BroadcastReceiver is not the same as the one that you defined in your Activity, you have to do the something that make the r variable has more scope in your application:
you can defined the r variable as
static Ringtone;
in your broadcastreceiver and then assign value to it in onReceive() method, then to call that variable from your activity just declare new r variable as bellow:
Ringtone r=yourpackge.Your_BroadcastReceiver_Class.r
then call
r.stop();
这篇关于从另一项活动振铃停止报警器 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!