下面列出了android.content.Intent#ACTION_UNINSTALL_PACKAGE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void actionUninstallPackage() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
actionUninstallPackageQ();
return;
}
Uri uri = Uri.fromParts("package", getIntent().getStringExtra("package"), null);
Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, uri);
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
// Currently, Install & Uninstall share the same logic
// after starting the system PackageInstaller
// because the only thing to do is to call the callback
// with the result code.
// If ANY separate logic is added for any of them,
// the request code should be separated.
startActivityForResult(intent, REQUEST_INSTALL_PACKAGE);
}
private void launchUninstallIntent(AppLauncher app, ViewGroup parent, View launcher) {
if (mChildLock) return;
mDragDropSource = parent;
mBeingUninstalled = launcher;
Log.d(TAG, "Uninstalling " + app.getPackageName());
Uri packageUri = Uri.parse("package:" + app.getPackageName());
Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageUri);
uninstallIntent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
startActivityForResult(uninstallIntent, UNINSTALL_RESULT);
}
public static void uninstall(Context context, Prefs prefs) {
try {
ComponentName devAdminReceiver = new ComponentName(context, DAReceiver.class);
DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
dpm.removeActiveAdmin(devAdminReceiver);
if (prefs.proximityToLock && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP && !Shell.SU.available())
prefs.setBool(Prefs.KEYS.PROXIMITY_TO_LOCK.toString(), false);
} catch (Exception ignored) {
}
Uri packageUri = Uri.parse("package:" + context.getPackageName());
Intent uninstallIntent =
new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageUri);
uninstallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(uninstallIntent);
}
public static Intent getUninstallIntent(String packageName) {
final Uri packageURI = Uri.parse("package:" + packageName);
final Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageURI);
intent.putExtra("android.intent.extra.UNINSTALL_ALL_USERS", true);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return intent;
}
@OnClick(R.id.delete)
public void onDelete() {
Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
intent.setData(Uri.parse("package:" + appsModel.getPackageName()));
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
startActivityForResult(intent, APP_RESULT);
fabtoolbar.slideOutFab();
}
public void onClick(View v) {
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);
}
@Override
public void uninstall(VersionedPackage versionedPackage, String callerPackageName, int flags,
IntentSender statusReceiver, int userId) throws RemoteException {
final int callingUid = Binder.getCallingUid();
mPermissionManager.enforceCrossUserPermission(callingUid, userId, true, true, "uninstall");
if ((callingUid != Process.SHELL_UID) && (callingUid != Process.ROOT_UID)) {
mAppOps.checkPackage(callingUid, callerPackageName);
}
// Check whether the caller is device owner or affiliated profile owner, in which case we do
// it silently.
final int callingUserId = UserHandle.getUserId(callingUid);
DevicePolicyManagerInternal dpmi =
LocalServices.getService(DevicePolicyManagerInternal.class);
final boolean isDeviceOwnerOrAffiliatedProfileOwner =
dpmi != null && dpmi.isActiveAdminWithPolicy(callingUid,
DeviceAdminInfo.USES_POLICY_PROFILE_OWNER)
&& dpmi.isUserAffiliatedWithDevice(callingUserId);
final PackageDeleteObserverAdapter adapter = new PackageDeleteObserverAdapter(mContext,
statusReceiver, versionedPackage.getPackageName(),
isDeviceOwnerOrAffiliatedProfileOwner, userId);
if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DELETE_PACKAGES)
== PackageManager.PERMISSION_GRANTED) {
// Sweet, call straight through!
mPm.deletePackageVersioned(versionedPackage, adapter.getBinder(), userId, flags);
} else if (isDeviceOwnerOrAffiliatedProfileOwner) {
// Allow the device owner and affiliated profile owner to silently delete packages
// Need to clear the calling identity to get DELETE_PACKAGES permission
long ident = Binder.clearCallingIdentity();
try {
mPm.deletePackageVersioned(versionedPackage, adapter.getBinder(), userId, flags);
} finally {
Binder.restoreCallingIdentity(ident);
}
} else {
ApplicationInfo appInfo = mPm.getApplicationInfo(callerPackageName, 0, userId);
if (appInfo.targetSdkVersion >= Build.VERSION_CODES.P) {
mContext.enforceCallingOrSelfPermission(Manifest.permission.REQUEST_DELETE_PACKAGES,
null);
}
// Take a short detour to confirm with user
final Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
intent.setData(Uri.fromParts("package", versionedPackage.getPackageName(), null));
intent.putExtra(PackageInstaller.EXTRA_CALLBACK, adapter.getBinder().asBinder());
adapter.onUserActionRequired(intent);
}
}
private void uninstallApp(String activityName) {
Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
intent.setData(Uri.parse("package:" + activityName.split("/")[0]));
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
startActivityForResult(intent, 97);
}
@Override
public void deleteApp(BackupApp backupApp) {
Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
intent.setData(new Uri.Builder().scheme("package").opaquePart(backupApp.packageMeta().packageName).build());
startActivity(intent);
}
/**
* Raw intent to open app's uninstall prompt
*/
public static Intent getAppUninstallIntent(Context ctx) {
Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
intent.setData(Uri.parse("package:" + ctx.getPackageName()));
return intent;
}
public static void uninstallApp(Activity activity, String packageName) {
Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
intent.setData(Uri.parse("package:" + packageName));
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
activity.startActivityForResult(intent, UNINSTALL_REQUEST_CODE);
}
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
intent.setData(Uri.parse(
"package:com.example.android.helloactivity"));
startActivity(intent);
}
private void uninstallAppOps() {
Toast.makeText(this, R.string.error_msg, Toast.LENGTH_LONG).show();
Uri uri = Uri.parse("package:" + getPackageName());
Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, uri);
startActivity(intent);
}