简体   繁体   中英

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 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 is clicked? 时得到的意图(或任何事情,我认为它是一个意图但不确定)?

Also
If they can do it, then there is some way. Please help.

I have found a way.

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

  2. 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.

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.

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.

try catching below intent in the broadcast reciver and fire up ur activity warning activity or processing what u want to carry on.

"android.intent.action.UNINSTALL_PACKAGE"

this intent will not given or broadcasted to same app which is about to get uninstalled

Looks like this has gone through a lot of changes. The default broadcast of PACKAGE_REMOVED doesn't work as intended. I came across this discussion, did not actually implement it, but people say it has worked for them. Do give this a try

https://groups.google.com/forum/?fromgroups=#!topic/android-developers/aX5-fMbdPR8

try this code

Please try to get the top activity in the task via ActivityManager, and check if it is the uninstall activity.

 ComponentName topActivity = mActivityManager.getRunningTasks(1).get(0).topActivity;
String packageName = topActivity.getPackageName();
String className = topActivity.getClassName();
Log.v(TAG, "packageName" + packageName);
Log.v(TAG, "className" + className);

if ("com.android.packageinstaller".equals(packageName)
    && "com.android.packageinstaller.UninstallerActivity".equals(className)) {
//Do anything you want here
}

như lone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM