0

我正在開發一個管理我的android擴展文件的cordava插件。擴展文件的讀取工作正常,但我在下載文件時遇到問題。onServiceConnected不叫

這裏是我的插件:

public class ExpansionFileReader extends CordovaPlugin implements IDownloaderClient { 

    private final String MEDIA_FOLDER_NAME = "mediafiles"; 
    private final String MAIN_EXPANSION = "main_expansion"; 
    private final String URL = "url"; 
    private final String TAG = "expansionFileReader"; 

    private Context context; 
    private CallbackContext callbackContext; 


    public void initialize(CordovaInterface cordova, CordovaWebView webView) { 
     super.initialize(cordova, webView); 
    } 

    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { 
     JSONObject callbackValue = new JSONObject(); 
     context = this.cordova.getActivity().getApplicationContext(); 
     if(action.equalsIgnoreCase("downloadExpansionFileIfNecessary")) { 
      this.callbackContext = callbackContext; 
      downloadExpansionFileIfNecessary(); 

     } else if(action.equalsIgnoreCase("getFile")){ 

      [getFileAction] 

     } 

     return true; 
    } 

    private void downloadExpansionFileIfNecessary() { 

     if (!expansionFilesDelivered()) { 
      Log.d(TAG, "NO EXPANSION FILE FOUND"); 
      try { 
       Intent launchIntent = this.cordova.getActivity().getIntent(); 
       Intent intentToLaunchThisActivityFromNotification = new Intent((context), context.getClass()); 
       intentToLaunchThisActivityFromNotification.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       intentToLaunchThisActivityFromNotification.setAction(launchIntent.getAction()); 

       if (launchIntent.getCategories() != null) { 
        for (String category : launchIntent.getCategories()) { 
         intentToLaunchThisActivityFromNotification.addCategory(category); 
        } 
       } 

       PendingIntent pendingIntent = PendingIntent.getActivity((context), 0, intentToLaunchThisActivityFromNotification, PendingIntent.FLAG_UPDATE_CURRENT); 
       // Request to start the download 
       Log.d(TAG, "REQUEST TO START DOWNLOAD"); 
       int startResult = DownloaderClientMarshaller.startDownloadServiceIfRequired(context, pendingIntent, DownloaderService.class); 

       if (startResult != DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED) { 

        mDownloaderClientStub = DownloaderClientMarshaller.CreateStub(this, DownloaderService.class); 
        if (null != mDownloaderClientStub) { 
         mDownloaderClientStub.connect(context); 
        } 

        return; 
       } 
       else { 
        Log.d(TAG, "DOWNLOAD NOT NECESSARY"); 
        callbackContext.success(); 
       } 
      } catch (PackageManager.NameNotFoundException e) { 
       Log.e(TAG, "Cannot find package!", e); 
      } 
     } else { 
      validateXAPKZipFiles(); 
     } 
    } 


    ///////////////////////////// 
    // INIT DOWNLOADS 
    ///////////////////////////// 

    private IDownloaderService mRemoteService; 
    private IStub mDownloaderClientStub; 
    private int mState; 
    private boolean mCancelValidation; 

    // region Expansion Downloader 
    private static class XAPKFile { 
     public final boolean mIsMain; 
     public final int mFileVersion; 
     public final long mFileSize; 

     XAPKFile(boolean isMain, int fileVersion, long fileSize) { 
      mIsMain = isMain; 
      mFileVersion = fileVersion; 
      mFileSize = fileSize; 
     } 
    } 

    private static final XAPKFile[] xAPKS = { 
      new XAPKFile(
        true, // true signifies a main file 
        1000006, // the version of the APK that the file was uploaded against 
        443975466L // the length of the file in bytes 
      ) 
    }; 
    static private final float SMOOTHING_FACTOR = 0.005f; 

    @Override 
    public void onStart() { 
     Log.d(TAG, "ON START"); 
     if (null != mDownloaderClientStub) { 
      mDownloaderClientStub.connect(context); 
     } 
     super.onStart(); 
    } 

    @Override 
    public void onStop() { 
     Log.d(TAG, "ON STOP"); 
     if (null != mDownloaderClientStub) { 
      mDownloaderClientStub.disconnect(context); 
     } 
     super.onStop(); 
    } 

    @Override 
    public void onServiceConnected(Messenger m) { 
     Log.d(TAG, "ON SERVICE CONNECTED"); 
     mRemoteService = DownloaderServiceMarshaller.CreateProxy(m); 
     mRemoteService.onClientUpdated(mDownloaderClientStub.getMessenger()); 
    } 

    @Override 
    public void onDownloadStateChanged(int newState) { 
     Log.d(TAG, "ON DOWNLOAD STATE CHANGED: " + newState); 
     setState(newState); 
     boolean showDashboard = true; 
     boolean showCellMessage = false; 
     boolean paused; 
     boolean indeterminate; 
     switch (newState) { 
      case IDownloaderClient.STATE_IDLE: 
       // STATE_IDLE means the service is listening, so it's 
       // safe to start making calls via mRemoteService. 
       paused = false; 
       indeterminate = true; 
       break; 
      case IDownloaderClient.STATE_CONNECTING: 
      case IDownloaderClient.STATE_FETCHING_URL: 
       showDashboard = true; 
       paused = false; 
       indeterminate = true; 
       break; 
      case IDownloaderClient.STATE_DOWNLOADING: 
       paused = false; 
       showDashboard = true; 
       indeterminate = false; 
       break; 

      case IDownloaderClient.STATE_FAILED_CANCELED: 
      case IDownloaderClient.STATE_FAILED: 
      case IDownloaderClient.STATE_FAILED_FETCHING_URL: 
      case IDownloaderClient.STATE_FAILED_UNLICENSED: 
       paused = true; 
       showDashboard = false; 
       indeterminate = false; 
       break; 
      case IDownloaderClient.STATE_PAUSED_NEED_CELLULAR_PERMISSION: 
      case IDownloaderClient.STATE_PAUSED_WIFI_DISABLED_NEED_CELLULAR_PERMISSION: 
       showDashboard = false; 
       paused = true; 
       indeterminate = false; 
       showCellMessage = true; 
       break; 

      case IDownloaderClient.STATE_PAUSED_BY_REQUEST: 
       paused = true; 
       indeterminate = false; 
       break; 
      case IDownloaderClient.STATE_PAUSED_ROAMING: 
      case IDownloaderClient.STATE_PAUSED_SDCARD_UNAVAILABLE: 
       paused = true; 
       indeterminate = false; 
       break; 
      case IDownloaderClient.STATE_COMPLETED: 
       showDashboard = false; 
       paused = false; 
       indeterminate = false; 
       validateXAPKZipFiles(); 
       return; 
      default: 
       paused = true; 
       indeterminate = true; 
       showDashboard = true; 
     }    
    } 


    @Override 
    public void onDownloadProgress(DownloadProgressInfo progress) { 
     Log.d(TAG, "ON DOWNLOAD PROGESS: " + progress);    
    } 


    boolean expansionFilesDelivered() { 
     Log.d(TAG, "IF EXPANSION FILE IS DELIVERED"); 
     for (XAPKFile xf : xAPKS) { 
      String fileName = Helpers.getExpansionAPKFileName(context, xf.mIsMain, xf.mFileVersion); 
      if (!Helpers.doesFileExist(context, fileName, xf.mFileSize, false)) { 
       Log.d(TAG, "EXPANSION FILE DOESN'T EXIST"); 
       return false; 
      } 
     } 
     Log.d(TAG, "EXPANSION FILE EXIST"); 
     return true; 
    } 

} 

我的問題是onServiceConnected是從來沒有所謂的,雖然我打電話mDownloaderClientStub.connect(context);

有誰知道它爲什麼不叫?我也沒有得到任何錯誤。

回答

0

無法建立服務連接,因爲您將不正確的Class對象傳遞給擴展庫方法。

您應該DownloaderService.classMyDownloaderService.class或任何類,你用它來擴展基DownloaderService在調用的參數更改爲DownloaderClientMarshaller。另外,請確保您的服務是在您的應用清單中定義的。

// use the correct service class! 
int startResult = DownloaderClientMarshaller.startDownloadServiceIfRequired(context, pendingIntent, MyDownloaderService.class); 

// when creating a stub also 
mDownloaderClientStub = DownloaderClientMarshaller.CreateStub(this, MyDownloaderService.class); 

我推薦使用更新的下載檔案庫包含在Better APK Expansion包。它解決了這個問題和其他問題,並且還提供了簡化的API,從而最大限度地減少了在腳中拍攝自己的機會。