IPC之AIDL -- APK之间进程通信(入门)
来源:互联网 发布:骁龙优化的浏览器 编辑:程序博客网 时间:2024/06/06 10:51
Server端
新建一个Activity,一个Service,一个AIDL
Activity建好就行,接着在任意目录下建立一个AIDL
我建的一个叫
IMyAIDLText.aidl
// IMyAIDLText.aidlpackage com.mo.aidltext;// Declare any non-default types here with import statementsinterface IMyAIDLText { String getString(String str);}
接着建立服务AIDL的Service
MyService.java
package com.mo.aidltext;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;public class MyService extends Service { public MyService() { } @Override public IBinder onBind(Intent intent) { return new IMyService(); } class IMyService extends IMyAIDLText.Stub { @Override public String getString(String str) throws RemoteException { Log.e("mo", "getString:"+str); return "server"; } } @Override public void onCreate() { Log.e("mo", "onCreate"); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e("mo", "onStartCommand"); return super.onStartCommand(intent, flags, startId); }}
以上server端就写好了
Client端
这边需要两步
1)建一个Module,把Server的AIDL拷过来(注意,连同包整个拷过来,以此来保证包名一致。aidl的包名一样,系统会认为两个程序中的接口是同一个)
2)Activity
package com.mo.myaidlservice;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.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.EditText;import android.widget.Toast;import com.mo.aidltext.IMyAIDLText;public class MainActivity extends AppCompatActivity { private IMyAIDLText aidlText; private ServiceConnection serviceConnection =new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { aidlText = IMyAIDLText.Stub.asInterface(iBinder); Toast.makeText(MainActivity.this, "Connect success!", Toast.LENGTH_SHORT).show(); } @Override public void onServiceDisconnected(ComponentName componentName) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void sandStr(View view) { try { String s=aidlText.getString(((EditText) findViewById(R.id.client_editText)).getText().toString()); Toast.makeText(this,"client:"+s,Toast.LENGTH_SHORT).show(); } catch (RemoteException e) { e.printStackTrace(); } } public void binder(View view){ Intent intent = new Intent(); intent.setPackage("com.mo.aidltext"); //intent.setAction("com.mo.myservice"); bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE); } @Override protected void onDestroy() { unbindService(serviceConnection); super.onDestroy(); }}
对应的XML为
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.mo.myaidlservice.MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送" android:onClick="sandStr" android:layout_marginTop="26dp" app:layout_constraintTop_toBottomOf="@+id/client_editText" android:layout_marginLeft="8dp" app:layout_constraintLeft_toLeftOf="parent" android:layout_marginRight="8dp" app:layout_constraintRight_toRightOf="parent" /> <EditText android:id="@+id/client_editText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:text="Name" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="131dp" android:layout_marginRight="8dp" app:layout_constraintRight_toRightOf="parent" android:layout_marginLeft="8dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintHorizontal_bias="0.503" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="binder" android:onClick="binder" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" app:layout_constraintBottom_toTopOf="@+id/client_editText" android:layout_marginLeft="8dp" app:layout_constraintLeft_toLeftOf="parent" android:layout_marginRight="8dp" app:layout_constraintRight_toRightOf="parent" /></android.support.constraint.ConstraintLayout>
由此启动Server端,启动Client端,点击binder,连接成功后,就可以愉快的通信了
阅读全文
0 0
- IPC之AIDL -- APK之间进程通信(入门)
- Android 进程间通信(IPC)---AIDL入门
- android进程间通信(IPC)之AIDL
- Android进程间通信(IPC)之AIDL
- Android AIDL进程间通信(IPC)
- AIDL 实现 IPC(进程间通信)
- AIDL 进程间通信(IPC)
- IPC通信之AIDL
- 两个apk之间数据通信。(AIDL通信)
- 进程之间的通信之AIDL
- Android中IPC(进程间通信)机制之AIDL
- 安卓IPC(进程间通信)之AIDL基本使用
- Android IPC 进程间通信机制之 AIDL
- 进程通信aidl入门
- 进程(app)之间使用aidl通信
- Android开发之AIDL实现远程服务进程通信(IPC)
- Android中使用aidl在进程间通信(IPC)
- Android进程间通信(IPC)AIDL详解
- 一次Java调用OS命令的优化过程实录
- day_02_布尔、函数重载、内存分配、引用
- Spring Boot入门一:在Eclipse中使用Spring boot
- 8.8 奇异值分解
- mqtt向android推送消息(一)——发送端使用.net
- IPC之AIDL -- APK之间进程通信(入门)
- BZOJ 2351: [BeiJing2011]Matrix 哈希
- python对外接口环境配置(linux)
- 杭电暑期集训-Add More Zero
- Android.mk文件详解介绍
- C语言:变量的存储类型
- 短标记法
- nginx配置多个IP地址
- SpringMVC URL拦截器