@Override public void onReceive(Context context, Intent intent) { // 监听添加和删除事件 if (PluginManager.ACTION_PACKAGE_ADDED.equals(intent.getAction())) { try { PackageManager pm = getActivity().getPackageManager(); String pkg = intent.getData().getAuthority(); PackageInfo info = PluginManager.getInstance().getPackageInfo(pkg, 0); mApkListAdapter.addApkItem(new ApkItem(pm, info, info.applicationInfo.publicSourceDir)); } catch (Exception e) { e.printStackTrace(); } } else if (PluginManager.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) { String pkg = intent.getData().getAuthority(); int num = mApkListAdapter.getCount(); ApkItem removedItem = null; for (int i = 0; i < num; i++) { ApkItem item = mApkListAdapter.getApkItem(i); if (TextUtils.equals(item.packageInfo.packageName, pkg)) { removedItem = item; break; } } if (removedItem != null) { mApkListAdapter.removeApkItem(removedItem); } } }
@Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); LinearLayoutManager llm = new LinearLayoutManager(view.getContext()); llm.setOrientation(LinearLayoutManager.VERTICAL); mRvRecycler.setLayoutManager(llm); mApkListAdapter = new ApkListAdapter(getActivity(), ApkOperator.TYPE_START); mRvRecycler.setAdapter(mApkListAdapter); mInstallApkReceiver = new InstallApkReceiver(); mInstallApkReceiver.registerReceiver(this.getActivity()); if (PluginManager.getInstance().isConnected()) { loadApks(); } else { PluginManager.getInstance().addServiceConnection(mServiceConnection); } }
// 获取安装中获取Apk private ArrayList<ApkItem> getApkFromInstall() { ArrayList<ApkItem> apkItems = new ArrayList<>(); try { final List<PackageInfo> infos = PluginManager.getInstance().getInstalledPackages(0); if (infos == null) { return apkItems; } final PackageManager pm = getActivity().getPackageManager(); // noinspection all for (final PackageInfo info : infos) { apkItems.add(new ApkItem(pm, info, info.applicationInfo.publicSourceDir)); } } catch (RemoteException e) { e.printStackTrace(); } return apkItems; }