2012-06-21 233 views
0

我想在Android中安裝非市場應用程序。它是在下載:file:///mnt/sdcard/Download/App.apkAndroid:安裝非市場應用程序

我正在做的事情是:

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 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); 
       } 
      } 
     } 
    } 
}; 

如何解決?

+0

的可能的複製[機器人:安裝的apk編程](http://stackoverflow.com/questions/4967669/android-install-apk-以編程方式) – jk2K

回答

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); 

也許你的代碼中有一個斜線太多(文件:/// MNT的不是文件:// MNT(不知道這裏))。

+0

它打開一個對話框:使用nimbuzz,gps,facebook完成動作.... –

+0

您是使用股票Android還是三星,HTC或任何具有自定義表面的手機?也許他們已經改變了軟件包安裝程序的意圖? – Tim

+0

我正在使用Sony ericsson,xperia arc with android 2.3.4 –

1

取代

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

與:

c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME)); 
相關問題