linwoain的个人blog

知我者谓我心忧,不知我者谓我何求

0%

android安装和卸载app是否成功可回调

安装APP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + File.separator + "HelloActivity.apk")));
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
intent.putExtra(Intent.EXTRA_ALLOW_REPLACE, true);
intent.putExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME,
getApplicationInfo().packageName);
startActivityForResult(intent, REQUEST_INSTALL);
卸载APP:
Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
intent.setData(Uri.parse(
"package:com.example.android.helloactivity"));
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
startActivityForResult(intent, REQUEST_UNINSTALL);

处理回调:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == REQUEST_INSTALL) {
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, "Install succeeded!", Toast.LENGTH_SHORT).show();
} else if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(this, "Install canceled!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Install Failed!", Toast.LENGTH_SHORT).show();
}
} else if (requestCode == REQUEST_UNINSTALL) {
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, "Uninstall succeeded!", Toast.LENGTH_SHORT).show();
} else if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(this, "Uninstall canceled!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Uninstall Failed!", Toast.LENGTH_SHORT).show();
}
}
}