四大应用组件之Service【Android】

一、关于Service

1.Service是一个应用组件, 它用来在后台完成一个时间跨度比较大的工作且没有关联任何界面
2.一个Service可以完成下面这些工作:
访问网络 
播放音乐
文件IO操作
大数据量的数据库操作
……
3. 服务的特点:

Service在后台运行,不用与用户进行交互         

即使应用退出, 服务也不会停止. 

在默认情况下,Service运行在应用程序进程的主线程(UI线程)中,如果需要在Service中处理一些网络连接等耗时的操作,那么应该将这些任务放在分线程中处理,避免阻塞用户界面

4.与Activity的区别:

1). Activity:    

Activity对应一个界面

应用退出, Activity对象就会死亡

应用再次进入, 启动的Activity对象是重新创建的



2). Service

不与任何界面关联

应用退出, Service仍在运行

应用再次进入, 启动的Service还是前面运行的Service对象

5.与Thread的区别:

1). Service

用来在后台完成一个时间跨度比较大的工作的应用组件

Service的生命周期方法运行在主线程, 如果Service想做持续时间比较长的工作, 需要启动一个分线程(Thread)

应用退出: Service不会停止

应用再次进入: 可以与正在运行的Service进行通信



2). Thread

用来开启一个分线程的类, 做一个长时间的工作

Thread对象的run()在分线程执行

应用退出: Thread不会停止, 

应用再次进入: 不能再控制前面启动的Thread对象

6.定义Service

本地:

1)、定义一个类继承于Service类

<pre name="code" class="java">package com.example.service.local;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
/**
 * 自定义本地服务
 * @author Xiaocici
 *
 */
/*
1.startService()
	第一次调用:-->构造方法()-->onCreate()-->onStartCommand()
	(重要)后面再调用:-->onStartCommand()
  stopService():-->onDestroy()
2.bindService(intent, serviceConnection)
	第一次调用:-->构造方法()-->onCreate()-->onBind()-->onServiceConnected()
  unbindService():(只有当前Activity与Service连接)—->onUnbind()-->onDestroy()
 */
public class MyService extends Service {

	
	public MyService() {
		Log.e("TAG", "MyService()");
	}
	@Override
	public IBinder onBind(Intent intent) {
		Log.e("TAG", "onBind()");
		return new Binder();
	}
	
	@Override
	public boolean onUnbind(Intent intent) {
		Log.e("TAG", "onUnbind()");
		return super.onUnbind(intent);
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.e("TAG", "MyService onStartCommand()");
		return super.onStartCommand(intent, flags, startId);
	}
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		Log.e("TAG", "MyService onCreate()");
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.e("TAG", "MyService onDestroy()");
	}

}

 
 

2)、在AndroidManifest.xml中配置Service

<service android:name="com.example.service.local.MyService"></service>

7.启动与停止Service

方式一 :  一般启动与停止

context.startService(Intent intent)

context.stopService(Intent intent)



方式二 : 绑定启动与解绑

context.bindService(Intent intent, ServiceConnection connection)

context.unbindService(ServiceConnection connection)

例:

package com.example.service;

import com.example.service.local.MyService;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	
	//启动服务
	public void startMyService(View v){
		Intent intent = new Intent(this, MyService.class);
		startService(intent);
		Toast.makeText(this, "start service", 0).show();
	}
	//停止服务
	public void stopMyService(View v){
		Intent intent = new Intent(this, MyService.class);
		stopService(intent);
		Toast.makeText(this, "stop service", 0).show();
	}
	
	private ServiceConnection conn;
	//绑定服务
	public void bindMyService(View v){
		Intent intent = new Intent(this, MyService.class);
		//创建连接对象
		if(conn==null){
			conn = new ServiceConnection() {
			
				@Override
				public void onServiceDisconnected(ComponentName name) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onServiceConnected(ComponentName name, IBinder service) {
					// TODO Auto-generated method stub
					Log.e("Tag","onServiceConnected()");
				}
			};
			//绑定Service
			bindService(intent, conn, Context.BIND_AUTO_CREATE);
			Toast.makeText(this, "bind service", 0).show(); 
		}else{
			Toast.makeText(this, "已经bind service", 0).show(); 
		}
	}
	
	//解绑服务
	public void unbindMyService(View v){
		if(conn!=null){
			unbindService(conn);
			conn = null;
			Toast.makeText(this, "unbind service", 0).show();
		}else{
			Toast.makeText(this, "还没有bind service", 0).show();
		}
	}
	
	//在销毁Activity之前调用
	@Override
	protected void onDestroy() {
		if(conn!=null){
			unbindService(conn);
			conn = null;
		}
		super.onDestroy();
	}
}



区别: 看Service启动后是否与启动者有关联?
  Service对象经历的生命周期是否相同?

8.生命周期:


二,AIDL(远程Service)

1.AIDL理解

每个应用程序都运行在自己的独立进程中,并且可以启动另一个应用进程的服务,而且经常需要在不同的进程间传递数据对象。

在Android平台,一个进程不能直接访问另一个进程的内存空间,所以要想对话,需要将对象分解成操作系统可以理解的基本单元,并且有序的通过进程边界。

AIDL (Android Interface Definition Language) 

用于生成可以在Android设备上两个进程之间进行进程间通信(interprocess communication, IPC)的代码。

如果在一个进程中(例如Activity)要调用另一个进程中(例如Service)对象的操作,就可以使用AIDL生成可序列化的参数。

2.定义AIDL接口

编写AIDL需要注意:

1.接口名和aidl文件名相同.

2.接口和方法前不用加访问权限修饰符public,private,protected等,也不能用final,static.

3.Aidl默认支持的类型包话java基本类型(int,long,boolean等)和(String,List,Map,

  CharSequence),使用这些类型时不需要import声明.对于List和Map中的元素类型必须是

  Aidl支持的类型.如果使用自定义类型作为参数或返回值,自定义类型必须实现Parcelable接口.

4.自定义类型和AIDL生成的其它接口类型在aidl描述文件中,应该显式import,即便在该类和定义的包在同一个包中.

5.在aidl文件中所有非Java基本类型参数必须加上in、out、inout标记,以指明参数是输入参数、输出参数还是输入输出参数.

6.Java原始类型默认的标记为in,不能为其它标记.

如1).创建文件:IStudentService.aidl

package com.example.service.remote;
import com.example.service.remote.Student;
interface IStudentService {
	Student getStudentById(int Id);
}

2).创建自定义类型Student

package com.example.service.remote;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

public class Student implements Parcelable{
	private int id;
	private String name;
	private double price;
	public Student(int id, String name, double price) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", price=" + price
				+ "]";
	}
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}
	//将当前对象的属性数据打成包:写到包对象中
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		Log.e("TAG","打包writeToParcel()");
		// id
		dest.writeInt(id);
		//name
		dest.writeString(name);
		//price
		dest.writeDouble(price);
	}
	
	//添加一个静态成员,名为CREATOR,该对象实现了Parcelable.Creator接口
	public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {
		
		//解包:读取包中的数据并封装成对象
		@Override
		public Student createFromParcel(Parcel source) {
			Log.e("TAG","解包createFromParcel()");
			//id
			int id = source.readInt();
			//name
			String name = source.readString();
			//price
			double price = source.readDouble();
			return new Student(id,name, price);
		}

		//返回一个指定大小的对象容器
		@Override
		public Student[] newArray(int size) {
			// TODO Auto-generated method stub
			return new Student[size];
		}
	};	
}
3).定义自定义类型的AIDL接口

创建文件:Student.aidl

package com.example.service.remote;
parcelable Student;

4).在Service类中使用生成的类实现业务方法

package com.example.service.remote;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
/**
 * 远程Service
 * @author Xiaocici
 *
 */
public class MyRemoteService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		Log.e("TAG","onBind()");
		return new StudentService();
	}
	
	@Override
	public boolean onUnbind(Intent intent) {
		Log.e("TAG","onUnbind()");
		return super.onUnbind(intent);
	}
	//处理Student相关的业务逻辑类
	class StudentService extends IStudentService.Stub{

		@Override
		public Student getStudentById(int Id) throws RemoteException {
			Log.e("TAG","Service getStudentById"+Id);
			return new Student(Id, "Tom", 10000);
		}
		
	}

}
在 Manifest中注册Service,由于是远程服务,所以要加<intent-filter>

        <service android:name="com.example.service.remote.MyRemoteService">
            <intent-filter>
                <action android:name="com.example.service.remote.MyRemoteService.Action"/>
            </intent-filter>
        </service>


5).进行客户端编码

  • 复制服务端AIDL的相关定义
  • eclipse自动生成一个通信接口类
  • 在Activity中bind远程Service,并调用业务方法

绑定远程服务 

<span style="white-space:pre">	</span>private ServiceConnection conn;
	private IStudentService studentService;
	public void bindRemoteService(View v){
		if(conn==null){
			conn = new ServiceConnection() {
				
				@Override
				public void onServiceDisconnected(ComponentName name) {
					
				}
				
				@Override
				public void onServiceConnected(ComponentName name, IBinder service) {
					Log.e("TAG", "onServiceConnected()");
					studentService = IStudentService.Stub.asInterface(service);
				}
			};
			Intent intent = new Intent("com.example.service.remote.MyRemoteService.Action");
			bindService(intent, conn, Context.BIND_AUTO_CREATE);
			Toast.makeText(this, "绑定Service", 0).show();
		} else {
			Toast.makeText(this, "已经绑定Service", 0).show();
		}
	}
调用远程服务器里的方法

	public void invokeRemote(View v) throws RemoteException{
		if(studentService!=null){
			int id = Integer.parseInt(et_aidl_id.getText().toString());
			Student student = studentService.getStudentById(id);
			Toast.makeText(this, student.toString(), 0).show();
		}
	}
解除远程服务器的绑定

	public void unbindRemoteService(View v){
		if(conn!=null){
			unbindService(conn);
			conn = null;
			studentService = null;
			Toast.makeText(this, "解绑Service", 0).show();
		} else {
			Toast.makeText(this, "还未绑定Service", 0).show();
		}
	}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值