Domanda

I need to install apk file without any user prompt like Google PlayStore does. I know this question has been asked many times but still i didn't found any solvable answer. I am just installing app from my sdcard and i tried following way,

public void getInstall(Context context) {


        File file = new File(Environment.getExternalStorageDirectory()
                + "/Demo.apk");
        Uri mPackageURI = Uri.fromFile(file);

        ApplicationManager appMgr = null;

        try {
            appMgr = new ApplicationManager(context);
        } catch (SecurityException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (NoSuchMethodException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        int installFlags = 1;
        PackageManager pm = context.getPackageManager();

        Method method = null;

        try {
            method = pm.getClass().getMethod(
                    "installPackage",
                    new Class[] { android.net.Uri.class,
                            android.content.pm.IPackageInstallObserver.class,
                            Integer.TYPE, String.class });
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String installerPackageName = "com.myapp.demo";
        PackageInstallObserver observer = appMgr.new PackageInstallObserver();

        try {

            method.invoke(pm, new Object[] { mPackageURI, observer,
                    installFlags, installerPackageName });

        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

But I am getting error Neither user 10057 nor current process has android.permission.INSTALL_PACKAGES even though i added this permission in my manifest file.

My device is not rooted and i want it to do without rooting , In many answers i found that it is not possible as per security reason they are not allowing but if it is possible with PlayStore and AndroidLost app then why not with us?

Please i need help if it is possible !

Thank you,

È stato utile?

Soluzione

My device is not rooted and i want it to do without rooting

This is not possible on Android for third party apps. Google Play is able to do it because it is installed to /system/app instead of /data/app. If you can convince any OEM or ROM maker to have your app on the system partition, you can utilize the INSTALL_PACKAGES permission, as that is only granted to system apps.

In any case, this is an Android anti pattern. If you were allowed to arbitrarily install apps without user consent, it would be a serious security hole, and the user would no longer be in charge of the device.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top