【问题标题】:How to run service in background every 10 minute?如何每 10 分钟在后台运行一次服务?
【发布时间】:2015-01-21 08:15:48
【问题描述】:

我想使用无限期在后台运行的服务并每 10 分钟调用一次方法 并且它正在运行的应用程序甚至被杀死

如何创建?

【问题讨论】:

标签: android


【解决方案1】:

您可以通过以下方式使用服务

@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;
  }

即使app被杀,该服务也会自动启动,并且postdelayed会运行

【讨论】:

  • 在 Service 中使用 Handler 安全吗?
  • 它不会使用此代码每 10 分钟运行一次代码。它只会在 10 分钟内运行一次代码。
【解决方案2】:

有关“如何使用服务”,请参阅
Services - Android
Services in Android - Vogella

这是一个清晰的解决方案,专注于使用 AlarmManager 的“每 10 分钟”部分:https://stackoverflow.com/a/10222390/2591556

【讨论】:

    【解决方案3】:

    假设你有一个正在运行的服务

    用户 AlarmManager 每 10 分钟运行一次服务

       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
         }
    

    【讨论】:

    • RTC_WAKEUP 不适用于Service。您必须使用 WakefulBroadcastReceiver 来执行此操作
    • 请解释一下,它是用于通过警报管理器启动挂起的意图,并且已经过验证并且可以正常工作。但距离最初在 Android 上发布许多东西已经过去了 2 年。
    【解决方案4】:

    你可以写一个后台服务: Running in a Background Service

    每 10-11 分钟启动一次服务(AlarmManager 省电行为的原因),或者使用 AlarmManager.setExact 以准确的时间(每次都需要安排下一次执行)启动服务

    例子:

    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);
        }
    

    【讨论】:

      【解决方案5】:

      您可以使用每隔 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 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>
      

      您必须创建接收器,您将在其中接收数据作为上述指定名称的 AlarmReceiver_.class

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-04
        • 2023-03-16
        • 1970-01-01
        • 2020-03-06
        • 2023-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多