更新:上面的代码,因为已被删除包含错误下面是一个工作代码:
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);
}
}
}
更新2: 这个程序简单枚举所有APK文件,并将其写入的,它试图按顺序安装所有此apk文件后文件的阵列举例来说,在我来说,我在把应用程序golddream.apk我的模拟器的SD卡。应用程序是为SDK v 10.我看到在我的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)
Uri.parse(文件[I] .getAbsolutePath(), “应用/ vnd.android.package - 归档”));解析错误,它说删除参数匹配解析字符串。也是变量我是未申报的。它是否必须是类变量或本地? – chandanmahmud 2011-12-26 10:56:34
非常感谢您的回复。这简直是可观的。现在代码中没有错误。是否有可能在模拟器中测试代码? – chandanmahmud 2011-12-26 18:14:46
是的,我已经在我的模拟器上测试过了,因为我没有真正的手机) – Yury 2011-12-26 18:16:03