AIDL:Android Interface Definition Language,即Android接口定義語言拓哺。
今天做項目又一次用到了AIDL窗价,為了避免每次都要到處的查,在這里先記一下屈糊,以備后用
AIDL用來在不用應用間單向獲取數(shù)據(jù)钱烟,
AIDL的使用
- 首先定義AIDL
//aidl協(xié)議文件,服務端眶俩、客戶端都需添加(注意莹汤,包名必須一致)
package com.example.service;
interface IAidl{
//方法前不支持public等訪問修飾符
String getValue();
//TODO 定義方法,僅支持java基本數(shù)據(jù)類型和String
}```
* 服務端
1. 定義協(xié)議文件
把IAidl.aidl放在`com.example.service`目錄下颠印,如果不報錯纲岭,Android編譯器會在gen目錄下自動生成IAidl.java文件。
2. 定義服務
package com.example.service.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import com.example.service.IAidl;
import com.example.service.util.Utils;
public class MyService extends Service {
private final IAidl.Stub mBinder = new IAidl.Stub() {
@Override
public String getValue() throws RemoteException {
return "服務端的數(shù)據(jù),APP版本號:"+Utils.getVersion(getApplicationContext());
}
};
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}```
- 在AndroidManifest.xml中注冊
android:name=".service.MyService"
android:exported="true">
<intent-filter>
<action android:name="com.lhk.service" />
</intent-filter>
</service>```
* 客戶端測試代碼
1. 同服務端第一步
2. 獲取數(shù)據(jù)
package com.example.client;
import com.example.service.IAidl;
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.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tv;
private Button btn;
private IAidl mIAidl;
ServiceConnection connection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.text);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
service();
}
});
connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mIAidl = IAidl.Stub.asInterface(service);
try {
String value = mIAidl.getValue();
// System.out.println("000000000000000"+value);
tv.setText(value);
} catch (RemoteException e) {
e.printStackTrace();
// System.out.println("000000000000000");
tv.setText(e.getMessage());
}
}
};
}
private void service(){
Intent intent = new Intent("com.lhk.service");
bindService(intent, connection, Context.BIND_AUTO_CREATE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (connection != null) {
unbindService(connection);
}
}
}
* 效果圖
![result.jpg](http://upload-images.jianshu.io/upload_images/740931-eb14b0e4653c969b.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)