我有一个片段,当他们单击卸载时,它使用意图卸载应用程序。
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();
}
}