[英]How to run service in background every 10 minute?
I want to use a service that run in background indefinitely and call a method every 10 minute and its running even app killed我想使用一个无限期在后台运行的服务,每 10 分钟调用一个方法,它的运行甚至应用程序被杀死
How to create it?如何创建它?
You can do by using service as follows您可以按如下方式使用服务
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//performe the deskred task
}
}, 10minutes time in milisecods);
// If we get killed, after returning from here, restart
return START_STICKY;
}
This service will get started automatically even if app get killed, and postdelayed will run即使应用程序被杀死,此服务也会自动启动,并且 postdelayed 将运行
For "how to work with services", see有关“如何使用服务”,请参阅
Services - Android 服务 - 安卓
Services in Android - Vogella Android 中的服务 - Vogella
Here is a clear solution that focus on "every 10 minutes" part using AlarmManager: https://stackoverflow.com/a/10222390/2591556这是一个明确的解决方案,使用 AlarmManager 专注于“每 10 分钟”部分: https ://stackoverflow.com/a/10222390/2591556
Assuming you have a Running Service假设你有一个正在运行的服务
User AlarmManager to run Service every 10 minutes用户 AlarmManager 每 10 分钟运行一次 Service
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, YourService.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 600000, pi); // Millisec * Second * Minute
}
you could write a background Service: Running in a Background Service你可以写一个后台服务: 在后台服务中运行
and start the service every 10-11 min (cause of AlarmManager power saving behaviour), or with exact timing (needs to shedule next execution every time) with AlarmManager.setExact并每 10-11 分钟启动一次服务(AlarmManager 节能行为的原因),或者使用 AlarmManager.setExact 以准确的时间(每次都需要调度下一次执行) 启动服务
Example:例子:
private static PendingIntent createClockIntent(Context context) {
Intent intent = new Intent(context.getString(R.string.widget_broadcast_clock_update));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 1,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}
public static void startClockAlarm(Context context) {
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
clockIntent = createClockIntent(context);
alarmManager.setRepeating(AlarmManager.RTC, 0,
600000, clockIntent);
}
You can use the Alarm manager which will be called after every 10 mins您可以使用每隔 10 分钟调用一次的警报管理器
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
notificationIntent.setClass(this,AlarmReceiver_.class);
notificationIntent.addCategory("android.intent.category.DEFAULT");
PendingIntent broadcast = PendingIntent.getBroadcast(YourClass.this, m, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 300000L,
600000L, broadcast);
ManiFest receiver Code where you will get a receiver response ManiFest 接收器代码,您将在其中获得接收器响应
<receiver android:name="com.yourpackage.AlarmReceiver_"
>
<intent-filter>
<action android:name="android.media.action.DISPLAY_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.REBOOT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
You will have to create Receiver where you will receive data as an above-specified name of AlarmReceiver_.class您将必须创建接收器,您将在其中接收数据作为上面指定的 AlarmReceiver_.class 名称
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.