开发者

Update Android application without asking the user

开发者 https://www.devze.com 2023-01-29 02:49 出处:网络
I am building an android application and want to know if there is a way to update this application automatically without requesting the user\'s permission for this...

I am building an android application and want to know if there is a way to update this application automatically without requesting the user's permission for this...

This application will run as a stand alone app in a dedicated tablet running android and one of the desired features is to开发者_Go百科 be able to update this app automatically from the server that the app connects to...

Is there any way to do this ???

Thanks a lot...


The answer you seek is right here:

http://paulononaka.wordpress.com/2011/07/02/how-to-install-a-application-in-background-on-android/

and here:

http://paulononaka.wordpress.com/2011/10/19/apk-with-system-privileges/


It is not normally possible to update an app without the user going through a confirmation dialog, as the user is considered to be the device owner and their active consent required.

However, if it's a dedicated device and you have the ability reflash the firmware you could make a modification to the android platform to permit it.

If the confirmation dialog is okay (and really, why shouldn't it be?), you can just enable third party apps and use the install package intent, or send the browser to a web page hosting your app with the appropriate mime type, etc...


Once it's installed, you can manually go to the market and allow the app to update automatically. If any permissions change, however, the update must be done manually.


It sounds like this is not an application destined for the market? If it is possible to ensure the device is rooted, you can perform silent updates pretty easily.


Yes there is a way for you to acheive self updating of your application. Although, for this answer I will assume that you have full access to the device A.K.A root access.

A quick summary is that you can use the PackageInstaller api combined with the INSTALL_PACKAGES permission to self update your application as long as the new apk is signed with the same key as the currently installed one.

Here are the instructions:

First you must add this permission to your android manifest. <uses-permission android:name="android.permission.INSTALL_PACKAGES" /> Android studio may complain that this is a system permission and won't be granted. Don't worry, since your app is going to be installed to the /system/priv-app folder, it will get this system only permission.

After adding permission you can use the following static method to install packages. All you need to do is provide a url as a String that can be used to access the file, and a Context and the application will be installed.

 public static boolean installPackage(final Context context, final String url)
        throws IOException {
    //Use an async task to run the install package method
    AsyncTask<Void,Void,Void> task = new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... voids) {
            try {
                PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
                PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
                        PackageInstaller.SessionParams.MODE_FULL_INSTALL);

                // set params
                int sessionId = packageInstaller.createSession(params);
                PackageInstaller.Session session = packageInstaller.openSession(sessionId);
                OutputStream out = session.openWrite("COSU", 0, -1);
                //get the input stream from the url
                HttpsURLConnection apkConn = (HttpsURLConnection) new URL(url).openConnection();
                InputStream in = apkConn.getInputStream();
                byte[] buffer = new byte[65536];
                int c;
                while ((c = in.read(buffer)) != -1) {
                    out.write(buffer, 0, c);
                }
                session.fsync(out);
                in.close();
                out.close();
                //you can replace this intent with whatever intent you want to be run when the applicaiton is finished installing
                //I assume you have an activity called InstallComplete
                Intent intent = new Intent(context, InstallComplete.class);
                intent.putExtra("info", "somedata");  // for extra data if needed..
                Random generator = new Random();
                PendingIntent i = PendingIntent.getActivity(context, generator.nextInt(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
                session.commit(i.getIntentSender());
            } catch (Exception ex){
                Log.e("AppStore","Error when installing application. Error is " + ex.getMessage());
            }

            return null;
        }
    };
   task.execute(null,null);
    return true;
}

Note: If the installation fails even though the installer app is located in system/priv-app then ensure that you have signed the app with a release key. Sometimes signing with a debug key will prevent the Install_Packages permission from being granted

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号