0

私はAndroid用の拡張ファイルを管理するcordavaプラグインを開発しています。拡張ファイルの読み込みは機能しますが、ダウンロードに問題があります。ここでonServiceConnectedが呼び出されていません

は私のプラグインです:私の問題は、私はmDownloaderClientStub.connect(context);

呼び出しが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; 
    } 

} 

それが呼び出されない理由を誰もが知っていますか?私も間違いがないよ。

答えて

0

正しくないClassオブジェクトを拡張ライブラリメソッドに渡すため、サービス接続を確立できませんでした。

DownloaderClientMarshallerの呼び出しで引数をDownloaderService.classからMyDownloaderService.classに変更するか、ベースを拡張するために使用するクラスをDownloaderServiceに変更する必要があります。また、サービスがアプリケーションのマニフェストに定義されていることを確認してください。

// 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パッケージに含まれている最新のDownloaderライブラリを使用することをおすすめします。この問題を修正し、簡単なAPIを提供し、足で自分を撃つ機会を最小限に抑えます。

関連する問題