我正在开发一个应用程序,我需要将短信发送到特定的电话号码.我可以使用以下代码发送短信.
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
现在我想要的是短信应该自动进行.消息应该自动进入的时间存储在MySQL数据库中.因此,我需要在该时间到来时继续检查的代码,然后自动将消息发送到该号码.它是一种提醒事物.用户将在应用程序中保留提醒,例如;我需要在1小时后收到消息.所以1小时后消息应该会出现. PLZ帮忙?
终于我明白了.
/** Code For reminder is here */
int time=Integer.parseInt(answer);
int num = (int)System.currentTimeMillis();
Intent intent = new Intent(getApplication(), MyBroadcastReceiver.class);
intent.putExtra("phoneNo",phoneNo);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MINUTE, time);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), num, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ calendar.getTimeInMillis() , pendingIntent);
Toast.makeText(getApplication(), "Alarm set in " + time + " minutes",
Toast.LENGTH_SHORT).show();
/** Code for reminder is over */
我的收件人代码是
public void onReceive(Context context, Intent intent) {
String sms= "Your turn is about to come. Please be ready. Thank You";
String phoneNo;
Bundle extrasBundle = intent.getExtras();
phoneNo=extrasBundle.getString("phoneNo");
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
Toast.makeText(context, "SMS Sent to " + phoneNo,Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(context,"SMS faild, please try again later!",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
万一有人可能需要这个……谢谢