【问题标题】:Start Service after a delay延迟后启动服务
【发布时间】:2018-02-16 18:24:36
【问题描述】:

我已经开发了一个服务,该服务在活动启动时自行启动。 但我想在一定的时间间隔后停止服务,比如 10 秒,然后在一段时间后再次启动服务,比如 30 秒后。 我对android编程有点陌生,所以不知道怎么做,请帮忙。 我正在使用广播接收器来启动服务。

【问题讨论】:

标签: android


【解决方案1】:

我建议使用警报管理器并发送待定意图来启动服务。很像这样:

AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent serviceIntent = new Intent(context, ServiceReceiver.class); 
PendingIntent pi = PendingIntent.getBroadcast(context, ServiceIdsConstants.SERVICE_ID,     serviceIntent , PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 30000, pi);

然后在 BroadcastReceiver 中这样做:

Intent intent = new Intent(context, MyServiceService.class);
context.startService(intent);

【讨论】:

  • 在使用wifimanager和bluetoothmanager时可以使用报警管理器吗?
  • 我不确定我是否遵循您的问题。一般来说,广播接收器没有太多时间来执行。您通常会从他们那里启动服务。然后使用服务上下文,您可以获得蓝牙或 wifi 管理器。
  • 这里有什么理由使用am.getBroadcast(serviceIntent) 而不是直接使用am.getService(MyServiceService),完全绕过广播?
  • 我相信我推荐了警报管理器,因为该用户希望在未来的特定时间重新启动服务。
  • fwiw,对于查看此示例的任何人,请注意 am.set() 的 30000 参数并不意味着“在 30 秒内”,它的意思是“在挂钟时间戳 30000”,即是在 1970 年 12 月的某个时候,所以会立即开火。您可以通过 System.currentTimeMillis() + 30000 获取“未来 30 秒”作为此参数。如果时钟被重置,可能还值得使用 ELAPSED_REALTIME 和 SystemClock.elapsedRealtime() + 30000 而不是 System.currentTimeMillis()。
【解决方案2】:

你需要把你的任务分解成比这更原始的部分。然后你可以看到你需要谷歌搜索并得到更好的结果:)

  1. 在另一个线程上使用scheduler to schedule a new task
  2. 'sleep' the thread 持续 X 毫秒。
  3. 使用您的意图和广播接收器启动您的服务

另外(高级方法),使用报警管理器how to schedule some code execution in android or: what exactly are daemon threads in android?

【讨论】:

    【解决方案3】:

    刚刚写了一个你和其他人可以使用的实用程序:

    public static void startDelayedWakefulService(Context context,long delayInMillis,Class<? extends Service> serviceClass) {
        AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent serviceIntent = new Intent(context, DelayedStartServiceBroadcastReceiver.class);
        serviceIntent.putExtra("className",serviceClass.getName());
        PendingIntent pi= PendingIntent.getBroadcast(context, 7, serviceIntent , PendingIntent.FLAG_UPDATE_CURRENT);
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeInMillis()+delayInMillis, pi);
    }
    

    public class DelayedStartServiceBroadcastReceiver extends WakefulBroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            String className = intent.getStringExtra("className");
            try {
                startWakefulService(context,new Intent(context,Class.forName(className)) );
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                completeWakefulIntent(intent);
            }
        }
    }
    

    别忘了将它添加到您的清单中

        <receiver
            android:name=".utils.DelayedStartServiceBroadcastReceiver"
            android:enabled="true"
            android:exported="true" >
        </receiver>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-07
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      • 2021-05-24
      • 1970-01-01
      相关资源
      最近更新 更多