从CordovaPlugin打开一个活动

kra*_*off 29 android phonegap-plugins cordova

我编写了一个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.");
    }
}

}
Run Code Online (Sandbox Code Playgroud)

我想从这堂课开一个新的活动.如何访问基于phonegap的课程的原始上下文?

ρяσ*_*я K 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);
Run Code Online (Sandbox Code Playgroud)

并确保您已注册下一个活动 AndroidManifest.xml


小智 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;
    }
    
    Run Code Online (Sandbox Code Playgroud)


zai*_*ini 5

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

cordova.startActivityForResult(this, intent,0);
Run Code Online (Sandbox Code Playgroud)