Tokuchi Toua Tokuchi Toua - 25 days ago 12
Android Question

ring an alarm even in silent mode when sms keyword is received for android

What does alarmmanager really do? Is it the one that really plays the sound or just some kind of trigger? I'm trying to make an application that will play an alarm even in silent mode when received a sms with a certain keyword, then display an alert dialog that will provide the user with buttons to press to reply to the message. I've been studying and finding working examples online for about a week but none of them even play the alarm. plus, the examples are always different and confusing that just gets me more frustrated. I'm a newbie in android app dev. please help me.. thanks in advance. here is my 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<pdusObj.length; i++)
{
messages[i] = SmsMessage.createFromPdu ((byte[])
pdusObj[i]);
sender = messages[i].getOriginatingAddress();
}

for (SmsMessage msg : messages) {
if (msg.getMessageBody().contains("alert")) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 5);
Intent i = new Intent(context, ReceiverInterface.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,
12345, i, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager)context.getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
pendingIntent);
}//end if
}//end for

}// end onreceive
}


I'm sorry if the code was really messy

public class ReceiverInterface extends Activity{
final Context context = this;
String my_password = "1234";
AlertDialog.Builder alertDialogBuilder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.receiverinterface);

alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder.setTitle("Emergency signal received");

alertDialogBuilder
.setMessage("Click availability status")
.setCancelable(false)
.setPositiveButton("available",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dispatch_available_action();
Toast.makeText(getApplicationContext(), "available", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("not available",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// show it
alertDialog.show();

}// end oncreate()


public void dispatch_available_action(){
final EditText password_input = new EditText(this); // create an text input field
password_input.setHint("Enter Password"); // put a hint in it
password_input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); // change it to password type

alertDialogBuilder = new AlertDialog.Builder(
context);
AlertDialog.Builder alertDialog = new Builder(this); // create an alert box
alertDialog.setTitle("Enter Password"); // set the title
alertDialog.setView(password_input); // insert the password text field in the alert box
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { // define the 'OK' button
public void onClick(DialogInterface dialog, int which) {
String entered_password = password_input.getText().toString();
if (entered_password.equals(my_password)) {
alertDialogBuilder.setTitle("Send status");

alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {

try {

String message = "available";
SmsManager smsManager = SmsManager.getDefault();
EAlarmReceiver eReceiver = new EAlarmReceiver();
String eSender = eReceiver.sender;
Toast.makeText(getApplicationContext(), eSender,
Toast.LENGTH_LONG).show();
smsManager.sendTextMessage(eSender, null, message, null, null);

} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS failed, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// show it
alertDialog.show();

} else {
Toast.makeText(getApplicationContext(),
"Wrong password!",
Toast.LENGTH_LONG).show();
}
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { // define the 'Cancel' button
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show(); // show the alert box
}
}


The alertdialog is working fine, I just can't get the alarm to ring, let alone do it while in silent mode.

Answer

All AlarmManager does is execute a PendingIntent at a given time, with an optional repeat. It has nothing to do with audio streams. This howto will probably help you play a sound when your PendingIntent is received.

Comments