cordovaプラグイン呼び出しactivityと結果戻り

1997 ワード

一部のプラグインでは、別のactivityを開いてcordovaのactivityに戻り、結果を得る必要があります.
次の2つのケースがあります.

一、直接プラグインクラスでジャンプactivityを使用する

  • 最初のステップ:
  • public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode);
    
    
    

    説明:このメソッドは、CordovaPluginベースクラスのcordovaメンバー変数を継承するメソッドです.使用法は通常のactivityと同様ですが、最初のパラメータはCordovaPluginまたはそのサブクラスでなければなりません.
  • 第2ステップ:プラグインクラスに次の方法を書き換え、この方法で結果を得ることができる.
  • 	@Override
    	public void onActivityResult(int requestCode, int resultCode, Intent intent)
    
    
    

    二、プラグインクラスに別のotherクラスが使用され、contextパラメータが入力された後、otherはactivityをジャンプする


    この場合、activityで結果を返す場合は、プラグインクラスで取得します.次の手順に従います.
  • Other otherObject = new Other(cordova.getActivity());//contextに転送され、otherクラスはactivityをスキップする方法
  • を呼び出す.
  • プラグインクラスはcordovaを呼び出す.setActivityResultCallback(CordovaPlugin plugin);//結果を返すときにこのプラグインを呼び出すonActivity Resultメソッド
  • を設定します.
  • プラグインクラスでの書き換え方法:onActivity Result(int requestCode,int resultCode,Intent intent)
  • cordova結果コールバック繰返し呼び出し


    また、例えばBluetoothを検索するプラグインを作ると、結果が一度に与えるものではないため、しばらくしてBluetoothデバイスが検索される可能性があり、jsレイヤに結果を伝える必要がある場合、mCallback Contextを直接繰り返し呼び出す.success(“device:”+name);またはmCallbackContext.error(「失敗」);間違いを報告します.
    cordova送信結果コールバックの実装を表示します.
    public void sendPluginResult(PluginResult pluginResult) {
    	synchronized (this) {
    	    if (finished) {
                    Log.w(LOG_TAG, "Attempted to send a second callback for ID: " + callbackId + "
    Result was: " + pluginResult.getMessage()); return; } else { finished = !pluginResult.getKeepCallback(); } } webView.sendPluginResult(pluginResult, callbackId); }

    従って、js層に情報を取得するようにjs層に繰り返し通知することができる.
    PluginResult pluginResult = new PluginResult(PluginResult.Status.OK," ");
    pluginResult.setKeepCallback(true);
    callbackContext.sendPluginResult(pluginResult);