服務器端與客戶端 aidl包必須一致 如果有實體類必須實現parcelable 並且如圖在java包裏也要與aidl工程包一致
重構項目會自動生成進程通訊接口
客戶端聲明權限
<!--聲明權限-->
<uses-permission android:name="cn.codingblock.permission.ACCESS_CONTACT_MANAGER"/>
<!--定義權限-->
<permission
android:name="cn.codingblock.permission.ACCESS_CONTACT_MANAGER"
android:protectionLevel="normal"/>
客戶端綁定服務器端創建的服務
然後有了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項