stop alarm from ringing in another activity - android

柔情痞子 提交于 2019-12-23 03:52:07

问题


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()

回答1:


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();


来源:https://stackoverflow.com/questions/13711856/stop-alarm-from-ringing-in-another-activity-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!