我正在开发一个Android应用程序,它可以获取我的应用程序数据库的存储日期,并检查此日期与当前日期,如果匹配,然后自动发送一些短信给该人。但我不知道如何做这个。请帮助。在数据库中存在一个表,其中包含一些实体,如人名,手机号,日期,消息。帮助!!
答案 0 :(得分:0)
您可以使用PendingIntent和AlarmManager来实现此目的。看看吧。
首先初始化alarmManager和PendingIntent。
intentsOpen = new Intent(this, AlarmReceiver.class);
intentsOpen.setAction("com.sandeep.alarm.ACTION");
pendingIntent = PendingIntent.getBroadcast(this,111, intentsOpen, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
设置您要发送短信的日期和时间。
//set what you want only. I am giving all
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, hours);
calendar.set(Calendar.MINUTE, minutes);
// set action
intentsOpen.setAction("com.sandeep.sendsms.ACTION");
pendingIntent = PendingIntent.getBroadcast(this,112, intentsOpen, 0);
if((calendar.getTimeInMillis() > System.currentTimeMillis())){
// Always use this if condition so your receiver will not trigger when you start app. if triggerAtMillis is older than the currentMillis, the alarm will be fired no matter when you set it
// to send sms on repetitive basis
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pendingIntent);
// 24*60*60*1000 is a duration means how much day you want to repeat.
// to send sms once only
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), pendingIntent)
}
这是警报接收器类。
public class AlarmReceiver extends BroadcastReceiver {
private final String SEND_SMS = "com.sandeep.sendsms.ACTION";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (SEND_SMS .equals(action)) {
// HERE you can code to send sms.
}
}
}