Планирование смс в андроиде
Я хочу создать приложение для планирования SMS, которое отправляет SMS в заранее установленное время. Я решил использовать таймер для этой цели. Во время моего исследования я обнаружил, что Alarm Manager был более подходящим вариантом для планирования одноразовых событий в 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");
}
}
1 ответ
Это то, что я имею для вас:
http://mobile.tutsplus.com/tutorials/android/android-fundamentals-scheduling-recurring-tasks/
Редактировать: Как запустить трансляцию через 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 будет означать одну секунду.