You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:Android中PackageInstaller.uninstall程序化卸载应用的实现示例

How to Replace Deprecated ACTION_UNINSTALL_PACKAGE with PackageInstaller.uninstall()

No worries—let's break down a clean, working implementation of the recommended PackageInstaller.uninstall() method to replace the deprecated ACTION_UNINSTALL_PACKAGE API. This example covers everything you need, from permissions to handling uninstall results.

Step 1: Add Required Permission

First, declare the REQUEST_DELETE_PACKAGES permission in your AndroidManifest.xml—this is mandatory for uninstalling apps on Android 8.0 (API 26) and above:

<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />

This is a normal permission, so you don't need to request it dynamically; the system grants it automatically when your app is installed.

Step 2: Implement the Uninstall Logic

Here's a complete method you can add to your Activity/Fragment to trigger an uninstall:

private void uninstallApp(String packageName) {
    PackageInstaller packageInstaller = getPackageManager().getPackageInstaller();
    
    // Create a broadcast receiver to handle the uninstall result
    Intent resultIntent = new Intent(this, UninstallResultReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            this,
            0,
            resultIntent,
            // Use FLAG_IMMUTABLE for API 30+ (safer, since we don't need to modify the intent)
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
    );
    
    // Trigger the uninstall
    packageInstaller.uninstall(
            packageName,
            pendingIntent.getIntentSender()
    );
}

Step 3: Create a Broadcast Receiver for Results

You need a BroadcastReceiver to listen for whether the uninstall succeeded or failed. Create a new class like this:

public class UninstallResultReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        int resultCode = getResultCode();
        if (resultCode == RESULT_OK) {
            Toast.makeText(context, "App uninstalled successfully!", Toast.LENGTH_SHORT).show();
            // You can add logic here to update your app list, etc.
        } else {
            // Get error details if available
            String errorMessage = intent.getStringExtra(PackageInstaller.EXTRA_ERROR_MESSAGE);
            String failMsg = errorMessage != null ? "Uninstall failed: " + errorMessage : "Uninstall failed";
            Toast.makeText(context, failMsg, Toast.LENGTH_LONG).show();
        }
    }
}

Don't forget to register this receiver in your AndroidManifest.xml:

<receiver android:name=".UninstallResultReceiver" />

Key Notes

  • PendingIntent Flags: For API 30+, use FLAG_IMMUTABLE (as shown) unless you specifically need to modify the intent later. This aligns with Android's security best practices.
  • Result Handling: Unlike the old startActivityForResult approach, the broadcast receiver lets you capture results even if your app is in the background.
  • Compatibility: This works on API 21+ (since PackageInstaller was introduced in API 21), but the REQUEST_DELETE_PACKAGES permission is required starting from API 26.

Now you can call uninstallApp(app.getPackageName()) instead of your old code, and it'll work seamlessly on modern Android versions.

内容的提问来源于stack exchange,提问作者Toni Joe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:38:18