AIDL跨进程访问

点击打开链接aidl是android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口,一种本地代码技术

创建AIDL服务与使用AIDL服务举例,

一、AIDL服务的项目图


实现的步骤为:

1.创建IMyService.aidl文件,内容为

package com.fs.aidl.service;
interface IMyService
{
    String getValue(String s);
}

该文件放入到com.fs.aidl.service文件夹后,会自动在gen下com.fs.aidl.service中生成IMyService.java文件


2.创建MyService.java文件,内容为:

package com.fs.aidl.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class MyService extends Service {
	public class MyServiceImpl extends IMyService.Stub {
		// @Override
		public String getValue(String s) throws RemoteException {
			//写出自已的业务逻辑代码
			return "华清远见: "+s;
		}
	}
	@Override
	public IBinder onBind(Intent intent) {
		return new MyServiceImpl();
	}
}

3、AndroidManifest.xml内容为

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.fs.aidl.service"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".MyService" >
			<intent-filter> 
				<action android:name="com.fs.aidl.service.MyService" />
			</intent-filter>
		</service>
    </application>
    <uses-sdk android:minSdkVersion="4" />

</manifest> 

二、使用AIDL服务的项目图

 

1、android5_4aidlService项目中的MyService.java连同包一起复制出来粘贴在android5_4aidlClient项目中

2、main.xml内容为

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:id="@+id/textview" 
		android:textColor="#FFFF0000"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:textSize="36dp" />
	<Button android:id="@+id/btnBindAIDLService"
		android:layout_width="fill_parent" android:layout_height="wrap_content"
		android:text="绑定AIDL服务" android:layout_marginTop="10dp" />
	<Button android:id="@+id/btnInvokeAIDLService"
		android:layout_width="fill_parent" android:layout_height="wrap_content"
		android:text="调用AIDL服务" android:layout_marginTop="10dp" />
</LinearLayout>

3、MainActivity的内容:

package com.fs.aidl.client;

import com.fs.aidl.service.IMyService;

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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class UserActivity extends Activity implements OnClickListener {
	private IMyService myService = null;
	private Button btnInvokeAIDLService;
	private Button btnBindAIDLService;
	private TextView textView;
	private ServiceConnection serviceConnection = new ServiceConnection() {
		// @Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			myService = IMyService.Stub.asInterface(service);
			btnInvokeAIDLService.setEnabled(true);
		}
		// @Override
		public void onServiceDisconnected(ComponentName name) {
		}
	};
	// @Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btnBindAIDLService:
			this.bindService(
					new Intent("com.fs.aidl.service.MyService"),
					     serviceConnection, Context.BIND_AUTO_CREATE);
			break;
		case R.id.btnInvokeAIDLService:
			try {
				textView.setText(myService.getValue("xx"));
			} catch (Exception e) {
			}
			break;
		}
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		btnInvokeAIDLService = (Button) findViewById(R.id.btnInvokeAIDLService);
		btnBindAIDLService = (Button) findViewById(R.id.btnBindAIDLService);
		btnInvokeAIDLService.setEnabled(false);
		textView = (TextView) findViewById(R.id.textview);
		btnInvokeAIDLService.setOnClickListener(this);
		btnBindAIDLService.setOnClickListener(this);
	}
}


三、测试过程

1、安装android5_4aidlService项目到手机中

2、安装并运行android5_4aidlClient项目

 3、定击“绑定AIDL服务”按扭,得到图

 4、点击“调用AIDL服务”按钮,得到图

 

 四、完

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值