在这个应用程序中,我在EditText
一)3
b) 5554
c) iOS
当您单击“发送警报”时,它将每 1 分钟发送一次短信。现在我取消了警报,我再次启动这个应用程序,插入新值如下:
一)3
b) 12345
c) 安卓
再次启动警报,这次它没有采用新值。相反,它向包含文本“iOS”的“5554”发送短信。我想知道如何更新新值。我有两节课。1) MainActivity 2) 接收者
代码:
public class MyyActivity extends Activity {
EditText et, et1, et2;
Button b, b1;
AlarmManager alarmManager;
String no,text,ala;
PendingIntent pi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et = (EditText) findViewById(R.id.editTextEnter);
et1 = (EditText) findViewById(R.id.editTextNUMBER);
et2 = (EditText) findViewById(R.id.editTextTEXT);
b = (Button) findViewById(R.id.buttonSendAlarm);
b1 = (Button) findViewById(R.id.Cancel);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
no = et1.getText().toString();
text = et2.getText().toString();
ala = et.getText().toString();
int foo = Integer.parseInt(ala);
Intent myIntent = new Intent(MyyActivity.this, MyRecvr.class);
Bundle bundle = new Bundle();
bundle.putCharSequence("no", no);
bundle.putCharSequence("text", text);
myIntent.putExtras(bundle);
pi = PendingIntent.getBroadcast(getApplicationContext(), 0,
myIntent, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, foo);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*1,pi);
Toast.makeText(
getApplicationContext(),
"Start Alarm with \n" + "smsNumber = " + no + "\n"
+ "smsText = " + text, Toast.LENGTH_SHORT)
.show();
et.setText("");
et1.setText("");
et2.setText("");
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(), MyRecvr.class);
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(),
0, intent, 0);
alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(sender);
Toast.makeText(MyyActivity.this, "Cancel!", Toast.LENGTH_LONG)
.show();
finish();
}
});
}
和接收类:
public class MyRecvr extends BroadcastReceiver {
@SuppressWarnings("deprecation")
SmsManager sms;
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
String smsNumbr= (String) bundle.getCharSequence("no");
String smsText = (String) bundle.getCharSequence("text");
Toast.makeText(context, "MyAlarmService.onStart()", Toast.LENGTH_LONG)
.show();
Toast.makeText(
context,
"MyAlarmService.onStart() with \n" + "smsNumberToSend = "
+ smsNumbr + "\n" + "smsTextToSend = "
+ smsText, Toast.LENGTH_LONG).show();
sms = SmsManager.getDefault();
sms.sendTextMessage(smsNumbr, null, smsText, null, null);
}