我有一个片段,在单击卸载时使用意图卸载应用。
View.OnClickListener removeButtonClick = new View.OnClickListener() {
@Override
public void onClick(View paramView) {
PackageManager pm = ContextProvider.getContext().getPackageManager();
Intent intent = new Intent(Intent.ACTION_DELETE, Uri.fromParts("package",
app.packageName, null));
int result = 0;
startActivityForResult(intent, result);
}
};
当活动完成后,它成功调用:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
new MainActivity.UpdateDatabase().execute(); // update database
ContextProvider.getContext().getFragmentManager().popBackStack(); // go back
}
但是requestCode&无论用户是按下卸载还是取消,resultCode都是0,数据为空。我怎样才能知道他们选择了什么?
这就是我最终做的事情:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Boolean found = false;
PackageManager pm = ContextProvider.getContext().getPackageManager();
List<PackageInfo> packages = pm.getInstalledPackages(0);
for (PackageInfo pi : packages) {
if (pi.packageName.equals(app.packageName)) {
found = true;
break;
}
}
if (!found) { // User uninstalled app so update database and go back
new MainActivity.UpdateDatabase().execute();
goBack();
}
}
答案 0 :(得分:2)
不知道该怎么做,但是你能否只使用包管理器看看包装是否存在?