服务(Service)全解析(三)--IntentService

来源:互联网 发布:百度云显示网络不给力 编辑:程序博客网 时间:2024/04/28 05:36

MainActivity如下:

package cc.testservice3;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;/** * Demo描述: * IntentService的使用 *  * Demo详情: * 在此处使用Service和IntentService模拟耗时任务. * 那么Service会出现ANR错误,IntentService则不会 *  * 原因说明: * 1 Service不是专门启动的一条单独进程,Service与它所在的应用位于同一进程中 * 2 Service也不是一条新的线程,所以不能在Service里面处理耗时的任务。所以,可以在service中开启子线程来处理耗时的任务 *  * IntentService继承自Service但是会在onHandleIntent()中开启新的线程 * 来处理任务,所以不会造成ANR *  * 总结IntentService的两大特点: * 1 会开启一个新的线程执行异步耗时任务 * 2 IntentService在执行完耗时任务后会自动停止该Service *   不用我们以前那样stopService()或者unbindService(). */public class MainActivity extends Activity { private Button mStartServiceButton; private Button mStartIntentServiceButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);init();}private void init(){    //利用Service操作耗时任务mStartServiceButton=(Button) findViewById(R.id.startServiceButton);mStartServiceButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {Intent intent=new Intent(MainActivity.this, ServiceSubclass.class);startService(intent);}});//利用IntentService操作耗时任务mStartIntentServiceButton=(Button) findViewById(R.id.startIntentServiceButton);mStartIntentServiceButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {Intent intent=new Intent(MainActivity.this, IntentServiceSubclass.class);startService(intent);}});}}



ServiceSubclass如下:

package cc.testservice3;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class ServiceSubclass extends Service {@Overridepublic IBinder onBind(Intent arg0) {return null;}public void onCreate() {System.out.println("---> Service onCreate()");}@Overridepublic void onStart(Intent intent, int startId) {super.onStart(intent, startId);System.out.println("---> Service onStart()");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {System.out.println("---> Service onStartCommand()");//模拟耗时的操作try {Thread.sleep(30*1000);} catch (Exception e) {e.printStackTrace();}return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {super.onDestroy();System.out.println("---> Service onDestroy()");}}


IntentServiceSubclass如下:

package cc.testservice3;import android.app.IntentService;import android.content.Intent;/** * 注意事项: * 在继承自IntentService时Eclipse会提示生成一个带String参数的 * 构造方法.我们按照该提示生成构造方法后,运行时会报错: * java.lang.InstantiationException *  * 解决办法: * 删除该提示生成的构造方法,写一个空参数的构造方法. * 但在该空参数构造方法里执行super(""); * */public class IntentServiceSubclass extends IntentService {public IntentServiceSubclass() {super("");}@Overrideprotected void onHandleIntent(Intent intent) {//模拟耗时的操作try {Thread.sleep(30*1000);} catch (Exception e) {e.printStackTrace();}}}


main.xml如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:gravity="center_horizontal"   >    <Button        android:id="@+id/startServiceButton"        android:layout_width="200dip"        android:layout_height="150dip"        android:text="启动Service"      />          <Button        android:id="@+id/startIntentServiceButton"        android:layout_width="200dip"        android:layout_height="150dip"        android:text="启动IntentService"       /></LinearLayout>