自定义Android应用安装

时间:2011-08-09 22:39:17

标签: android

在测试阶段,我需要能够在不通过Android商店的情况下发送更新。我找到了几个关于此的帖子,但它们似乎都不适合我。文件本身下载很好,但对于两种选择,我都会收到错误:

08-09 16:31:20.411:ERROR / AndroidRuntime(906):android.content.ActivityNotFoundException:找不到处理Intent的Activity {act = android.intent.action.VIEW dat = http:// localhost:4567 typ = application / vnd.android.package-archive}

有什么想法吗?

的Android

// first way
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://localhost:4567"));

// second way
// Intent intent = new Intent(Intent.ACTION_VIEW);
// intent.setDataAndType(Uri.parse("http://localhost:4567"), 
//                      "application/vnd.android.package-archive");
startActivity(intent); 

require 'sinatra'

get "/" do
  content_type "application/vnd.android.package-archive"
  attachment('agatha_v4.0.apk')
  file = File.open("agatha.apk", "rb")
  response.write(file.read)
end

2 个答案:

答案 0 :(得分:3)

Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File
                    (Environment.getExternalStorageDirectory() + "/download/" + "app.apk")), "application/vnd.android.package-archive");
            context.startActivity(intent); 

答案 1 :(得分:1)

以下是Android方面的功能。正如Pyrodante所提到的,你必须先下载文件。我找到的另一个问题是我必须将文件设置为MODE_WORLD_READBALE(MODE_PRIVATE)失败。

final String APP_FILE_NAME = "agatha.apk";
HttpClient http = new DefaultHttpClient();
HttpGet request = new HttpGet("http://10.10.100.113:4567");

try {
    HttpResponse response = http.execute(request);
    InputStream fileStream = response.getEntity().getContent();
        FileOutputStream output = openFileOutput(APP_FILE_NAME, MODE_WORLD_READABLE);

    byte[] buffer = new byte[1024];
    int len = 0;
    while ((len = fileStream.read(buffer)) > 0) {
        output.write(buffer, 0, len);
    }
    fileStream.close();
    output.close();

} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(getFileStreamPath(APP_FILE_NAME)), 
                       "application/vnd.android.package-archive");
    startActivity(intent); 
}