一、IntentService
案例一:Service & IntentService。两个Service中,都有休眠20s的耗时操作。启动Service,主界面阻塞,会出现 ANR;但启动IntentService,主界面未阻塞。
一、IntentService
IntentService:异步处理服务。它新开一个线程:handlerThread,在线程中发消息,然后处理完成后,会清理线程,并且关掉服务。
IntentService 里面是可以进行耗时的操作的。IntentService 使用队列的方式将请求的 Intent 加入队列,然后开启一个 worker thread 来处理队列中的 Intent。对于异步的 startService 请求,IntentService会处理完成一个之后再处理第二个。当耗时的时候,Service去做耗时的操作了,不影响 Activity。
IntentService的特点:
(1)它创建了一个独立的工作线程来处理所有的通过onStartCommand()传递给服务的intents。
(2)创建了一个工作队列,来逐个发送intent给onHandleIntent()。
(3)不需要主动调用stopSelf()来结束服务。因为,在所有的intent被处理完后,系统会自动关闭服务。
(4)默认实现的onBind()返回null
(5)默认实现的onStartCommand()的目的是将intent插入到工作队列中
继承IntentService的类至少要实现两个函数:构造函数和onHandleIntent()函数。要覆盖IntentService的其它函数时,注意要通过super调用父类的对应的函数。
案例一:Service & IntentService。两个Service中,都有休眠20s的耗时操作。启动Service,主界面阻塞,会出现 ANR;但启动IntentService,主界面未阻塞。
主界面如下:
当启动Service的时候,因为Service里面有休眠20s的耗时操作,而且没有写在单独的线程里面,因此主界面阻塞,出现ANR。
20s后,进入如下界面:
当启动IntentService的时候,会直接进入如下界面,主界面不阻塞。(里面的工作线程会休眠20s)
1. activity_main.xml。写一个按钮。
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:id="@+id/btnServiceDemo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="@string/title_activity_service_demo" /> </RelativeLayout>
2. MainActivity。写按钮点击事件,点击后,跳转到另一个Activity。
public void onClick(View view) { startActivity(new Intent(this, ServiceDemoActivity.class)); }
3. 创建 ServiceDemoActivity。
4. 创建一个 Service:MyService。
package com.android.servicedemo; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class MyService extends Service { public MyService() { } @Override public void onCreate() { super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // Service里面是不能进行耗时的操作的 // 必须要手动开启一个工作线程来处理耗时操作: new Thread(){}.start; // 此处直接进行耗时操作,因此主界面阻塞,出现 ANR。 try { // 线程休眠20秒(服务在这个方法里面工作了20秒。) Thread.sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } return START_STICKY; } @Override public void onDestroy() { super.onDestroy(); } @Override public IBinder onBind(Intent intent) { return null; } }
5. 创建一个 IntentService:MyIntentService。
package com.android.servicedemo; import android.app.IntentService; import android.content.Intent; import android.util.Log; public class MyIntentService extends IntentService { private static final String TAG = "MainActivity"; public MyIntentService() { super("IntentService"); } /** * IntentService 已经实现了线程。 * 不管是耗时还是不耗时的,都是用线程去处理。 * * @param intent */ @Override protected void onHandleIntent(Intent intent) { Log.v(TAG, "onHandleIntent()"); try { Thread.sleep(20000); } catch (InterruptedException e) { e.printStackTrace(); } Log.v(TAG, "睡眠结束"); } }
代码说明:
1. 10-12行。
父类IntentService中只有带参数的构造方法:
public IntentService(String name) { super(); mName = name; }
但是,我们这儿必须用无参构造方法,因此要在 super()里面,传一个字符串。
之所以必须用无参构造方法,是为了在 ServiceDemoActivity 中:
startService(new Intent(this, MyIntentService.class));
intent跳转到其他组件,其他组件必须包含无参构造方法。如果只有含参构造方法,则在跳转的时候必须传入参数。但是跳转的时候无法传参,因此必须有无参构造方法。
利用intent跳转到另一个 Activity 的时候,代码里面没有写无参构造方法。当没有写构造方法的时候,虚拟机会默认产生一个无参构造方法。当只写了一个含参构造方法时,虚拟机不会产生无参构造方法,就只有一个含参构造方法了。因此,如果写了含参构造方法,还需要无参构造方法,必须再写一个无参构造方法。
6. ServiceDemoActivity中启动MyService,主界面阻塞,出现ANR(Application not responding)。
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_service_demo); // 启动服务 // 主界面阻塞,最终会出现 ANR(Application not responding) startService(new Intent(this, MyService.class)); }
7. ServiceDemoActivity中启动MyIntentService,主界面未阻塞。
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_service_demo); // 连续两次启动 IntentService,会发现应用程序不会阻塞, // 而且最重的是第二次的请求会在第一个请求结束之后运行 // (这个证实了IntentService采用单独的线程每次只从队列中拿出一个请求进行处理) startService(new Intent(this, MyIntentService.class)); startService(new Intent(this, MyIntentService.class)); }
何时用Service和IntentService?
如果用onCreate()启动,则用 IntentService,它实现了一个线程,直接用 onHandleIntent 方法可执行耗时操作。
如果要用服务中自定义的方法,要用中间代理人binder,那么就用 Service。(但要注意,必须要手动开启一个工作线程来处理)。
(当然也可以用 IntentService,但是它也同样得重写 onBind()方法,然后做和 Service一样的事情。而且也利用不到 onHandleIntent方法。因此直接用 Service就可以了。)