Android中Services之异步IntentService

简介:

IntentService:异步处理服务,新开一个线程:handlerThread在线程中发消息,然后接受处理完成后,会清理线程,并且关掉服务。

IntentService有以下特点:

(1)  它创建了一个独立的工作线程来处理所有的通过onStartCommand()传递给服务的intents。

(2)  创建了一个工作队列,来逐个发送intent给onHandleIntent()。

(3)  不需要主动调用stopSelft()来结束服务。因为,在所有的intent被处理完后,系统会自动关闭服务。

(4)  默认实现的onBind()返回null

(5)  默认实现的onStartCommand()的目的是将intent插入到工作队列中

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

界面设置两按钮:

复制代码
 <Button
        android:id="@+id/btnStartIntentService"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="开始下载" >
    </Button>

    <Button
        android:id="@+id/btnStopIntentService"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="结束下载" >
    </Button>
复制代码

声明IntentServiceSub继承IntentService

复制代码
public class IntentServiceSub extends IntentService {

    private static final String TAG = "IntentServiceSub";

    public IntentServiceSub() {
        super("IntentServiceSub");
        Log.i(TAG, "=>IntentServiceSub");
    }

    /* (non-Javadoc)
     * @see android.app.IntentService#onCreate()
     */
    @Override
    public void onCreate() {
        Log.i(TAG, "=>onCreate");
        super.onCreate();
    }

    /* (non-Javadoc)
     * @see android.app.IntentService#onDestroy()
     */
    @Override
    public void onDestroy() {
        Log.i(TAG, "=>onDestroy");
        super.onDestroy();
    }
    
    @Override
    protected void onHandleIntent(Intent arg0) {
        Log.i(TAG, "IntentService 线程:"+Thread.currentThread.getId());
Thread.sleep(2000);
}
复制代码

页面按钮事件

复制代码
        btnStartIntentService = (Button) this.findViewById(R.id.btnStartIntentService);
        btnStopIntentService = (Button) this.findViewById(R.id.btnStopIntentService);

private OnClickListener listener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            
            case R.id.btnStartIntentService:
                Log.i(TAG, "主线程ID:"+Thread.currentThread.getId());
                if (mServiceIntent == null)
                    mServiceIntent = new Intent(AndroidServiceActivity.this,IntentServiceSub.class);
                startService(mServiceIntent);
                break;
            case R.id.btnStopIntentService:
                Log.i(TAG, "btnStopIntentService");
                if (mServiceIntent != null) {
                    stopService(mServiceIntent);
                    mServiceIntent = null;
                }
                break;
            }

        }
    };
复制代码

 本文转自欢醉博客园博客,原文链接http://www.cnblogs.com/zhangs1986/p/3602154.html如需转载请自行联系原作者


欢醉

相关文章
|
2月前
|
Java 数据库 Android开发
Android异步之旅:探索AsyncTask
Android异步之旅:探索AsyncTask
24 0
|
2月前
|
Android开发 开发者
Android异步之旅:探索IntentService
Android异步之旅:探索IntentService
20 0
|
2月前
|
消息中间件 数据库 Android开发
Android异步之旅:探索HandlerThread
Android异步之旅:探索HandlerThread
24 0
|
2月前
|
Android开发 对象存储
OSS对象储存android开发进行下载到本地文件时异步操作失效
android vivo80使用官方示例代码进行文件下载,但是使用oss.asyncGetObject(get, new OSSCompletedCallback<GetObjectRequest, GetObjectResult>()时onSuccess和onFailure不执行
|
9月前
|
安全 Java Android开发
Android 中AsyncTask后台线程,异步任务的理解
Android 中AsyncTask后台线程,异步任务的理解
101 0
|
5月前
|
XML Java 调度
Android App网络通信中通过runOnUiThread快速操纵界面以及利用线程池Executor调度异步任务实战(附源码 简单易懂)
Android App网络通信中通过runOnUiThread快速操纵界面以及利用线程池Executor调度异步任务实战(附源码 简单易懂)
31 0
|
5月前
|
XML Java Android开发
Android Studio App开发中异步任务AsynTask与异步服务IntentService的讲解与实战(实现四大名著的加载进度条 附源码)
Android Studio App开发中异步任务AsynTask与异步服务IntentService的讲解与实战(实现四大名著的加载进度条 附源码)
63 0
|
调度 Android开发
Android异步消息处理机制之Handler、Looper、Message
因为Android UI线程是线程不安全的,在子线程中更新UI会直接程序崩溃,另外当UI线程需要执行一个比较耗时的操作的话(IO操作,网络通信等),若是执行时间超过5s,程序会直接ANR,为了解决上述问题,可以使用异步消息处理机制[Handler]
|
Android开发
android用异步操作AsyncTask编写文件查看器
android用异步操作AsyncTask编写文件查看器
111 0
|
Android开发
Android面试题:bindService获取代理是同步还是异步
Android面试题:bindService获取代理是同步还是异步
376 0
Android面试题:bindService获取代理是同步还是异步