如何从 cordova 插件调用 activity 方法?
How to call activity methods from cordova plugin?
如何从 cordova 插件 class 中调用 activity class 中的方法?
// Plugin class
public class BLEPlugin extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("greet")) {
String name = args.getString(0);
String message = "Hello, " + name;
//callbackContext.success(message);
this.greet(message, callbackContext);
} else if (action.equals("isBLESupported")) {
this.isBLESupported(callbackContext);
}
return true;
}
// Returns true if BLE is supported.
private void isBLESupported(CallbackContext callbackContext) {
boolean isSupported = .. ? // how to access MainActivity method?
Log.w(TAG, "isSupported: " + isSupported);
JSONObject params = new JSONObject();
try {
params.put("isSupported", isSupported);
} catch (JSONException execp) {}
callbackContext.success(params);
}
}
// Main activity
// ..
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// ..
}
// How to access this method?
public boolean isBLESupported() {
return getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
}
@Override
protected void onResume() {
super.onResume();
// ..
}
}
在您的 MainActivity 中创建静态方法
public static boolean isBLESupported(Context c) {
System.out.println("Activity");
return c.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
}
并在您的 Cordova 插件中更改您的方法
// Returns true if BLE is supported.
private void isBLESupported(CallbackContext callbackContext) {
boolean isSupported = MainActivity.isBLESupported(this.cordova.getActivity().getApplicationContext());
Log.w(TAG, "isSupported: " + isSupported);
JSONObject params = new JSONObject();
try {
params.put("isSupported", isSupported);
} catch (JSONException execp) {}
callbackContext.success(params);
}
或者您可以通过 cordova context
直接从插件 class 检查布尔值
boolean isSupported = this.cordova.getActivity().getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
如何从 cordova 插件 class 中调用 activity class 中的方法?
// Plugin class
public class BLEPlugin extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("greet")) {
String name = args.getString(0);
String message = "Hello, " + name;
//callbackContext.success(message);
this.greet(message, callbackContext);
} else if (action.equals("isBLESupported")) {
this.isBLESupported(callbackContext);
}
return true;
}
// Returns true if BLE is supported.
private void isBLESupported(CallbackContext callbackContext) {
boolean isSupported = .. ? // how to access MainActivity method?
Log.w(TAG, "isSupported: " + isSupported);
JSONObject params = new JSONObject();
try {
params.put("isSupported", isSupported);
} catch (JSONException execp) {}
callbackContext.success(params);
}
}
// Main activity
// ..
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// ..
}
// How to access this method?
public boolean isBLESupported() {
return getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
}
@Override
protected void onResume() {
super.onResume();
// ..
}
}
在您的 MainActivity 中创建静态方法
public static boolean isBLESupported(Context c) {
System.out.println("Activity");
return c.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
}
并在您的 Cordova 插件中更改您的方法
// Returns true if BLE is supported.
private void isBLESupported(CallbackContext callbackContext) {
boolean isSupported = MainActivity.isBLESupported(this.cordova.getActivity().getApplicationContext());
Log.w(TAG, "isSupported: " + isSupported);
JSONObject params = new JSONObject();
try {
params.put("isSupported", isSupported);
} catch (JSONException execp) {}
callbackContext.success(params);
}
或者您可以通过 cordova context
直接从插件 class 检查布尔值 boolean isSupported = this.cordova.getActivity().getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);