美文网首页
Android随笔-AIDL

Android随笔-AIDL

作者: pyboy | 来源:发表于2016-05-18 14:26 被阅读22次

AIDL:Android Interface Definition Language,即Android接口定义语言。

今天做项目又一次用到了AIDL,为了避免每次都要到处的查,在这里先记一下,以备后用

AIDL用来在不用应用间单向获取数据,

AIDL的使用

  • 首先定义AIDL
//aidl协议文件,服务端、客户端都需添加(注意,包名必须一致)
package com.example.service;
interface IAidl{
    //方法前不支持public等访问修饰符
    String getValue(); 
    //TODO 定义方法,仅支持java基本数据类型和String
}```

* 服务端
 1. 定义协议文件
把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 "服务端的数据,APP版本号:"+Utils.getVersion(getApplicationContext());
}
};
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}```

  1. 在AndroidManifest.xml中注册
            android:name=".service.MyService"
            android:exported="true">
            <intent-filter> 
                <action android:name="com.lhk.service" /> 
            </intent-filter>
        </service>```

* 客户端测试代码
 1. 同服务端第一步
 2. 获取数据

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](https://img.haomeiwen.com/i740931/eb14b0e4653c969b.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

相关文章

网友评论

      本文标题:Android随笔-AIDL

      本文链接:https://www.haomeiwen.com/subject/xurwrttx.html