报警管理器到底是做什么的?它是真正播放声音的那个还是只是某种触发器?我正在尝试制作一个应用程序,当收到带有特定关键字的短信时,即使在静音模式下也会播放警报,然后显示一个警报对话框,该对话框将为用户提供按下按钮以回复消息。我已经在网上学习和寻找工作示例大约一周了,但他们都没有发出警报。另外,这些例子总是不同的和令人困惑的,这让我更加沮丧。我是 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
}
}
警报对话框工作正常,我只是无法让警报响起,更不用说在静音模式下这样做了。