我正在开发一个需要向特定电话号码发送短信的应用程序。我可以使用以下代码发送短信。
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();
}
}
以防万一有人需要这个...谢谢