最近开发的一款移动app应用基于ionic3+ng5+cordova移动框架,需求如下:需要在当前应用中通过urlScheme协议的方式打开本机的另外一个app应用,实现的效果如下:
1.自定义cordvoa插件核心代码如下
public class Printer extends CordovaPlugin {
private CallbackContext mcallbackContext;
@Override
public boolean execute(String action, String rawArgs, CallbackContext callbackContext) throws JSONException {
return super.execute(action, rawArgs, callbackContext);
}
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
this.mcallbackContext=callbackContext;
if (action.equals("openApp")){
final String url = args.getString(0);
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
go("guoxinsdk://?json="+url);
}
});
return true;
}
return false;
}
@Override
public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {
return super.execute(action, args, callbackContext);
}
private void go(String json){
PackageManager packageManager = cordova.getActivity().getPackageManager();
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(json));
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isValid = !activities.isEmpty();
if (isValid) {
cordova.startActivityForResult((CordovaPlugin) this, intent, 1);
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK);
pluginResult.setKeepCallback(true); // Keep callback
}else{
Toast.makeText(cordova.getActivity(), "请下载最新版国信影像云", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if(resultCode == RESULT_OK) {
// Log.i("aa-------------","+++++++++++++"+data.getStringExtra("siitresult"));
/* PluginResult pluginResult = new PluginResult(PluginResult.Status.OK,"中间消息");
pluginResult.setKeepCallback(true);
mcallbackContext.sendPluginResult(pluginResult);*/
try {
JSONObject jsonObject=new JSONObject(data.getStringExtra("siitresult"));
String result=jsonObject.optString("result");
if("2".equals(result)){
mcallbackContext.error(data.getStringExtra("siitresult"));
}else if("1".equals(result)){
mcallbackContext.success(data.getStringExtra("siitresult"));
}else {
// mcallbackContext.error("未知错误");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
break;
}
}
}
2.plugin.xml配置文件如下
<?xml version='1.0' encoding='utf-8'?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-huiju-printer"
version="0.0.1">
<name>cordova-plugin-huiju-printer</name>
<js-module name="Printer" src="www/printer.js">
<clobbers target="Printer" />
</js-module>
<platform name="android">
<source-file src="src/android/Printer.java" target-dir="src/com/huiju/cordova/plugins/printer" />
<config-file parent="/*" target="res/xml/config.xml">
<feature name="Printer">
<param name="android-package" value="com.huiju.cordova.plugins.printer.Printer" />
</feature>
</config-file>
</platform>
</plugin>
3.原生抛给前端调用的js如下
var exec = require('cordova/exec');
/**
* GxyxyURLScheme :是plugin.xml中配置的feature的nema
* openApp: 是js中调用的方法名
*/
exports.openApp = function(arg0,success,error) {
exec(success,error, "Printer", "openApp", [arg0]);
};