简体   繁体   English

如何每10分钟在后台运行一次服务?

[英]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.

相关问题 如何每1分钟在后台运行一次服务 - How to run service in background every 1 minute 如何每分钟在Android O中运行后台服务? - How to run the background service in Android O, every minute? 使后台服务在启动时以及每分钟运行 - Make Background Service run on startup as well as every minute 在后台android中每10分钟使用gps获取经度和经度 - getting latitude and longitude using gps every 10 minute in background android 每分钟发送位置的服务,即使应用程序在后台或系统重新启动 - Service to sending location every minute, even if app is in background or system is rebooted 每隔1分钟进行一次后台作业,服务后,我应该调用`stopSelf()`吗? - Background job every 1 minute, service, should I call `stopSelf()`? 每分钟如何运行BroadcastReceiver? - How to run BroadcastReceiver after every minute? 如何每 5 分钟运行 10 秒的前台服务? - How to run a foreground service for 10 seconds, every 5 minutes? 需要在android中每分钟运行一个后台任务 - Need to have one background task to run for every minute in android 每分钟运行一次的服务 - Service that runs every minute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM