服务器端与客户端 aidl包必须一致 如果有实体类必须实现parcelable 并且如图在java包里也要与aidl工程包一致
重构项目会自动生成进程通讯接口
tiny_aidl2.png
客户端声明权限
<!--声明权限-->
<uses-permission android:name="cn.codingblock.permission.ACCESS_CONTACT_MANAGER"/>
<!--定义权限-->
<permission
android:name="cn.codingblock.permission.ACCESS_CONTACT_MANAGER"
android:protectionLevel="normal"/>
客户端绑定服务器端创建的服务
tiny_aidl3.png
然后有了iMyAidlInterface 对象就可以调用服务器提供的方法进行进程间的通讯了
服务器的service 也很简单 创建一个class类继承aidl接口.Stub 重写接口里方法
然后把这个自定义类 返回给IBinder
服务器的service
public class TinyappRemoteService extends Service {
private List<AppListBean> appListBeans;
private AppListBeanDao appListBeanDao;
public TinyappRemoteService() {
}
public static String packName="com.tencent.tinyapp";
public static String startActivityName="com.tencent.tinyapp.view.WebviewActivity";
public static String startMain="com.tencent.tinyapp.MainActivity";
private List<AppletInfo> appletInfos;
@Override
public IBinder onBind(Intent intent) {
if (checkCallingOrSelfPermission("cn.codingblock.permission.ACCESS_CONTACT_MANAGER") == PackageManager.PERMISSION_DENIED) {
Log.i("TAG", "onBind: 权限校验失败,拒绝绑定...");
return null;
}
Log.i("TAG", "onBind: 权限校验成功!");
return new MyBinder();
}
@Override
public void onCreate() {
super.onCreate();
DaoSession appListDaoSession = MyApp.appListDaoSession;
appListBeanDao = appListDaoSession.getAppListBeanDao();
appListBeans = appListBeanDao.loadAll();
appletInfos=new ArrayList<>();
for (int i = 0; i < appListBeans.size(); i++) {
appletInfos.add(new AppletInfo(appListBeans.get(i).getCh_name(), appListBeans.get(i).getLogo_url()));
Log.i("TAG", "onCreate: "+appletInfos.get(i).getCh_name());
}
}
class MyBinder extends TinyAppInterface.Stub{
@Override
public List<AppletInfo> getAppletInfoList() throws RemoteException {
Log.i("TAG", "getAppletInfoList: =====");
return appletInfos;
}
@Override
public void startMiniGames(String ch_name) throws RemoteException {
List<AppListBean> list = appListBeanDao.queryBuilder().where(AppListBeanDao.Properties.Ch_name.eq(ch_name)).list();
String app_type = list.get(0).getApp_type();
String en_name = list.get(0).getEn_name();
String lastest_version = list.get(0).getLastest_version();
String s = en_name + Constant.FILE_LINK + lastest_version;
Log.i("TAG", "startMiniGames:en_name =="+en_name+",app_type =="+app_type+",ch_name ==" +ch_name);
Intent intentMain = new Intent();
intentMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName componentName1 = new ComponentName(packName, startMain);
intentMain.setComponent(componentName1);
// intentMain.putExtra("startName",ch_name);
// intentMain.putExtra("enName",en_name);
// startActivity(intentMain);
Intent intentWeb = new Intent();
ComponentName componentName = new ComponentName(packName, startActivityName);
intentWeb.setComponent(componentName);
intentWeb.setAction(Intent.ACTION_VIEW);
intentWeb.putExtra("startName",ch_name);
intentWeb.putExtra("enName",s);
intentWeb.putExtra("appType",app_type);
Intent[] intents={intentMain,intentWeb};
startActivities(intents);
}
}
}
总结 一定要注意包名,不要搞错了!上面那长的包名是服务器端创建service 自定义添加的filter项
网友评论