当收到sms关键字为android时,即使在静音模式下也会响铃

时间:2012-11-27 19:53:50

标签: android sms alarm

报警管理员真正做了什么?是真的播放声音还是某种触发器?我正在尝试制作一个应用程序,即使在收到带有特定关键字的短信时也会以静音模式发出警报,然后显示一个警告对话框,为用户提供按下以回复该消息的按钮。我一直在网上学习并找到工作实例大约一个星期,但他们都没有发出警报。此外,这些例子总是不同而且令人困惑,这让我更加沮丧。我是android app dev的新手。请帮帮我..提前谢谢。这是我的代码

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
}

如果代码真的很乱,我很抱歉

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
}
}

alertdialog工作正常,我只是无法响闹,更别说在静音模式下这样做了。

1 个答案:

答案 0 :(得分:0)

所有AlarmManager都会在给定时间执行PendingIntent,并带有可选的重复。它与音频流无关。 <{3}}可能会帮助您在收到PendingIntent时播放声音。