我已經開發了一項服務,在活動啓動時自動啓動。 但我想停止一定的時間間隔說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