Installing Apk Programmatically Without Defining The Apk Name
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 not
Solution 1:
Update: The previous code was deleted because contain errors. Here is a working code:
publicclassInstallAPKActivityextendsActivity {
/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ExtFilterapkFilter=newExtFilter("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());
Intentintent=newIntent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file[i]), "application/vnd.android.package-archive");
startActivity(intent);
}
}
classExtFilterimplementsFilenameFilter {
String ext;
publicExtFilter(String ext) {
this.ext = "." + ext;
}
publicbooleanaccept(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:112-21 06:44:39.453:D/InstallApk(14897):FileName:golddream.apk12-21 06:44:39.463:I/ActivityManager(62):Starting:Intent { act=android.intent.action.VIEWdat=file:///mnt/sdcard/golddream.apktyp=application/vnd.android.package-archivecmp=com.android.packageinstaller/.PackageInstallerActivity } frompid1489712-21 06:44:40.073:I/ActivityManager(62):Displayed com.android.packageinstaller/.PackageInstallerActivity:+578ms(total+1s229ms)
Post a Comment for "Installing Apk Programmatically Without Defining The Apk Name"