0

这是我下面的文件浏览器代码,当显示来自 sd 卡的文件时,我将多个 apk 文件放在 sd 卡中,但是此代码仅显示 sd 卡中的 apk 文件,当单击 ido 时未安装?请帮我。我想运行在 listview 上显示但未运行的 apk 文件如何修改此代码以运行选定的文件?

         public class MainActivity extends ListActivity {

 private List<String> item = null;
 private List<String> path = null;
 private String root;
private TextView myPath;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myPath = (TextView)findViewById(R.id.path);

    root = Environment.getExternalStorageDirectory().getPath();

    getDir(root);
}

private void getDir(String dirPath)
{
 myPath.setText("Location: " + dirPath);
 item = new ArrayList<String>();
 path = new ArrayList<String>();
 File f = new File(dirPath);
 File[] files = f.listFiles();

 if(!dirPath.equals(root))
 {
  item.add(root);
  path.add(root);
  item.add("../");
  path.add(f.getParent()); 
 }

 for(int i=0; i < files.length; i++)
 {
  File file = files[i];

  if(!file.isHidden() && file.canRead()){
   path.add(file.getPath());
      if(file.isDirectory()){
       item.add(file.getName() + "/");
      }else{
       item.add(file.getName());
      }
  } 
 }

  ArrayAdapter<String> fileList =
   new ArrayAdapter<String>(this, R.layout.row, item);
  setListAdapter(fileList); 
  }

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
File file = new File(path.get(position));

 if (file.isDirectory())
 {
 if(file.canRead()){
  getDir(path.get(position));
 }else{
  new AlertDialog.Builder(this)
 .setIcon(R.drawable.ic_launcher)
 .setTitle("[" + file.getName() + "] folder can't be read!")
 .setPositiveButton("OK", null).show(); 
} 
}else {
new AlertDialog.Builder(this)
 .setIcon(R.drawable.ic_launcher)
 .setTitle("[" + file.getName() + "]")
 .setPositiveButton("OK", null).show();

}
}

}
4

1 回答 1

0

实现一个 ListView(按照这个TUTORIAL)。

然后实现一个监听器来处理对项目的点击( listView.onItemClickListener(); )

然后使用下面的sniper进行安装:

Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("file:///path/to/your.apk"))
.setType("application/vnd.android.package-archive");
startActivity(promptInstall); 

参考:https ://stackoverflow.com/a/4604922/847818

你必须一步一步来。

于 2013-06-14T12:46:13.363 回答