install / uninstall APKs programmatically (PackageManager vs Intents)

后端 未结 10 2021
时光取名叫无心
时光取名叫无心 2020-11-22 13:53

My application installs other applications, and it needs to keep track of what applications it has installed. Of course, this could be achieved by simply keeping a list of i

相关标签:
10条回答
  • 2020-11-22 14:15

    The only way to access those methods is through reflection. You can get a handle on a PackageManager object by calling getApplicationContext().getPackageManager() and using reflection access these methods. Checkout this tutorial.

    0 讨论(0)
  • 2020-11-22 14:16

    Android P+ requires this permission in AndroidManifest.xml

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

    Then:

    Intent intent = new Intent(Intent.ACTION_DELETE);
    intent.setData(Uri.parse("package:com.example.mypackage"));
    startActivity(intent);
    

    to uninstall. Seems easier...

    0 讨论(0)
  • 2020-11-22 14:16

    If you're using Kotlin, API 14+, and just wish to show uninstall dialog for your app:

    startActivity(Intent(Intent.ACTION_UNINSTALL_PACKAGE).apply {
        data = Uri.parse("package:$packageName")
    })
    

    You can change packageName to any other package name if you want to prompt the user to uninstall another app on the device

    0 讨论(0)
  • 2020-11-22 14:19

    According to Froyo source code, the Intent.EXTRA_INSTALLER_PACKAGE_NAME extra key is queried for the installer package name in the PackageInstallerActivity.

    0 讨论(0)
  • 2020-11-22 14:21

    API level 14 introduced two new actions: ACTION_INSTALL_PACKAGE and ACTION_UNINSTALL_PACKAGE. Those actions allow you to pass EXTRA_RETURN_RESULT boolean extra to get an (un)installation result notification.

    Example code for invoking the uninstall dialog:

    String app_pkg_name = "com.example.app";
    int UNINSTALL_REQUEST_CODE = 1;
    
    Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);  
    intent.setData(Uri.parse("package:" + app_pkg_name));  
    intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
    startActivityForResult(intent, UNINSTALL_REQUEST_CODE);
    

    And receive the notification in your Activity#onActivityResult method:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == UNINSTALL_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                Log.d("TAG", "onActivityResult: user accepted the (un)install");
            } else if (resultCode == RESULT_CANCELED) {
                Log.d("TAG", "onActivityResult: user canceled the (un)install");
            } else if (resultCode == RESULT_FIRST_USER) {
                Log.d("TAG", "onActivityResult: failed to (un)install");
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 14:27

    This is not currently available to third party applications. Note that even using reflection or other tricks to access installPackage() will not help, because only system applications can use it. (This is because it is the low-level install mechanism, after the permissions have been approved by the user, so it is not safe for regular applications to have access to.)

    Also the installPackage() function arguments have often changed between platform releases, so anything you do trying access it will fail on various other versions of the platform.

    EDIT:

    Also it is worth pointing out that this installerPackage was only added fairly recently to the platform (2.2?) and was originally not actually used for tracking who installed the app -- it is used by the platform to determine who to launch when reporting bugs with the app, for implementing Android Feedback. (This was also one of the times the API method arguments changed.) For at least a long while after it was introduced, Market still didn't use it to track the apps it has installed (and it may very well still not use it), but instead just used this to set the Android Feedback app (which was separate from Market) as the "owner" to take care of feedback.

    0 讨论(0)
提交回复
热议问题