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

已获取Device admin权限,能否禁用应用的卸载选项?

How to Disable App Uninstall & Trigger API on Uninstall Attempt (With Device Admin Rights)

Awesome question! Since you already hold Device Admin privileges, you’re totally able to lock down the uninstall option for your app (the grayed-out button in Settings → Apps → App Info) and fire off your API when a user tries to uninstall it. Here’s a step-by-step breakdown of how to make this work:

1. Use Device Admin’s Built-in Uninstall Block

First off, when your app is registered as a Device Admin, the system will automatically gray out the "Uninstall" button in App Info by default. Users can only uninstall after removing your app’s admin status — and that’s exactly where we’ll hook in our API call.

2. Hook Into the Admin Disable Request

You’ll need to extend the DeviceAdminReceiver class and override the onDisableRequested() method. This method gets called the second a user tries to revoke your app’s admin rights (the first step to uninstalling). Here’s a concrete code example (Kotlin, adaptable to Java):

class MyDeviceAdminReceiver : DeviceAdminReceiver() {

    override fun onDisableRequested(context: Context, intent: Intent): CharSequence? {
        // Fire your API call here — triggers when user tries to disable admin (uninstall prerequisite)
        logUninstallAttemptToAPI()
        
        // Return a message to show users explaining why uninstall is blocked
        return "This app can't be uninstalled directly. Please reach out to your admin for assistance."
    }

    private fun logUninstallAttemptToAPI() {
        // Add your actual API call logic here (use Retrofit, Volley, coroutines, etc.)
        // Example with coroutines:
        // CoroutineScope(Dispatchers.IO).launch {
        //     try {
        //         val apiService = RetrofitClient.getApiService()
        //         apiService.logUninstallAttempt()
        //     } catch (e: Exception) {
        //         // Handle API call failure (log it, retry later, etc.)
        //     }
        // }
    }
}

3. Set Up Manifest & Device Admin Policies Correctly

Don’t forget to declare your receiver in the AndroidManifest.xml and enable the right policies to block uninstalls:

Manifest Declaration

<receiver
    android:name=".MyDeviceAdminReceiver"
    android:permission="android.permission.BIND_DEVICE_ADMIN">
    <meta-data
        android:name="android.app.device_admin"
        android:resource="@xml/device_admin_policy" />
    <intent-filter>
        <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
    </intent-filter>
</receiver>

Device Admin Policy XML (res/xml/device_admin_policy.xml)

Include the disable-uninstall-packages policy to formally block uninstalls:

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-policies>
        <disable-uninstall-packages />
        <!-- Add any other device admin policies your app needs here -->
    </uses-policies>
</device-admin>

4. Quick Tips to Avoid Headaches

  • Async API Calls: Always run your API call in a background thread (coroutines, AsyncTask, etc.) — never block the UI while the request processes.
  • User Clarity: The message returned from onDisableRequested() pops up in a system dialog, so make it clear and helpful for users.
  • Test Thoroughly: Walk through the flow yourself: try to uninstall via App Info, confirm the dialog appears, and verify your API gets triggered correctly.

内容的提问来源于stack exchange,提问作者Dinesh B

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 17:40:16