【攻克Android (29)】IntentService

[b][size=large]本文围绕以下三个部分展开: [/size][/b]

[b][size=large]一、IntentService[/size][/b]
[b][size=large]案例一:Service & IntentService。两个Service中,都有休眠20s的耗时操作。启动Service,主界面阻塞,会出现 ANR;但启动IntentService,主界面未阻塞。[/size][/b]


[b][size=large]一、IntentService[/size][/b]

[size=medium]IntentService:异步处理服务。它新开一个线程:handlerThread,在线程中发消息,然后处理完成后,会清理线程,并且关掉服务。[/size]

[size=medium]IntentService 里面是可以进行耗时的操作的。IntentService 使用队列的方式将请求的 Intent 加入队列,然后开启一个 worker thread 来处理队列中的 Intent。对于异步的 startService 请求,IntentService会处理完成一个之后再处理第二个。当耗时的时候,Service去做耗时的操作了,不影响 Activity。[/size]

[size=medium][b]IntentService的特点:[/b][/size]

[size=medium](1)它创建了一个独立的工作线程来处理所有的通过onStartCommand()传递给服务的intents。[/size]
[size=medium](2)创建了一个工作队列,来逐个发送intent给onHandleIntent()。[/size]
[size=medium](3)不需要主动调用stopSelf()来结束服务。因为,在所有的intent被处理完后,系统会自动关闭服务。[/size]
[size=medium](4)默认实现的onBind()返回null[/size]
[size=medium](5)默认实现的onStartCommand()的目的是将intent插入到工作队列中[/size]

[size=medium]继承IntentService的类至少要实现两个函数:构造函数和onHandleIntent()函数。要覆盖IntentService的其它函数时,注意要通过super调用父类的对应的函数。[/size]


[b][size=large]案例一:Service & IntentService。两个Service中,都有休眠20s的耗时操作。启动Service,主界面阻塞,会出现 ANR;但启动IntentService,主界面未阻塞。[/size][/b]

[size=medium]主界面如下:[/size]

[align=center][img]http://dl2.iteye.com/upload/attachment/0110/6455/4b0778cf-0d2a-3516-a7b8-afa8b39826f0.png[/img][/align]

[size=medium]当启动Service的时候,因为Service里面有休眠20s的耗时操作,而且没有写在单独的线程里面,因此主界面阻塞,出现ANR。[/size]

[align=center][img]http://dl2.iteye.com/upload/attachment/0110/6457/0dce68ce-e858-36e1-868b-736d167be77e.png[/img][/align]

[size=medium]20s后,进入如下界面:[/size]

[align=center][img]http://dl2.iteye.com/upload/attachment/0110/6459/11cf6342-cfcd-3424-8966-4d620d0b7e24.png[/img][/align]

[size=medium]当启动IntentService的时候,会直接进入如下界面,主界面不阻塞。(里面的工作线程会休眠20s)[/size]

[align=center][img]http://dl2.iteye.com/upload/attachment/0110/6459/11cf6342-cfcd-3424-8966-4d620d0b7e24.png[/img][/align]


[size=medium][b]1. activity_main.xml。写一个按钮。[/b][/size]

<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>


[size=medium][b]2. MainActivity。写按钮点击事件,点击后,跳转到另一个Activity。[/b][/size]

    public void onClick(View view) {
startActivity(new Intent(this, ServiceDemoActivity.class));
}


[size=medium][b]3. 创建 ServiceDemoActivity。[/b][/size]

[size=medium][b]4. 创建一个 Service:MyService。[/b][/size]

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;
}
}


[size=medium][b]5. 创建一个 IntentService:MyIntentService。[/b][/size]

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, "睡眠结束");
}

}


[size=medium][b]代码说明:[/b][/size]

[size=medium]1. 10-12行。[/size]

[size=medium]父类IntentService中只有带参数的构造方法:[/size]

 public IntentService(String name) {
super();
mName = name;
}


[size=medium]但是,我们这儿必须用无参构造方法,因此要在 super()里面,传一个字符串。[/size]

[size=medium]之所以必须用无参构造方法,是为了在 ServiceDemoActivity 中:[/size]

startService(new Intent(this, MyIntentService.class));


[size=medium]intent跳转到其他组件,其他组件必须包含无参构造方法。如果只有含参构造方法,则在跳转的时候必须传入参数。但是跳转的时候无法传参,因此必须有无参构造方法。[/size]

[size=medium]利用intent跳转到另一个 Activity 的时候,代码里面没有写无参构造方法。当没有写构造方法的时候,虚拟机会默认产生一个无参构造方法。当只写了一个含参构造方法时,虚拟机不会产生无参构造方法,就只有一个含参构造方法了。因此,如果写了含参构造方法,还需要无参构造方法,必须再写一个无参构造方法。[/size]

[size=medium][b]6. ServiceDemoActivity中启动MyService,主界面阻塞,出现ANR(Application not responding)。[/b][/size]

    @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));
}


[size=medium][b]7. ServiceDemoActivity中启动MyIntentService,主界面未阻塞。[/b][/size]

    @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));
}



[size=medium][b]何时用Service和IntentService?[/b][/size]

[size=medium]如果用onCreate()启动,则用 IntentService,它实现了一个线程,直接用 onHandleIntent 方法可执行耗时操作。[/size]

[size=medium]如果要用服务中自定义的方法,要用中间代理人binder,那么就用 Service。(但要注意,必须要手动开启一个工作线程来处理)。[/size]

[size=medium](当然也可以用 IntentService,但是它也同样得重写 onBind()方法,然后做和 Service一样的事情。而且也利用不到 onHandleIntent方法。因此直接用 Service就可以了。)[/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值