Note that there are some explanatory texts on larger screens.

plurals
  1. POtrigger an alarm sms for android
    text
    copied!<p>sir, i want to trigger an alarm when the user receives a sms containing a particular keyword, then provide the user with an alertdialog to reply back with a predefined message. in my code, i can't seem to get the alarm to work. it just displays the alertdialog. also, if possible, i want to use the default alarm of the phone and set it off even in silent mode. i want to replace the sms received tone with an alarm. please help me. thanks in advance. </p> <pre><code>public class EAlarmReceiver extends BroadcastReceiver { public static String sender; 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&lt;pdusObj.length; i++) { messages[i] = SmsMessage.createFromPdu ((byte[]) pdusObj[i]); sender = messages[i].getOriginatingAddress(); } for (SmsMessage msg : messages) { if (msg.getMessageBody().contains("firealert")) { Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); v.vibrate(300); Uri alert = RingtoneManager .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); MediaPlayer mMediaPlayer = new MediaPlayer(); try { mMediaPlayer.setDataSource(context, alert); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM); audioManager.setStreamVolume(AudioManager.STREAM_ALARM, maxVolume,AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) { mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); mMediaPlayer.setLooping(false); //mMediaPlayer.prepare(); mMediaPlayer.start(); Toast.makeText(context.getApplicationContext(), "alarm started", Toast.LENGTH_LONG).show(); }//end if Intent openInterface = new Intent("proj.receiver.RECEIVERINTERFACE"); openInterface.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(openInterface); }//end if }//end for }// end onreceive </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload