我创建了此代码来显示手机文件夹中的文件。现在,我的意图是在单击文件夹中的这些文件(apk 文件)之一时安装所选的 apk。这是代码
private List<String> fileList = new ArrayList<String>();
ApplicationInfo ai;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File root = new File(Environment.getExternalStorageDirectory().toString()+File.separator+"MyFolder");
ListDir(root);
}
@SuppressWarnings("deprecation")
protected void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
String appPos = fileList.get(position);
Uri packageUri = Uri.parse("package:"+getApplicationContext().getPackageName());
Intent installIntent = new Intent(Intent.ACTION_VIEW, packageUri);
try {
startActivity(installIntent);
}
catch (ActivityNotFoundException ex) {
final AlertDialog alertDialog1 = new AlertDialog.Builder(RestoreApp.this).create();
alertDialog1.setTitle("Error");
alertDialog1.setMessage("Unable open the selected activity");
alertDialog1.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog1.dismiss();
}
});
alertDialog1.show();
}
}
void ListDir(File f){
File[] files = f.listFiles();
fileList.clear();
for (File file : files){
fileList.add(file.getName());
}
ArrayAdapter<String> directoryList
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, fileList);
setListAdapter(directoryList);
}
}
当我单击一个项目时,显示对话框,这意味着进入 ActivityNotFoundException。为什么?怎么修?