在一个apk中 最简单的AIDL实现:
stepppppppppppppp1,
建立一个包,命名为com.example.aidl里面放这样的数据:
package com.example.aidl;
interface IAddService {
int addOne(int value);
}
steppppppppppppp2:
运行即可.
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>
运行即可.