Android进阶之路 - IntentService的使用方式

上篇( Andriod进阶之路 - Service(四大组件-服务)的使用方式)

本篇文章讲解的是之前文章没有写完的知识点,我们跟着上篇讲起,上篇我们讲到Service默认是运行在主线程的,我们为了减少ANR的可能性,我们往往会开启子线程,所以我们会在Service中的onStartCommand的生命周期中,开启子线程执行我们的逻辑;同时考虑到我们往往会让我们的逻辑执行完毕就关闭服务,不能让它一直运行在后台,所以我们会在逻辑执行完毕之后调用stopSelf()方法关闭服务,如下图。
Android进阶之路 - IntentService的使用方式_第1张图片

为了让活动与服务更贴切的使用,同时执行以上的操作,我们因此有了IntentService,默认其中的onHandleIntent方法中在内部开启了子线程去执行逻辑,之后通过了Handler传到主线程执行的结果。

Code 结构:
1.MainAcitivity、MyService、MyIntentService Code与MainActivity Xml Code
2.MyService与IntentService执行结果图

注意点:
1.使用Service记得在清单注册,四大组件都需要,只是广播的话有静态动态区别
2.使用不同的Service需要注意是继承的是Service还是IntentService,记得在不同的生命周期执行相应的操作
3.继承Intent 记得改为无参构造,也可以说是默认调用父类的有参构造

区别与优势:
1.IntentService内置handler机制
2.IntentService使用完毕之后自我销毁,不需要手动去进行stop或unbind
3.避免有些程序员使用后没有解绑造成内存泄露

MainActivity Code:

package com.example.intentservice;

import android.os.Bundle;
import android.app.Activity;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

    private TextView mCommandservice;
    private TextView mIntentService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        mCommandservice = (TextView) findViewById(R.id.tv_StartCommandservice);
        mIntentService = (TextView) findViewById(R.id.tv_Intentservice);

        mCommandservice.setOnClickListener(this);
        mIntentService.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        //一般的Service开启子线程使用
        case R.id.tv_StartCommandservice:
            Log.e("tag", "MainThread StartCommandservice Threadid is "
                    + Thread.currentThread().getId());
            Intent StartCommandservice = new Intent(MainActivity.this,
                    Myservice.class);
            startService(StartCommandservice);
            break;
            //IntentService
        case R.id.tv_Intentservice:
            Log.e("tag", "MainThread IntentService Threadid is "
                    + Thread.currentThread().getId());
            Intent Intnetservice = new Intent(MainActivity.this,
                    MyIntentService.class);
            startService(Intnetservice);
            break;
        default:
            break;
        }
    }
}

MyService Code:

package com.example.intentservice;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class Myservice extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("I think need it to do something");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("tag", "onCreate");
    }

    // 其实我们开启服务一般都逻辑都写在这里,为了减少ANR的可能,我们会开启子线程
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                Log.e("tag", "MyService onStartCommandThread  Threadid is"+Thread.currentThread().getId());
//              Toast.makeText(Myservice.this, "Myservice的逻辑操作执行完毕", 0).show();

            }
        }).start();
        // 关闭服务
        stopSelf();
        Log.e("tag", "onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("tag", "onDestroy");
    }
}

MyIntentService Code:

package com.example.intentservice;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

public class MyIntentService extends IntentService {

    //调用父类的有参构造参数
    public MyIntentService() {
        super("MyIntentService");
    }

    //这里其实默认开启了线程,且执行完任务会自己关闭服务
    @Override
    protected void onHandleIntent(Intent arg0) {
        Log.e("tag", "MyIntentService Threadid is "+Thread.currentThread().getId());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("tag", "MyIntentService is onDestroy");
    }
}

MainActivity Xml Code:

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

    <TextView
        android:id="@+id/tv_StartCommandservice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="一般Service子线程运行" />

    <TextView
        android:id="@+id/tv_Intentservice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Intent Service子线程运行" />

LinearLayout>

程序运行结果
-MyService:
这里写图片描述

-MyIntentService:
这里写图片描述

你可能感兴趣的:(Android进阶之路)