how to install apk file programmatically

前端 未结 5 1841
别跟我提以往
别跟我提以往 2020-12-09 13:30

I want to install an apk file from my application.

I have created an app that contains a button, when I click that button then another apk that I have stored in res

5条回答
  •  星月不相逢
    2020-12-09 14:19

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        File DbFile=new File("mnt/sdcard/HelloAndroid.apk");
        if(!(DbFile.exists()))
        {
            try
            {
                int length = 0;
                DbFile.createNewFile();
                InputStream inputStream = this.getAssets().open("HelloAndroid.apk");
                FileOutputStream fOutputStream = new FileOutputStream(DbFile);
                byte[] buffer = new byte[inputStream.available()];
                while ((length = inputStream.read(buffer)) > 0)
                {
                    fOutputStream.write(buffer, 0, length);
                }
                fOutputStream.flush();
                fOutputStream.close();
                inputStream.close();
            }
            catch (Exception ex)
            {
                System.out.println("Error in creating new database at mobile..." + ex);
                ex.printStackTrace();
            }
        }
    
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File("/mnt/sdcard/HelloAndroid.apk")), "application/vnd.android.package-archive");
        startActivity(intent);
    }
    

    Here i have stored my apk file in assets folder. You can try this.

提交回复
热议问题