先生,我如何提示停止警报响起然后在其他班级启动警报对话框?此代码总是强制关闭。如果我删除停止时钟的提示部分,闹钟将不会停止响铃,并且警报对话框仅显示一次。如果它收到另一条消息,它不会再次触发警报对话框。请帮我。提前感谢您的帮助
for (SmsMessage msg : messages) {
if (msg.getMessageBody().contains("alert")) {
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if(alert == null){
// alert is null, using backup
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if(alert == null){
// 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);
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, maxVolumeAlarm,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
r.play();
//prompt
alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder.setTitle("Alarm Received");
alertDialogBuilder
.setMessage("Stop alarm")
.setCancelable(false)
.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
r.stop();
Toast.makeText(getApplicationContext(), "alarm stopped", Toast.LENGTH_LONG).show();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
Toast.makeText(context.getApplicationContext(), "alarm started", Toast.LENGTH_LONG).show();
//open alertdialog in other activity
Intent openInterface = new Intent("proj.receiver.RECEIVERINTERFACE");
openInterface.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(openInterface);
}//end if
}//end for