Android studio 使用aidl实现远程服务
2016-06-19 13:44
459 查看
AIDL
定义
在androidstudio简单使用
服务端
客户端
代码
新建aidl文件
app右键 new->aidl
Build ->mark project (生成对应的java文件)
客户端代码
定义
在androidstudio简单使用
服务端
客户端
代码
AIDL
定义
Android Interface definition language的缩写,它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口在androidstudio简单使用
服务端
新建服务public class TestService extends Service { private static final String TAG = "TestService"; @Nullable @Override public IBinder onBind(Intent intent) { Log.e(TAG, "onBind: " ); return new TextBinder(); } class TextBinder extends IMyAidlInterface.Stub{ @Override public void pay(int money) throws RemoteException { TestService.this.pay(money); } } public void pay(int money){ Log.e(TAG, "pay: "+money+"sueecss" ); } }
新建aidl文件
app右键 new->aidl
// IMyAidlInterface.aidl package cn.zsp.aidldemo_01; // Declare any non-default types here with import statements interface IMyAidlInterface { void pay(int money); }
Build ->mark project (生成对应的java文件)
客户端
复制服务端的aidl (包括包要完全一致)客户端代码
package cn.zsp.aidldemo_02; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.ServiceConnection; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.IBinder; import android.os.RemoteException; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; import java.util.List; import cn.zsp.aidldemo_01.IMyAidlInterface; public class MainActivity extends AppCompatActivity { private EditText etMoney; private IMyAidlInterface mMyAidlInterface; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etMoney= (EditText) findViewById(R.id.etMoney); Intent intent=new Intent(); intent.setAction("cn.zsp.testservice"); Intent intent1=new Intent(getExplicitIntent(this,intent)); bindService(intent1, new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { mMyAidlInterface = IMyAidlInterface.Stub.asInterface(service); Toast.makeText(MainActivity.this, "服务连接成功", Toast.LENGTH_SHORT).show(); //服务链接成功才能进行后续操作 否者会报空指针异常 服务连接需要时间 } @Override public void onServiceDisconnected(ComponentName name) { Toast.makeText(MainActivity.this, "服务连接失败", Toast.LENGTH_SHORT).show(); } },BIND_AUTO_CREATE); } public void pay(View view){ try { String money=etMoney.getText().toString().trim(); mMyAidlInterface.pay(Integer.parseInt(money)); } catch (RemoteException e) { e.printStackTrace(); } } /** * Android 5.0 之后 服务必须显示启动 所以需要把隐式改成显示启动 * @param context * @param implicitIntent * @return */ public static Intent getExplicitIntent(Context context, Intent implicitIntent) { // Retrieve all services that can match the given intent PackageManager pm = context.getPackageManager(); List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0); // Make sure only one match was found if (resolveInfo == null || resolveInfo.size() != 1) { return null; } // Get component info and create ComponentName ResolveInfo serviceInfo = resolveInfo.get(0); String packageName = serviceInfo.serviceInfo.packageName; String className = serviceInfo.serviceInfo.name; ComponentName component = new ComponentName(packageName, className); // Create a new intent. Use the old one for extras and such reuse Intent explicitIntent = new Intent(implicitIntent); // Set the component to be explicit explicitIntent.setComponent(component); return explicitIntent; } }
代码
链接:http://pan.baidu.com/s/1nvdCKFv 密码:es58相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析