在一段时间后重复运行一个方法的服务 [英] Service that repeatedly runs a method, after an amount of time
问题描述
我想创建一个服务,它会在 10 秒后一遍又一遍地运行一个方法,直到我停止它,即使应用程序已关闭.我尝试的代码如下
I want to create a service, that runs a method after 10 seconds over and over again, until I stop it, even if the app is closed. My attempted code is below
package com.example.tauben;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class Reminder extends Service {
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
TimerTask task = new TimerTask() {
public void run(){
f();
}
};
Timer timer = new Timer();
timer.scheduleAtFixedRate(task, 0, 10000);
}
public void f(){
Toast t = Toast.makeText(this, "Service is still running", Toast.LENGTH_SHORT);
t.show();
}
}
这就是我启动服务的方式.
And this is how I start the service.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(Log_TAG,"_____________RESTART__________");
Intent i= new Intent(this, Reminder.class);
startService(i);
}
程序只是立即停止运行;如何更改我的代码以获得所需的行为?
The program just stops running immediately; how can I change my code to get the desired behaviour?
推荐答案
创建一个广播接收器,它会在收到来自 AlarmManager
的广播后启动你的服务:
create a broadcast receiver that will start your service after receiving broadcast from AlarmManager
:
public class SyncAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent data) {
// set alarm to start service
Calendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(System.currentTimeMillis());
Calendar cal = new GregorianCalendar();
cal.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
// start service after an hour
cal.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) + 60);
cal.set(Calendar.SECOND, calendar.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, calendar.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, calendar.get(Calendar.DATE));
cal.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
// set alarm to start service again after receiving broadcast
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, SyncAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
am.cancel(pendingIntent);
am.set(AlarmManager.RTC, cal.getTimeInMillis(), pendingIntent);
intent = new Intent(context, Reminder.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(intent);
}
}
在您启动应用程序时第一次启动您的广播接收器:
start your broadcast receiver for the first time when you start your application:
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, SyncAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
am.cancel(pendingIntent);
am.set(AlarmManager.RTC, System.currentTimeMillis(), pendingIntent);
还要将此添加到您的清单文件中:
Also add this to your Manifest File:
<receiver android:name=".SyncAlarm" >
</receiver>
你也应该看看这个 START_STICKY 如果你希望您的服务在资源可用时由 Android 系统启动,而不是通过 AlarmManager 接收广播.
You should also have alook at this START_STICKY if you want your service to be started by Android system as soon as resources are available instead of receiving Broadcast by AlarmManager.
这篇关于在一段时间后重复运行一个方法的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!