IntentService简介

本文详细介绍了Android中的IntentService组件,它是Service的一个子类,专门用于处理异步请求任务。客户端可以通过startService(Intent)方法将请求传递给IntentService,后者在独立线程中处理这些请求,避免阻塞主线程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  IntentService 是Service类的子类,用来处理异步请求。客户端可以通过startService(Intent)方法传递请求给 IntentService IntentService onCreate() 函数中通过 HandlerThread 单独开启一个线程来处理所有Intent请求对象(通过startService的方式发送过来的)所对应的任务,这样以免事务处理阻塞主线程。 执行完所一个Intent请求对象所对应的工作之后,如果没有新的Intent请求达到,则自动停止Service;否则执行下一个Intent请求所对应的任务
   IntentService在处理事务时,还是采用的Handler方式,创建一个名叫 ServiceHandler 的内部Handler,并把它直接绑定到 HandlerThread所对应的子线程。  ServiceHandler把处理 一个intent所对应的事务都封装到叫做 onHandleInten t的虚函数;因此我们直接实现 虚函数 onHandleIntent ,再在里面根据Intent的不同进行不同的事务处理就可以了。
另外, IntentService默认实现了Onbind()方法,返回值为null。
  使用IntentService需要两个步骤:
   1 、写构造函数
   2 实现 虚函数 onHandleIntent ,并在里面根据Intent的不同进行不同的事务处理就可以了。
好处:处理异步请求的时候可以减少写代码的工作量,比较轻松地实现项目的需求
注意IntentService的构造函数一定是参数为空的构造函数,然后再在其中调用 super("name")这种形式的构造函数。
因为Service的实例化是系统来完成的,而且系统是用 参数为空的 构造函数 实例化 Service的
关于Handler和Service的更多知识请阅读《 Looper和Handler 》,《 关于Handler技术 》,《 Service简介 》,《 AIDL和Service实现两进程通信
Public Constructors
IntentService( String name)
Creates an IntentService.
Public Methods
IBinder onBind( Intent intent)
Unless you provide binding for your service, you don't need to implement this method, because the default implementation returns null.
void onCreate()
Called by the system when the service is first created.
void onDestroy()
Called by the system to notify a Service that it is no longer used and is being removed.
void onStart( Intent intent, int startId)
This method is deprecated. Implement onStartCommand(Intent, int, int) instead.
int onStartCommand( Intent intent, int flags, int startId)
You should not override this method for your IntentService.
void setIntentRedelivery(boolean enabled)
Sets intent redelivery preferences.

If enabled is true, onStartCommand(Intent, int, int) will return START_REDELIVER_INTENT, so if this process dies before onHandleIntent(Intent)returns, the process will be restarted and the intent redelivered. If multiple Intents have been sent, only the most recent one is guaranteed to be redelivered.

If enabled is false (the default), onStartCommand(Intent, int, int) will return START_NOT_STICKY, and if the process dies, the Intent dies along with it.

设置为true时, onStartCommand 返回 START_REDELIVER_INTENT,否则返回 START_NOT_STICKY
关于此的更多内容请参考《 Service简介
Protected Methods
abstract void onHandleIntent( Intent intent)
This method is invoked on the worker thread with a request to process.
This method is invoked on the worker thread with a request to process. Only one Intent is processed at a time, but the processing happens on a worker thread that runs independently from other application logic. So, if this code takes a long time, it will hold up other requests to the same IntentService, but it will not hold up anything else. When all requests have been handled, the IntentService stops itself, so you should not call  stopSelf() .
该函数用于针对Intent的不同进行不同的事务处理就可以了.执行完所一个Intent请求对象所对应的工作之后,如果没有新的Intent请求达到,
则自动停止Service;否则 ServiceHandler会取得 下一个Intent请求传人该函数来处理其所对应的任务。


实例1
MyIntentService.java文件
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p><div><span class="kwd" style="color: rgb(0, 0, 136);">package</span><span class="pln"> com</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">lenovo</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">robin</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">test</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></div><div><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln"> android</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">app</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">IntentService</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></div><div><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln"> android</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">content</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">Intent</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></div><div><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln"> android</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">util</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">Log</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></div><div>
</div><div><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">class</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">MyIntentService</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">extends</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">IntentService</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span></div><div><span class="kwd" style="color: rgb(0, 0, 136);">final</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">static</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">String</span><span class="pln"> TAG</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="str" style="color: rgb(0, 136, 0);">"robin"</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></div><div><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">MyIntentService</span><span class="pun" style="color: rgb(102, 102, 0);">()</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span></div><div><span class="pln">  </span><span class="kwd" style="color: rgb(0, 0, 136);">super</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"com.lenovo.robin.test.MyIntentService"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></div><div><span class="pln">  </span><span class="typ" style="color: rgb(102, 0, 102);">Log</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">i</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln">TAG</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="kwd" style="color: rgb(0, 0, 136);">this</span><span class="pun" style="color: rgb(102, 102, 0);">+</span><span class="str" style="color: rgb(0, 136, 0);">" is constructed"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></div><div><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">}</span></div><div><span class="pln"> </span><span class="lit" style="color: rgb(0, 102, 102);">@Override</span></div><div><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">protected</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln"> onHandleIntent</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Intent</span><span class="pln"> arg0</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span></div><div><span class="pln">  </span><span class="typ" style="color: rgb(102, 0, 102);">Log</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">i</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln">TAG</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="str" style="color: rgb(0, 136, 0);">"begin onHandleIntent() in "</span><span class="pun" style="color: rgb(102, 102, 0);">+</span><span class="kwd" style="color: rgb(0, 0, 136);">this</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></div><div><span class="pln">  </span><span class="kwd" style="color: rgb(0, 0, 136);">try</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span></div><div><span class="pln">   </span><span class="typ" style="color: rgb(102, 0, 102);">Thread</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">sleep</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">10</span><span class="pun" style="color: rgb(102, 102, 0);">*</span><span class="lit" style="color: rgb(0, 102, 102);">1000</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></div><div><span class="pln">  </span><span class="pun" style="color: rgb(102, 102, 0);">}</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">catch</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">InterruptedException</span><span class="pln"> e</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span></div><div><span class="pln">  </span><span class="pln">   e</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">printStackTrace</span><span class="pun" style="color: rgb(102, 102, 0);">();</span></div><div><span class="pln">  </span><span class="pun" style="color: rgb(102, 102, 0);">}</span></div><div><span class="pln">  </span><span class="typ" style="color: rgb(102, 0, 102);">Log</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">i</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln">TAG</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="str" style="color: rgb(0, 136, 0);">"end onHandleIntent() in "</span><span class="pun" style="color: rgb(102, 102, 0);">+</span><span class="kwd" style="color: rgb(0, 0, 136);">this</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></div><div><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">}</span></div><div><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln"> onDestroy</span><span class="pun" style="color: rgb(102, 102, 0);">()</span></div><div><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span></div><div><span class="pln">  </span><span class="kwd" style="color: rgb(0, 0, 136);">super</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">onDestroy</span><span class="pun" style="color: rgb(102, 102, 0);">();</span></div><div><span class="pln">  </span><span class="typ" style="color: rgb(102, 0, 102);">Log</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">i</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln">TAG</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="kwd" style="color: rgb(0, 0, 136);">this</span><span class="pun" style="color: rgb(102, 102, 0);">+</span><span class="str" style="color: rgb(0, 136, 0);">" is destroy"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></div><div><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">}</span></div><div><span class="pun" style="color: rgb(102, 102, 0);">}</span></div><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p>
启动 MyIntentServic的代码片段
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p><div><span class="pln">   </span><span class="typ" style="color: rgb(102, 0, 102);">Intent</span><span class="pln"> intent</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Intent</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">this</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="typ" style="color: rgb(102, 0, 102);">MyIntentService</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="kwd" style="color: rgb(0, 0, 136);">class</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></div><div><span class="pln">   </span><span class="pln">startService</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln">intent</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></div><div><span class="pln">   </span><span class="pln">startService</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln">intent</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></div><div><span class="pln">   </span><span class="pln">startService</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln">intent</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></div><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p>
AndroidManifest.xml文件代码片段
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="tag" style="color: rgb(0, 0, 136);"><service</span><span class="pln"> </span><span class="atn" style="color: rgb(102, 0, 102);">android:name</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="atv" style="color: rgb(0, 136, 0);">".MyIntentService"</span><span class="pln"> </span><span class="tag" style="color: rgb(0, 0, 136);">/></span></p>
运行结果
09-14 22:23:34.730: I/robin(30943): com.lenovo.robin.test.MyIntentService@40541370 is  constructed
09-14 22:23:34.730: I/robin(30943):  begin  onHandleIntent() in com.lenovo.robin.test.MyIntentService@ 40541370
09-14 22:23:44.730: I/robin(30943):  end  onHandleIntent() in com.lenovo.robin.test.MyIntentService@ 40541370
09-14 22:23:44.730: I/robin(30943):  begin  onHandleIntent() in com.lenovo.robin.test.MyIntentService@ 40541370
09-14 22:23:54.740: I/robin(30943):  end  onHandleIntent() in com.lenovo.robin.test.MyIntentService@ 40541370
09-14 22:23:54.740: I/robin(30943):  begin  onHandleIntent() in com.lenovo.robin.test.MyIntentService@ 40541370
09-14 22:24:04.739: I/robin(30943):  end  onHandleIntent() in com.lenovo.robin.test.MyIntentService@ 40541370
09-14 22:24:04.739: I/robin(30943): com.lenovo.robin.test.MyIntentService@40541370 is  destroy
IntentService源码
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p><div><span class="kwd" style="color: rgb(0, 0, 136);">package</span><span class="pln"> android</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">app</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></div><div>
</div><div><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln"> android</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">content</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">Intent</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></div><div><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln"> android</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">os</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">Handler</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></div><div><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln"> android</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">os</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">HandlerThread</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></div><div><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln"> android</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">os</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">IBinder</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></div><div><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln"> android</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">os</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">Looper</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></div><div><span class="kwd" style="color: rgb(0, 0, 136);">import</span><span class="pln"> android</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">os</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">Message</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></div><div>
</div><div><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">abstract</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">class</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">IntentService</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">extends</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Service</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span></div><div><span class="pln">    </span><span class="kwd" style="color: rgb(0, 0, 136);">private</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">volatile</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Looper</span><span class="pln"> mServiceLooper</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></div><div><span class="pln">    </span><span class="kwd" style="color: rgb(0, 0, 136);">private</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">volatile</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">ServiceHandler</span><span class="pln"> mServiceHandler</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></div><div><span class="pln">    </span><span class="kwd" style="color: rgb(0, 0, 136);">private</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">String</span><span class="pln"> mName</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></div><div><span class="pln">    </span><span class="kwd" style="color: rgb(0, 0, 136);">private</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">boolean</span><span class="pln"> mRedelivery</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></div><div>
</div><div><span class="pln">    </span><span class="kwd" style="color: rgb(0, 0, 136);">private</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">final</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">class</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">ServiceHandler</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">extends</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">Handler</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span></div><div><span class="pln">        </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">ServiceHandler</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Looper</span><span class="pln"> looper</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span></div><div><span class="pln">            </span><span class="kwd" style="color: rgb(0, 0, 136);">super</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln">looper</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></div><div><span class="pln">        </span><span class="pun" style="color: rgb(102, 102, 0);">}</span></div><div>
</div><div><span class="pln">        </span><span class="lit" style="color: rgb(0, 102, 102);">@Override</span></div><div><span class="pln">        </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln"> handleMessage</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Message</span><span class="pln"> msg</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span></div><div><span class="pln">            onHandleIntent</span><span class="pun" style="color: rgb(102, 102, 0);">((</span><span class="typ" style="color: rgb(102, 0, 102);">Intent</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln">msg</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">obj</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></div><div><span class="pln">            stopSelf</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln">msg</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">arg1</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></div><div><span class="pln">        </span><span class="pun" style="color: rgb(102, 102, 0);">}</span></div><div><span class="pln">    </span><span class="pun" style="color: rgb(102, 102, 0);">}</span></div><div>
</div><div><span class="pln">    </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">IntentService</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">String</span><span class="pln"> name</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span></div><div><span class="pln">        </span><span class="kwd" style="color: rgb(0, 0, 136);">super</span><span class="pun" style="color: rgb(102, 102, 0);">();</span></div><div><span class="pln">        mName </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln"> name</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></div><div><span class="pln">    </span><span class="pun" style="color: rgb(102, 102, 0);">}</span></div><div>
</div><div><span class="pln">    </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln"> setIntentRedelivery</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">boolean</span><span class="pln"> enabled</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span></div><div><span class="pln">        mRedelivery </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln"> enabled</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></div><div><span class="pln">    </span><span class="pun" style="color: rgb(102, 102, 0);">}</span></div><div>
</div><div><span class="pln">    </span><span class="lit" style="color: rgb(0, 102, 102);">@Override</span></div><div><span class="pln">    </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln"> onCreate</span><span class="pun" style="color: rgb(102, 102, 0);">()</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span></div><div><span class="pln">        </span><span class="com" style="color: rgb(136, 0, 0);">// TODO: It would be nice to have an option to hold a partial wakelock</span></div><div><span class="com" style="color: rgb(136, 0, 0);">        // during processing, and to have a static startService(Context, Intent)</span></div><div><span class="com" style="color: rgb(136, 0, 0);">        // method that would launch the service & hand off a wakelock.</span></div><div>
</div><div><span class="pln">        </span><span class="kwd" style="color: rgb(0, 0, 136);">super</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">onCreate</span><span class="pun" style="color: rgb(102, 102, 0);">();</span></div><div><span class="pln">        </span><span class="typ" style="color: rgb(102, 0, 102);">HandlerThread</span><span class="pln"> thread </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">HandlerThread</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"IntentService["</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">+</span><span class="pln"> mName </span><span class="pun" style="color: rgb(102, 102, 0);">+</span><span class="pln"> </span><span class="str" style="color: rgb(0, 136, 0);">"]"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></div><div><span class="pln">        thread</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">start</span><span class="pun" style="color: rgb(102, 102, 0);">();</span></div><div>
</div><div><span class="pln">        mServiceLooper </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln"> thread</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">getLooper</span><span class="pun" style="color: rgb(102, 102, 0);">();</span></div><div><span class="pln">        mServiceHandler </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">ServiceHandler</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln">mServiceLooper</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></div><div><span class="pln">    </span><span class="pun" style="color: rgb(102, 102, 0);">}</span></div><div>
</div><div><span class="pln">    </span><span class="lit" style="color: rgb(0, 102, 102);">@Override</span></div><div><span class="pln">    </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln"> onStart</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Intent</span><span class="pln"> intent</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">int</span><span class="pln"> startId</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span></div><div><span class="pln">        </span><span class="typ" style="color: rgb(102, 0, 102);">Message</span><span class="pln"> msg </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln"> mServiceHandler</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">obtainMessage</span><span class="pun" style="color: rgb(102, 102, 0);">();</span></div><div><span class="pln">        msg</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">arg1 </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln"> startId</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></div><div><span class="pln">        msg</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">obj </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln"> intent</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></div><div><span class="pln">        mServiceHandler</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">sendMessage</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln">msg</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></div><div><span class="pln">    </span><span class="pun" style="color: rgb(102, 102, 0);">}</span></div><div>
</div><div><span class="pln">    </span><span class="com" style="color: rgb(136, 0, 0);">/**</span></div><div><span class="com" style="color: rgb(136, 0, 0);">     * You should not override this method for your IntentService. Instead,</span></div><div><span class="com" style="color: rgb(136, 0, 0);">     * override {@link #onHandleIntent}, which the system calls when the IntentService</span></div><div><span class="com" style="color: rgb(136, 0, 0);">     * receives a start request.</span></div><div><span class="com" style="color: rgb(136, 0, 0);">     * @see android.app.Service#onStartCommand</span></div><div><span class="com" style="color: rgb(136, 0, 0);">     */</span></div><div><span class="pln">    </span><span class="lit" style="color: rgb(0, 102, 102);">@Override</span></div><div><span class="pln">    </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">int</span><span class="pln"> onStartCommand</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Intent</span><span class="pln"> intent</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">int</span><span class="pln"> flags</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">int</span><span class="pln"> startId</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span></div><div><span class="pln">        onStart</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln">intent</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="pln"> startId</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></div><div><span class="pln">        </span><span class="kwd" style="color: rgb(0, 0, 136);">return</span><span class="pln"> mRedelivery </span><span class="pun" style="color: rgb(102, 102, 0);">?</span><span class="pln"> START_REDELIVER_INTENT </span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln"> START_NOT_STICKY</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></div><div><span class="pln">    </span><span class="pun" style="color: rgb(102, 102, 0);">}</span></div><div>
</div><div><span class="pln">    </span><span class="lit" style="color: rgb(0, 102, 102);">@Override</span></div><div><span class="pln">    </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln"> onDestroy</span><span class="pun" style="color: rgb(102, 102, 0);">()</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span></div><div><span class="pln">        mServiceLooper</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln">quit</span><span class="pun" style="color: rgb(102, 102, 0);">();</span></div><div><span class="pln">    </span><span class="pun" style="color: rgb(102, 102, 0);">}</span></div><div><span class="pln">    </span><span class="lit" style="color: rgb(0, 102, 102);">@Override</span></div><div><span class="pln">    </span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln"> </span><span class="typ" style="color: rgb(102, 0, 102);">IBinder</span><span class="pln"> onBind</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Intent</span><span class="pln"> intent</span><span class="pun" style="color: rgb(102, 102, 0);">)</span><span class="pln"> </span><span class="pun" style="color: rgb(102, 102, 0);">{</span></div><div><span class="pln">        </span><span class="kwd" style="color: rgb(0, 0, 136);">return</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">null</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></div><div><span class="pln">    </span><span class="pun" style="color: rgb(102, 102, 0);">}</span></div><div>
</div><div><span class="pln">   </span><span class="kwd" style="color: rgb(0, 0, 136);">protected</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">abstract</span><span class="pln"> </span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln"> onHandleIntent</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Intent</span><span class="pln"> intent</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></div><div><span class="pun" style="color: rgb(102, 102, 0);">}</span></div><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p>

结束!
内容概要:本文档主要展示了C语言中关于字符串处理、指针操作以及动态内存分配的相关代码示例。首先介绍了如何实现键值对(“key=value”)字符串的解析,包括去除多余空格和根据键获取对应值的功能,并提供了相应的测试用例。接着演示了从给定字符串中分离出奇偶位置字符的方法,并将结果分别存储到两个不同的缓冲区中。此外,还探讨了常量(const)修饰符在变量和指针中的应用规则,解释了不同类型指针的区别及其使用场景。最后,详细讲解了如何动态分配二维字符数组,并实现了对这类数组的排序与释放操作。 适合人群:具有C语言基础的程序员或计算机科学相关专业的学生,尤其是那些希望深入理解字符串处理、指针操作以及动态内存管理机制的学习者。 使用场景及目标:①掌握如何高效地解析键值对字符串并去除其中的空白字符;②学会编写能够正确处理奇偶索引字符的函数;③理解const修饰符的作用范围及其对程序逻辑的影响;④熟悉动态分配二维字符数组的技术,并能对其进行有效的排序和清理。 阅读建议:由于本资源涉及较多底层概念和技术细节,建议读者先复习C语言基础知识,特别是指针和内存管理部分。在学习过程中,可以尝试动手编写类似的代码片段,以便更好地理解和掌握文中所介绍的各种技巧。同时,注意观察代码注释,它们对于理解复杂逻辑非常有帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值