How to know that an app is going to be uninstalled in android?

I want to implement something like AppLock application is doing.
If in its settings it is set to lock applications uninstall/install, then while uninstalling any app (precisely on clicking the Uninstall button) a lock screen comes which asks for a password/pattern.
Only after entering the password user is allowed to uninstall the app.

What's that intent(or anything, I assume it to be an intent but not sure) that one get when Uninstall button is clicked?

Also I dont want to make it device admin, as the app I mentioned does require to be device admin.
If they can do it, then there is some way. Please help.


I have found a way.

  • When you go to Settings -> Manage Apps -> Click on any app .
    you get a broadcast with name of the package in extras.

  • When you click on the Uninstall button on that screen, an activity is opened always name com.android.uninstaller.UnistallerActivity .

  • So the solution to this problem is a combined way of 1 and 2 steps mentioned above.

    When ever you get the intent mentioned in first step and the extras contain the package name of your app start an activity watcher using PackageManager by which you get the top most visible activity and its package.
    So now if uninstaller activity is launched then you can be sure that user wants to uninstall your app.

    There after you can do necessary action to stop him to do that.


    You can intercept the intent for your application uninstall. Simply put the following code in your manifest file:

    <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.DELETE_PACKAGES" />
    <uses-permission android:name="android.permission.CLEAR_APP_CACHE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CLEAR_APP_USER_DATA" />
    
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".UninstallActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <action android:name="android.intent.action.DELETE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="package" android:pathPattern="com.package.yourapp" />
            </intent-filter>            
        </activity>
    </application>
    

    After this you can somehow process that your application is going to be deleted, and call the package manager uninstaller.


    Make it a device administrator instead. That will automatically block the user from uninstalling it. If the user tries to deactivate it from the Security > Device Administrator list in order to uninstall it, your app can then ask for the password.

    链接地址: http://www.djcxy.com/p/41882.html

    上一篇: Cygwin和MinGW有什么区别?

    下一篇: 如何知道应用程序将在android中被卸载?