Automatically install Android application

≡放荡痞女 提交于 2021-01-27 17:43:01

问题


I need to deploy and update various enterprise applications to Android devices given to a limited number of users.

These applications are not supposed to be published on Google Play but must be distributed via a separate channel.

What I need to do is an "enterprise package manager" application to automatically check for new apps/updates and automatically trigger installation of new or updated APKs without asking user consent first.

I know that, by design, Android doesn't allow 3rd party applications to interact with installed applications. I also know that rooted phones don't have this problem because you can inject any APK into the device.

  • If I cook a ROM (even based on CWM) with the "enterprise package manager" installed as system application, but without su binary (it's still an enterprise phone...), will that program be able to install new apps automatically?
  • How am I supposed to install an application without asking for consent? I mean, I need a basic code sample and permissions, if required
  • Anyway, do system apps run as root user? I remember so

回答1:


If you want to check for your application which is on somewhere on your server you have to check for Update in every 24 hour once, if there is any update available then it will navigate to the async task where your updated version build will get installed

public void checkforUpdate() {

        /* Get Last Update Time from Preferences */
        SharedPreferences prefs = getPreferences(0);
        lastUpdateTime = prefs.getLong("lastUpdateTime", 0);
        if ((lastUpdateTime + CommonString.timeCheckDuration) < System.currentTimeMillis() && System.currentTimeMillis()>lastUpdateTime) {
            // Asynch task
            new VersionCheckTask().execute();
        }
        else{
            // do nothing
        }
    }

now it will navigate to:

private class VersionCheckTask extends AsyncTask<Void, Void, Void> {
        ProgressDialog progressDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            try {
                progressDialog = new ProgressDialog(Login.this, android.R.style.Theme_Holo_Light_Dialog);
                //progressDialog.setTitle("AppName");
                progressDialog.setMessage("Checking for updates...");
                progressDialog.setCancelable(false);
                progressDialog.setIndeterminate(true);
                progressDialog.show();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        protected Void doInBackground(Void... params) {
            /**
             *  Simulates a background job.
             */
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }

            HashMap<String, String> map = new HashMap<String, String>();
            map.put("build",CommonString.buildVersion);
            map.put("en", CommonString.en);

            responce = CommonFunction.PostRequest("updateCheck", map);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            if (progressDialog != null && progressDialog.isShowing())
                progressDialog.dismiss();
            if(!CommonFunction.isNetworkAvailable()){
                Toast.makeText(ClaimColonyApplication.getAppContext(), CommonString.NO_NETWORK, Toast.LENGTH_SHORT).show();
                return;
            }
            ParseUpdateResponse(responce);
            if(rCodeUpdate == 100 && ApkLink.length() >0){
                new AlertDialog.Builder(Login.this,android.R.style.Theme_Holo_Light_Dialog)
                .setIcon(R.drawable.ic_launcher)
                .setTitle("Update Available")
                .setMessage(""+UpdateMessage)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                         //User clicked OK so do some stuff 
                        new VersionCheckTaskDialog().execute();
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                         //User clicked Cancel 
                        finish();
                    }
                })
                .show();
            }else{
                if(rCodeUpdate == 100){
                    lastUpdateTime = System.currentTimeMillis();
                    SharedPreferences.Editor editor = getPreferences(0).edit();
                    editor.putLong("lastUpdateTime", lastUpdateTime);

                    editor.commit();
                }
            }
            super.onPostExecute(result);
        }
    }

    private class VersionCheckTaskDialog extends AsyncTask<Void, Void, Void> {
        ProgressDialog progressDialogUpdate;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            try {
                progressDialogUpdate = new ProgressDialog(Login.this, android.R.style.Theme_Holo_Light_Dialog);
                //progressDialog.setTitle("AppName");
                progressDialogUpdate.setMessage("Fetching updates...");
                progressDialogUpdate.setCancelable(false);
                progressDialogUpdate.setIndeterminate(true);
                progressDialogUpdate.show();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        protected Void doInBackground(Void... params) {
            /**
             *  Simulates a background job.
             */
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }

            String extStorageDirectory =    Environment.getExternalStorageDirectory().toString();
            File folder = new File(extStorageDirectory, "pdf");
            folder.mkdir();
            File file = new File(folder, "AppName."+"apk");
            try {
                    file.createNewFile();
            } catch (IOException e1) {
                    e1.printStackTrace();
            }
            /**
             * replace url to ApkLink
             */
             //DownloadFile(ApkLink, file);
             DownloadFile("URL", file);

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            if (progressDialogUpdate != null && progressDialogUpdate.isShowing())
                progressDialogUpdate.dismiss();
            if(!CommonFunction.isNetworkAvailable()){
                Toast.makeText(ClaimColonyApplication.getAppContext(), CommonString.NO_NETWORK, Toast.LENGTH_SHORT).show();
                return;
            }
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/pdf/" + "AppName.apk")), "application/vnd.android.package-archive");
                startActivity(intent);
                lastUpdateTime = System.currentTimeMillis();
                SharedPreferences.Editor editor = getPreferences(0).edit();
                editor.putLong("lastUpdateTime", lastUpdateTime);

                editor.commit();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                System.out.println("Exception in start intent for launch app-------: "+e.toString());
                e.printStackTrace();
            } 
            super.onPostExecute(result);
        }
    }

I am checking for update once in 24 hours, if there is any update available then it will show pop up to upgrade your application otherwise will save your last checking time in Preferences. Now this will allow you to update and install your application and this will check for next update after 24 hours, you may need to work on conditions to check for update. Please change name of your .apk file and URL.

You will need following permissions:

<uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Best of luck.



来源:https://stackoverflow.com/questions/13836318/automatically-install-android-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!