Android入门之路 - IntentService的使用方式

本篇文章讲解的是之前文章没有写完的知识点,我们跟着上篇讲起…

Service 兄弟篇

上篇我们讲到Service默认是运行在主线程的,我们为了减少ANR的可能性,我们往往会开启子线程,所以我们会在Service中的onStartCommand的生命周期中,开启子线程执行我们的逻辑;

同时考虑到我们往往会让逻辑执行完毕就关闭服务,不能让它一直运行在后台,所以我们会在逻辑执行完毕之后调用stopSelf()方法关闭服务,如下图。
这里写图片描述

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

区别与优势

  • IntentService本身内部封装了一个异步框架,内置ServiceHandler、ServiceThread、还有Looper的轮询器,所以理解handler机制还是很重要的
  • IntentService使用完毕之后自我销毁,不需要手动去进行stop或unbind
  • 避免有些程序员使用后没有解绑造成内存泄露

注意点

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

我们先看一下俩种Service的执行结果

  • MyService
    这里写图片描述

  • MyIntentService
    这里写图片描述

平常Service - MyService

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

IntentService - MyIntentService

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

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

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

activity_main

<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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

远方那座山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值