31

CordavaPlugin 派生クラスを作成しました。

public class ShowMap extends CordovaPlugin {

@Override
public boolean execute(String action, JSONArray args,
        CallbackContext callbackContext) throws JSONException {

    if (action.compareTo("showMap") == 0)
    {
        String message = args.getString(0); 
        this.echo(message, callbackContext);

        Intent i = new Intent();


        return true;
    }

    return false;
}

private void echo(String message, CallbackContext callbackContext) {
    if (message != null && message.length() > 0) { 
        callbackContext.success(message);
    } else {
        callbackContext.error("Expected one non-empty string argument.");
    }
}

}

このクラスから新しい活動を始めたいと思っています。phonegap ベースのクラスの元のコンテキストにアクセスするにはどうすればよいですか?

4

6 に答える 6

42

次のように試してください:

    Context context=this.cordova.getActivity().getApplicationContext();
    //or Context context=cordova.getActivity().getApplicationContext();
    Intent intent=new Intent(context,Next_Activity.class);

    context.startActivity(intent);
    //or cordova.getActivity().startActivity(intent);

次のアクティビティが登録されていることを確認してくださいAndroidManifest.xml

于 2013-01-14T15:53:19.423 に答える
16
  1. アクティビティを AndroidManifest ファイルに登録します
  2. プラグインには、次のようなコードが必要です。「callback.success()」が呼び出されていないことに注意してください。
  3. バックグラウンド スレッドではなく、UI スレッドでアクションを実行します。
  4. 楽しい

    if (action.equals("myaction")) {
        cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Context context = cordova.getActivity()
                        .getApplicationContext();
                Intent intent = new Intent(context, MyNewActivityGap.class);
                cordova.getActivity().startActivity(intent);
            }
        });
    
        return true;
    }
    
于 2013-05-08T21:26:49.207 に答える
5
Context context =  cordova.getActivity().getApplicationContext();
Intent intent = new Intent(context,Next_Activity.class);

cordova.startActivityForResult(this, intent,0);
于 2013-01-29T09:15:09.963 に答える
1

暗黙の意図を使用して、この機能を機能させました

  Intent i = new Intent("ACTION_PLAY_VIDEO");
 this.cordova.startActivityForResult((CordovaPlugin) this,i, 0);

マニフェスト ファイルのターゲット アクティビティにインテント フィルターを入れることを忘れないでください

<activity android:name="VideoPlayerActivity" >
       <intent-filter>
            <action android:name="ACTION_PLAY_VIDEO" />


            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

于 2014-07-02T10:06:50.233 に答える
-2

この例を参照してください。

まず、config.xml でカスタム プラグインを宣言する必要があります。このファイルは res > xml フォルダーにあります。

<feature name="CustomPlugin">
      <param name="android-package" value="com.Phonegap.CustomPlugin" />
</feature>

次に、Java コードを使用してプラグインを実装する必要があります。

public class CustomPlugin extends CordovaPlugin {

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
            throws JSONException {

        if (action.equals("sayHello")){
            try {
                String responseText = "Hello world, " + args.getString(0);
                callbackContext.success(responseText);
            } catch (JSONException e){
                callbackContext.error("Failed to parse parameters");
            }
            return true;
        }

        return false;
    }
}

最後に、javascript からプラグインを呼び出します

function initial(){
    var name = $("#NameInput").val();
    cordova.exec(sayHelloSuccess, sayHelloFailure, "CustomPlugin", "sayHello", [name]);
}

function sayHelloSuccess(data){
    alert("OK: " + data);
}

function sayHelloFailure(data){
    alert("FAIL: " + data);
}
于 2015-03-16T04:14:27.367 に答える