【Android学习】Android服务之Service--AIDLService

Android系统中,各应用程序都运行在自己的进程中,进程之间一般无法直接进行数据交换,为了实现这种跨进程通信(IPC),Android提供了AIDLService。

其跨进程调用Service过程:

--创建AIDL文件:


package com.ye_yun_lin.aidlservice;
interface ICat{
	String getColor();
	double getWeight();
}

将借口暴露给客户端:

package com.ye_yun_lin.aidlservice;

import java.util.Timer;
import java.util.TimerTask;

import com.ye_yun_lin.aidlservice.ICat.Stub;

import android.R.integer;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

public class AidlService extends Service{
	private CatBinder catBinder;
	Timer timer=new Timer();
	String[] colors=new String[]{
			"黑色",
			"红色",
			"黄色"
	};
	double[] weights=new double[]{
			2.2,
			2.6,
			3.5
	};
	
	private String color;
	private double weight;

	public class CatBinder extends Stub{

		@Override
		public String getColor() throws RemoteException {
			return color;
		}

		@Override
		public double getWeight() throws RemoteException {
			return weight;
		}
		
	}
	@Override
	public IBinder onBind(Intent intent) {
		return catBinder;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		catBinder=new CatBinder();
		timer.schedule(new TimerTask() {
			
			@Override
			public void run() {
				int rand=(int)(Math.random()*3);
				color=colors[rand];
				weight=weights[rand];
				System.out.println("---------"+rand);
			}
		}, 0, 800);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

}

在AndroidManifest中配置该Service:

        <service android:name="com.ye_yun_lin.aidlservice.AidlService">
            <intent-filter>
                <action android:name="com.ye_yun_lin.aidlservice.AidlService.action.AIDL_SERVICE"/>
            </intent-filter>
        </service>

客户端访问AIDLService:

package com.ye_yun_lin.aidlservice;

import android.R.color;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
	private ICat catService;
	private Button button;
	private EditText edit1,edit2;
	
	private ServiceConnection conn=new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			catService=null;
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			catService=ICat.Stub.asInterface(service);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		button=(Button)findViewById(R.id.button);
		edit1=(EditText)findViewById(R.id.edit1);
		edit2=(EditText)findViewById(R.id.edit2);
		
		Intent intent=new Intent();
		intent.setAction("com.ye_yun_lin.aidlservice.AidlService.action.AIDL_SERVICE");
		bindService(intent, conn, Service.BIND_AUTO_CREATE);
		
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				try {
					edit1.setText(catService.getColor());
					edit2.setText(catService.getWeight()+"");
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		});
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	
}

结果:


【Android学习】Android服务之Service--AIDLService,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。