Android:安装非市场应用

时间:2012-06-21 06:31:57

标签: android install google-play

我正在尝试在Android中安装非市场应用。它被下载到:file:///mnt/sdcard/Download/App.apk

我在做的是:

Intent promptInstall = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(uriString)).setType("application/vnd.android.package-archive");
startActivity(promptInstall);

其中uriString是:file:///mnt/sdcard/Download/App.apk

但有一个例外:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW typ=application/vnd.android.package-archive }

修改

我正在使用DownloadManager下载应用程序:

DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request req = new DownloadManager.Request(Uri.parse(MY_PATH_TP_APK));
Request req.setTitle("Test")
       .setDescription("Something useful.")
       .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "App.apk");
long enqueue = dm.enqueue(req);

下载后:

_receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
            Query query = new Query();
            query.setFilterById(enqueue);
            Cursor c = dm.query(query);
            if (c.moveToFirst()) {
                int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
                    String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                    Intent promptInstall = new  Intent(Intent.ACTION_VIEW)
                    .setData(Uri.parse(uriString))
                    .setType("application/vnd.android.package-archive");
                    startActivity(promptInstall);
                }
            }
        }
    }
};

如何解决?

2 个答案:

答案 0 :(得分:3)

我在我的一个应用程序中使用此代码并且工作正常:

Uri fileUri = Uri.fromFile(new File("/mnt/sdcard/Download", "App.apk"));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(fileUri, "application/vnd.android.package-archive");
startActivity(intent);

也许你的代码有一个斜杠太多了(file:/// mnt而不是file:// mnt(这里不确定))。

答案 1 :(得分:1)

替换

String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));

使用:

c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));