在Android的短信调度 [英] sms scheduling in android
问题描述
我想打一个短信调度应用程序,在那个predefined时间发送短信。我决定用一个定时器用于这一目的。在我的研究,我发现,告警管理是调度Android的一次事件更合适的选择。任何指导将是富有成果的。
I want to make an SMS scheduling app, that sends SMS at predefined time. I have decided to use a timer for that purpose. During my research, I found out that Alarm Manager was more appropriate option for scheduling one time events in android. Any guidance would be fruitful.
我要实现我的服务计时器如图所示给予code:
I want to implement the timer in my service as shown in the give code:
公共类SMSTimerService延伸服务{
public class SMSTimerService extends Service {
private Timer timer = new Timer();
Long delay = 10000L;//for long we have to keep L at the last of the integer;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;//null means we are not using any IPC here
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.i("prativa","service has started");
startService();
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("prativa","service is destroying");
shutdownService();
}
/*
* starting the service
* */
private void startService()
{
TimerTask task = new TimerTask(){
@Override
public void run() {
sendSMS();
}};
timer.schedule(task, delay);
}
private void sendSMS()
{
String phone = "5556";
String message = "This is my test message";
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phone, null, message, null, null);
}
private void shutdownService()
{
if(timer != null)
timer.cancel();
Log.i("Prativa","Timer has stopped");
}
}
推荐答案
这是我对你的:
的http://mobile.tutsplus.com/tutorials/android/android-fundamentals-scheduling-recurring-tasks/
编辑:如何通过AlarmManager触发广播:
How to trigger a broadcast via the AlarmManager:
Intent broadCastIntent = new Intent(this, "YOURBROADCASTRECEIVER.class");
PendingIntent intent = PendingIntent pendingIntent = PendingIntent.getBroadcast(
this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(),
AlarmManager.INTERVAL_HOUR, pendingIntent);
请注意,此报警将掀起立即在第一时间。如果你想设置的以后你可以乘System.currentTimeMillis的()* X,其中x = 1000将意味着一秒。
Note that this alarm will set off immediately the first time. If you want to set it of later you can multiply "System.currentTimeMillis() * x" where x = 1000 would mean one second.
这篇关于在Android的短信调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!