我想做一个短信安排应用程序,在预定义的时间发送短信。我决定为此使用计时器。在我的研究过程中,我发现Alarm Manager更适合在Android中安排一次事件。任何指导都是有益的。短信安排在Android中
我要实现我的服务计时器如图所示给予代码:
公共类SMSTimerService延伸服务{
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");
}
}
到目前为止你做了什么? – 2012-02-13 06:21:04
@Seshu Vinay - 到目前为止,我已经拿起联系电话号码并保存了信息。我已经设置了短信应该发送的延迟时间。我的主要问题是我不知道如何执行调度。我很困惑定时器,报警管理器和计数器。 - – Prativa 2012-02-13 07:31:26