需求:在应用中,能够获取推荐的应用列表,下载应用进行安装,且安装成功后会修改我当前的UI界面。
对于没有Root的机型,我的安装应用的方法就是下载APK到本地后,发送Intent请求,源码如下:
public static boolean installNormal(Context context, String filePath) {
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(filePath);
if (!file.exists() || !file.isFile() || file.length() < 0) {
return false;
}
LogUtil.e(TAG, "path=" + filePath);
intent.setDataAndType(Uri.parse("file://" + filePath),
"application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
return true;
}
使用这种方式,我无法判断出用户最终是否选择安装了应用(因为用户可能取消安装),因此无法进行相关UI的修改。
所以,想问一下大家,使用intent的方法安装应用如何获取应用安装的结果?
我个人想了两种方式:
注册广播,但是使用广播是否应该动态注册?监听哪些intent?
写一个线程无限轮询,但是不清楚线程默认的结束时间等相关限制条件?
Listen to system application installation or update broadcasts
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
The last line must be added, and then extracted from the obtained Intent Package information, intent.getDataString(), see what application it is
When you jump out to install, no matter whether the user clicks to install or cancel, it will go to onResume when you come back. Then judge the installation status in onResume
If you overwrite and install the current apk, you cannot monitor it. If you install other programs with different package names from this program, the broadcast can be monitored. You need to check the specific IntentFilter. This official website There are corresponding ones above.