Wednesday 23 November 2016

Install / Uninstall App from Another App Dynamically


If you want to install a custom app dynamically from your app, then it is quite simple. For this, you need to place APK on a downloadable URL, so that it can be downloaded directly. Then you can access app url and install. Since you want to install app from your app, then you need to download the APK first and then install your custom app. It thus need to do in background. 
Here is the code.

I installed custom app on launch of my app i.e. MainActivity

//Download APK and then Install Custom App
InstallApp customApp= new InstallApp();
customApp.setContext(myContext);
customApp.execute("http://APK-URL/AppName.apk");

Following is the code to download the apk in background then install app.

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class InstallApp extends AsyncTask<String,Void,Void> {

    private Context context;
    private final String TAG = "InstallApp";
    public void setContext(Context context){

        this.context = context;
    }

    @Override
    protected Void doInBackground(String... apk_url) {

        try {

            //URL of the APK, to download
            URL download_url = new URL(apk_url[0]);
            HttpURLConnection c = (HttpURLConnection) download_url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            //Download APK to download folder. Save APK file with CustomApp.apk
            File file = new File(Environment.getExternalStorageDirectory() + "/download/");
            file.mkdirs();
            File downloadFile= new File(file, "CustomApp.apk");
            if(downloadFile.exists()){

                downloadFile.delete();
            }

            FileOutputStream fos = new FileOutputStream(downloadFile);
            InputStream is = c.getInputStream();

            byte[] buffer = new byte[1024];
            int lenght = 0;
            while ((lenght = is.read(buffer)) != -1) {

                fos.write(buffer, 0, lenght);
            }

            fos.close();
            is.close();

            //Install App Intent
            Intent install_intent = new Intent(Intent.ACTION_VIEW);
            install_intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "CustomApp.apk")), "application/vnd.android.package-archive");
            install_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(install_intent);
        } catch (Exception e) {

            Log.e(TAG, e.getMessage());
        }
        return null;
    }
}

Similarly, if you want to uninstall app from another app, then here is code:

 //Uninstall app
Uri packageUri = Uri.parse("package:"+"name of app  package");
//OS >= 14 supports ACTION_UNINSTALL_PACKAGE
//OS < 14 ACTION_DELETE
//Therefore, for all OS, use the following.

Intent appUninstall= new Intent( (Build.VERSION.SDK_INT >= 14)?Intent.ACTION_UNINSTALL_PACKAGE: Intent.ACTION_DELETE, packageUri);

startActivity(appUninstall);


24 comments:

  1. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.

    selenium Training in Chennai

    amazon web services Training in Chennai

    rpa Training in Chennai

    ReplyDelete
  2. This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.

    java training in tambaram | java training in velachery

    java training in omr | oracle training in chennai

    java training in annanagar | java training in chennai

    ReplyDelete
  3. Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..
    python training in pune
    python online training
    python training in OMR

    ReplyDelete
  4. I really like the dear information you offer in your articles. I’m able to bookmark your site and show the kids check out up here generally. Im fairly positive theyre likely to be informed a great deal of new stuff here than anyone
    Blueprism training in tambaram

    Blueprism training in annanagar

    Blueprism training in velachery

    ReplyDelete
  5. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information. 


    angularjs Training in chennai
    angularjs-Training in pune

    angularjs-Training in chennai

    angularjs Training in chennai

    angularjs-Training in tambaram

    ReplyDelete
  6. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us. Do check Angularjs Training in Chennai | Angular 2 Training in Chennai Get trained by an expert who will enrich you with the latest updates.
    Angularjs Training
    Angularjs Training near me
    Angularjs course in Chennai

    ReplyDelete
  7. After reading this web site I am very satisfied simply because this site is providing comprehensive knowledge for you to audience. Thank you to the perform as well as discuss anything incredibly important in my opinion. We loose time waiting for your next article writing in addition to I beg one to get back to pay a visit to our website in

    devops online training

    aws online training

    data science with python online training

    data science online training

    rpa online training

    ReplyDelete
  8. Really great post, I simply unearthed your site and needed to say that I have truly appreciated perusing your blog entries. I want to say thanks for great sharing.
    Microsoft Azure online training
    Selenium online training
    Java online training
    Python online training
    uipath online training

    ReplyDelete
  9. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog.share more details.
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete
  10. Nice Post! Thank you for sharing very good post, it was so Nice to read and useful to improve my knowledge as updated one, keep blogging.
    AWS training in chennai | AWS training in annanagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery


    ReplyDelete
  11. This blog is really amazing to read. The content is very precise. Thanks for sharing it across. The content is very useful and informative. I can understand very easily
    Selenium Training in Chennai

    Selenium Training in Velachery

    Selenium Training in Tambaram

    Selenium Training in Porur

    Selenium Training in Omr

    Selenium Training in Annanagar

    ReplyDelete
  12. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    tally training in chennai

    hadoop training in chennai

    sap training in chennai

    oracle training in chennai

    angular js training in chennai

    ReplyDelete
  13. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.
    IELTS Coaching in chennai

    German Classes in Chennai

    GRE Coaching Classes in Chennai

    TOEFL Coaching in Chennai

    Spoken english classes in chennai | Communication training

    ReplyDelete
  14. Title:
    Study Oracle Course in Chennai | Infycle Technologies

    Description:
    Did you want to set your career towards Oracle? Then Infycle is with you to make this into reality. Infycle Technologies gives the combined and best Oracle course in Chennai, which offers various stages of Oracle such as Oracle PL/SQL, Oracle DBA, etc., along with 100% hands-on training guided by experienced trainers in the field. Once after the training, the interviews will be arranged in the MNC's and firms for the placement. To have the Oracle with the best future, call 7502633633 and make this happen for your happy life.

    Best training in Chennai

    ReplyDelete
  15. Title:
    Top AWS Course in Chennai | Infycle Technologies

    Description:
    Learn Amazon Web Services for excellent job opportunities from Infycle Technologies, the best AWS training center in Chennai. Infycle Technologies gives the most trustworthy AWS course in Chennai, with full hands-on practical training from professional trainers in the field. Along with that, the placement interviews will be arranged for the candidates, so that, they can meet the job interviews without missing them. To transform your career to the next level, call 7502633633 to Infycle Technologies and grab a free demo to know more

    best training institute in chennai

    ReplyDelete
  16. Grab Data Science Certification in Chennai for skyrocketing your career with Infycle Technologies, the best Software Training & Placement institutes in and around Chennai. In addition to the Certification, Infycle also gives the best placement training for personality tests, interview preparation, and mock interviews for leveling up the candidate's grades to a professional level.

    ReplyDelete
  17. Infycle Technologies, the top software training institute and placement center in Chennai offers the Best Digital Marketing course in Chennai for freshers, students, and tech professionals at the best offers. In addition to Digital Marketing, other in-demand courses such as DevOps, Data Science, Python, Selenium, Big Data, Java, Power BI, Oracle will also be trained with 100% practical classes. After the completion of training, the trainees will be sent for placement interviews in the top MNC's. Call 7504633633 to get more info and a free demo.

    ReplyDelete
  18. Infycle Technologies offers the best Python training in Chennai for tech professionals and freshers with New year offers. In addition to the Python Training Course, Infycle also offers other technical courses such as Data Science, Oracle, Java, Power BI, Digital Marketing, Big Data, etc., which will be trained with complete practical classes. Dial 7504633633 to get more info and a free demo.

    ReplyDelete