2016-05-16 134 views
9

在我的一個用例中,開始時的應用程序聯繫服務器並下載相同應用程序的更新。以編程方式安裝在棉花糖上的APK安裝

對於下載,我正在使用Android的DownloadManager類。 然後使用以下代碼嘗試在onReceive()中創建應用安裝程序意圖,即在APK成功下載之後。

@Override 
public void onReceive(Context context, Intent intent) { 

    //check if the broadcast message is for our Enqueued download 
    long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); 
    if (downloadReference == referenceId) { 
     try { 
      Log.v("", "Downloading of the new app version complete"); 

      String filepath = Environment.getExternalStorageDirectory() + "/" + 
        Environment.DIRECTORY_DOWNLOADS + "/" + mLatestVersionFileName; 

      //start the installation of the latest version 
      Uri uri = downloadManager.getUriForDownloadedFile(downloadReference); 

      //downloadManager.getUriForDownloadedFile(downloadReference) 
      Uri fileLoc = Uri.fromFile(new File(filepath)); 

      Intent promptInstall = new Intent(Intent.ACTION_VIEW); 
      promptInstall.setDataAndType(uri, "application/vnd.android.package-archive"); 
      promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(promptInstall); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 
} 

該代碼適用於前棉花糖設備和用戶獲取屏幕安裝APK。

這不適用於棉花糖設備。

我得到以下的棉花糖例外:

android.content.ActivityNotFoundException:無活動處理意向{行動= android.intent.action.VIEW DAT =內容://下載/ my_downloads/287典型應用=/vnd.android.package歸檔FLG = 0x10000000的}

我曾嘗試以下操作:

  1. 使用三月的確切PackageInstaller shmallow (com.google.android.packageinstaller/com.android.packageinstaller.PackageInstallerActivity)組件名稱作爲Intent的一部分。
  2. 傳遞fileLoc而不是uri。
promptInstall.setDataAndType(uri, "application/vnd.android.package-archive"); 

這將導致 '解析錯誤'。

理想的意圖是由Android操作系統解決。我想這不是一個大問題,我可能在創建Intent的時候錯過了一些東西。

回答

3

嘗試了這一點

public void onReceive(Context context, Intent intent) { 
       String action = intent.getAction(); 
       if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) { 
        long downloadId = intent.getLongExtra(
          DownloadManager.EXTRA_DOWNLOAD_ID, 0); 
        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)) { 

          ImageView view = (ImageView) findViewById(R.id.imageView1); 
          String uriString = c 
            .getString(c 
              .getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); 

          //start the installation of the latest version 
          Uri uri = Uri.parse(uriString); 
          Intent promptInstall = new Intent(Intent.ACTION_VIEW); 
          promptInstall.setDataAndType(uri, "application/vnd.android.package-archive"); 
          promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
          mContext.startActivity(promptInstall); 
         } 
        } 
       } 
      } 
3

你可以試試這個:

@Override 
public void onReceive(Context context, Intent intent) { 

String action = intent.getAction(); 

     if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) { 

      DownloadManager.Query query = new DownloadManager.Query(); 
      query.setFilterById(yourDownloadID); 
     DownloadManager dm = (DownloadManager)getSystemService(DOWNLOAD_SERVICE); 
      Cursor downloadResult = dm.query(query); 

      if (downloadResult.moveToFirst()) { 
       int statusColumnIndex = downloadResult.getColumnIndex(DownloadManager.COLUMN_STATUS); 
       int status = downloadResult.getInt(statusColumnIndex); 

       if (status == DownloadManager.STATUS_SUCCESSFUL) { 
        //download completed successfully 
        int localFileNameId = downloadResult.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME); 

        String downloadPathFile = downloadResult.getString(localFileNameId); 

     Intent intent = new Intent(Intent.ACTION_VIEW); 
        intent.setDataAndType(Uri.fromFile(new File(downloadPathFile)), "application/vnd.android.package-archive"); 
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        startActivity(intent); 
}