我们在用service的时候 有一个类IntentService 用法如下:
Creates a default worker thread that executes all intents delivered to onStartCommand() separate from your application's main thread.
Creates a work queue that passes one intent at a time to your onHandleIntent() implementation, so you never have to worry about multi-threading.
Stops the service after all start requests have been handled, so you never have to call stopSelf().
Provides default implementation of onBind() that returns null.
Provides a default implementation of onStartCommand() that sends the intent to the work queue and then to your onHandleIntent() implementation.
总之:a constructor and an implementation of onHandleIntent(). 即实现构造方法和实现onHandleIntent()即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public class HelloIntentService extends IntentService { /** * A constructor is required, and must call the super IntentService(String) * constructor with a name for the worker thread. */ public HelloIntentService() { super ( "HelloIntentService" ); } /** * The IntentService calls this method from the default worker thread with * the intent that started the service. When this method returns, IntentService * stops the service, as appropriate. */ @Override protected void onHandleIntent(Intent intent) { // Normally we would do some work here, like download a file. // For our sample, we just sleep for 5 seconds. long endTime = System.currentTimeMillis() + 5 * 1000 ; while (System.currentTimeMillis() < endTime) { synchronized ( this ) { try { wait(endTime - System.currentTimeMillis()); } catch (Exception e) { } } } } }如果我们要重写一些方法such as onCreate(),onStartCommand(), or onDestr(), be sure to call the super implementation
我们必须返回父类的方法 super()
来自为知笔记(Wiz)
转载于:https://www.cnblogs.com/feelbest/p/3696218.html
相关资源:数据结构—成绩单生成器