alarmmanagerは実際に何をしますか?それは本当に音を再生するものですか、それともある種のトリガーですか?特定のキーワードのSMSを受信したときにサイレントモードでもアラームを再生するアプリケーションを作成し、メッセージに返信するために押すボタンをユーザーに提供するアラートダイアログを表示しようとしています。私は約1週間オンラインで実用的な例を研究して見つけてきましたが、どれも警告を発していません。さらに、例は常に異なり、混乱を招くため、私はさらにイライラします。私はAndroidアプリ開発の初心者です。私を助けてください..事前に感謝します。これが私のコードです
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は正常に機能しています。サイレントモードでは、アラームを鳴らすことはできません。