我已经开发了一项服务,在活动启动时自动启动。 但我想停止一定的时间间隔说10秒后服务,并在30秒后说一段时间后再次启动服务。 我是一个新的android编程,所以没有得到如何做到这一点,请帮助。 我正在使用广播接收器来启动服务。延迟启动服务
延迟启动服务
回答
你需要把你的任务分解成更原始的部分。然后你就可以看到你所需要的谷歌,并会得到更好的效果:)
- 使用另一个线程
- 'sleep' the thread为X毫秒的scheduler to schedule a new task。
- 使用你的意图和广播接收器
另外(高级方法)启动服务,使用警报器经理how to schedule some code execution in android or: what exactly are daemon threads in android?
我会建议使用报警管理器和发送挂起的意图来启动服务。就像这样:
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);
然后在广播接收器做到这一点:
Intent intent = new Intent(context, MyServiceService.class);
context.startService(intent);
使用wifimanager和bluetoothmanager时可以使用闹钟管理器吗? – user2661237
我不确定我是否按照你的问题。一般来说,广播接收器没有很多时间来执行。你通常会从他们开始一项服务。然后使用服务上下文,您可以获得蓝牙或WiFi管理器。 –
是否有任何理由直接使用am.getBroadcast(serviceIntent)而不是直接使用am.getService(MyServiceService),完全绕过广播? – averasko
只是写了一个实用工具,您和其他人可以使用:
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>
- 1. MSBuild - MsBuildExtensionPack启动Windows服务延迟
- 2. 服务器启动时间长延迟
- 3. Azure移动服务延迟
- 4. Rails启动延迟
- 5. 启动QPropertyAnimation延迟
- 6. 如何克服启动程序延迟?
- 7. 从服务中启动活动时的延迟
- 8. 如何将Tomcat服务安装为自动(延迟启动)?
- 9. 设置自动(延迟启动)Windows服务的时间间隔
- 10. C++中服务的自动延迟启动
- 11. 启动时延迟活动
- 12. 为什么Windows远程管理服务坚持“延迟启动”?
- 13. systemd:延迟服务的返回,直到启动完成
- 14. 启动服务时bindService()和onBind()之间的延迟
- 15. .net窗体web服务调用SLOW /启动延迟
- 16. 将服务安装为延迟自动
- 17. Jetty 8.1.2启动延迟
- 18. 石英延迟启动
- 19. Android相机启动延迟
- 20. JBoss启动时间延迟
- 21. 码头启动延迟
- 22. 方法启动延迟
- 23. 添加启动延迟
- 24. jQuery延迟启动功能
- 25. 服务器端延迟
- 26. 执行顺序的启动任务和自动延迟的Windows服务
- 27. “自动”与“自动(延迟启动)”
- 28. 功能是否可以延迟启动而不是延迟?
- 29. 活动启动延迟(使用WakeLock)
- 30. Youtube延迟自动启动视频
使用报警管理 –
可能重复[如何安排一些代码执行在Android或:?究竟是Android的守护线程(http://stackoverflow.com/questions/3883246/how- to-schedule-some-code-execution-in-android-or-what-exactly-are-daemon-threa) – Gusdor
使用TimerTask或AlarmManager – AndroUser