Delete my application programmatically (Android)

前端 未结 3 1743
暗喜
暗喜 2020-12-09 11:08

I want to uninstall my application on button click. For this I am using following code.

Uri packageURI = Uri.parse(\"package:\"+packageName);
Intent uninstal         


        
相关标签:
3条回答
  • 2020-12-09 11:40

    You should first look into the Android native PackageInstaller. I would recommendating you to update all the code you use.


    Next step is to inspect PackageInstaller which is an normal class. You will find that uninstall function there. The bad news is that this needs Manifest.permission.DELETE_PACKAGES permission and its only granted to system apps. This means that this is not available directly to other developers. But we can access it using device owner permission.


    This requires:

    • Android 6.0 or newer
    • Device owner permission to uninstall the package

    Generally the DELETE_PACKAGES permission says:

    Allows an application to delete packages.

    Not for use by third-party applications.

    Once your app gets the device owner permission, you can uninstall an package like this:

    String appPackage = "com.your.app.package";
    Intent intent = new Intent(getApplicationContext(), 
    getApplicationContext().getClass()); //getActivity() is undefined!
    PendingIntent sender = PendingIntent.getActivity(getActivity(), 0, intent, 0);
    PackageInstaller mPackageInstaller = 
    getActivity().getPackageManager().getPackageInstaller();
    mPackageInstaller.uninstall(appPackage, sender.getIntentSender());
    

    The code used available here:

    PackageInstaller "Silent install and uninstall of apps by Device Owner” - Android M Preview

    0 讨论(0)
  • 2020-12-09 11:53

    Uninstalling without user confirmation is not allowed to 3rd party applications.

    As xDragonZ points out, a root process can crudely do this by literally removing the directory and leaving the package manager to deal with the loss, but that's not a very widely deployable solution, since AFAIK no devices ship with that capability for apps to run their own root helper process - that's a risky aftermarket modification.

    0 讨论(0)
  • 2020-12-09 12:02

    Yes it is possible to uninstall a package in Android. Moreover you can also skip asking user to press OK button on uninstall screen. You can do it by using Accessibility service in Android.

    public class MyAccessibilityService extends AccessibilityService {
        private static final String TAG = MyAccessibilityService.class
                .getSimpleName();
    
        @Override
        public void onAccessibilityEvent(AccessibilityEvent event) {
            Log.i(TAG, "ACC::onAccessibilityEvent: " + event.getEventType());
    
            //TYPE_WINDOW_STATE_CHANGED == 32
            if (AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED == event
                    .getEventType()) {
                AccessibilityNodeInfo nodeInfo = event.getSource();
                Log.i(TAG, "ACC::onAccessibilityEvent: nodeInfo=" + nodeInfo);
                if (nodeInfo == null) {
                    return;
                }
    
                List<AccessibilityNodeInfo> list = nodeInfo
                        .findAccessibilityNodeInfosByViewId("com.android.settings:id/left_button");
                for (AccessibilityNodeInfo node : list) {
                    Log.i(TAG, "ACC::onAccessibilityEvent: left_button " + node);
                    node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                }
    
                list = nodeInfo
                        .findAccessibilityNodeInfosByViewId("android:id/button1");
                for (AccessibilityNodeInfo node : list) {
                    Log.i(TAG, "ACC::onAccessibilityEvent: button1 " + node);
                    node.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                }
            }
    
        }
    
        @Override
        public void onServiceConnected() {
            Log.i(TAG, "ACC::onServiceConnected: ");
        }
    
        @Override
        public void onInterrupt() {
            // TODO Auto-generated method stub
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题