在一个apk中 最简单的AIDL实现:

在一个apk中 最简单的AIDL实现:
stepppppppppppppp1, 
  建立一个包,命名为com.example.aidl里面放这样的数据:
package com.example.aidl;


interface IAddService {
int addOne(int value);
}
steppppppppppppp2:

建立一个service.java和一个client.java, 里面放如下:


package com.example.aidltest;


import com.example.aidl.IAddService;

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

public class AddService extends Service {

	IAddService.Stub mBinder;
   int mValue=200;
	@Override
	public void onCreate() {
		super.onCreate();
		mBinder = new IAddService.Stub() {

			@Override
			public int addOne(int value) throws RemoteException {

//				return value + 1;
				return mValue+20;
			}
		};
	}

	@Override
	public IBinder onBind(Intent arg0) {
		return mBinder;
	}

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

}
package com.example.aidltest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;


import com.example.aidl.IAddService;
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.os.RemoteException;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AddClient extends Activity {

	IAddService service;
	myServiceConnection connection;

	///创建一个类
	class myServiceConnection implements ServiceConnection {
		@Override
		public void onServiceConnected(ComponentName name, IBinder boundservice) {
			service = IAddService.Stub.asInterface(boundservice);
			Toast.makeText(AddClient.this, "Service connected",
					Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			service = null;
			Toast.makeText(AddClient.this, "Service disconnected",
					Toast.LENGTH_SHORT).show();
		}
	}

	private void initService() {
		connection = new myServiceConnection();
		Intent i = new Intent();
		i.setClassName("com.example.aidltest",
				com.example.aidltest.AddService.class.getName());
		if (!bindService(i, connection, Context.BIND_AUTO_CREATE)) {
			Toast.makeText(AddClient.this, "Bind Service Failed",
					Toast.LENGTH_SHORT).show();
		}
	}

	private void releaseService() {
		unbindService(connection);
		connection = null;
	}

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

		initService();

		Button buttonCalc = (Button) findViewById(R.id.addone);
		buttonCalc.setOnClickListener(new View.OnClickListener() {
			TextView result = (TextView) findViewById(R.id.result);
			EditText value1 = (EditText) findViewById(R.id.value);

			@Override
			public void onClick(View v) {
				int v1, res = 0;
				try {
					v1 = Integer.parseInt(value1.getText().toString());
					res = service.addOne(v1);
				} catch (RemoteException e) {
					e.printStackTrace();
				}
				result.setText(Integer.valueOf(res).toString());
			}
		});
	}

	@Override
	protected void onDestroy() {
		releaseService();
		super.onDestroy();
	}
}


其他的AndroidMainfest里的内容如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.aidltest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.aidltest.AddClient"
            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="com.example.aidltest.AddService" android:process="com.example.aidltest.AddService"></service>
    </application>

</manifest>

运行即可. 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值