installing apk programmatically without defining the apk name

后端 未结 1 1277
悲&欢浪女
悲&欢浪女 2021-01-26 08:28

I am trying to install apk programmatically from SD card without mentioning the name of the apk. What I can do now is I can get installed the apk I named in my code. But it is n

相关标签:
1条回答
  • 2021-01-26 08:59

    Update: The previous code was deleted because contain errors. Here is a working code:

    public class InstallAPKActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            ExtFilter apkFilter = new ExtFilter("apk");
            File file[] = Environment.getExternalStorageDirectory().listFiles(apkFilter);
            Log.d("InstallApk", "Filter applied. Size: "+ file.length);
    
            for (int i=0; i < file.length; i++)
            {
                Log.d("InstallApk", "FileName:" + file[i].getName());
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(file[i]), "application/vnd.android.package-archive");
                startActivity(intent);
            }
    
    
    
    
    
        }
    
        class ExtFilter implements FilenameFilter { 
            String ext; 
            public ExtFilter(String ext) { 
                this.ext = "." + ext; 
            } 
            public boolean accept(File dir, String name) { 
                return name.endsWith(ext); 
            }
        }
    }
    

    Update 2: This program simply enumerates all apk file and writes them to the array of File. After that it tries to install all this apk files sequentially. For instance, in my case I put application golddream.apk on a sdcard of my emulator. The application is developed for SDK v 10. I see the following output in my logcat:

    12-21 06:44:39.453: D/InstallApk(14897): Filter applied. Size: 1
    12-21 06:44:39.453: D/InstallApk(14897): FileName:golddream.apk
    12-21 06:44:39.463: I/ActivityManager(62): Starting: Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/golddream.apk typ=application/vnd.android.package-archive cmp=com.android.packageinstaller/.PackageInstallerActivity } from pid 14897
    12-21 06:44:40.073: I/ActivityManager(62): Displayed com.android.packageinstaller/.PackageInstallerActivity: +578ms (total +1s229ms)
    
    0 讨论(0)
提交回复
热议问题