我正在尝试为Cordova创建一个自定义插件 - android。插件的目的是通过单击HTML5屏幕上的按钮来提高本机android活动的意图。 所以最初,我会在带有按钮的HTML5屏幕上。单击按钮,我应该被重定向到本机android活动屏幕。
以下是我已经完成的一些代码,
customplugin.js
function CustomPlugin(){};
CustomPlugin.prototype.launchActivity = function(startClass)
{
alert("@@@ Starting plugin to launch native activity.");
cordova.exec(null, null, 'CustomPlugin', 'launchActivity', [startClass]);
};
if(!window.plugins) {
window.plugins = {};
}
if (!window.plugins.customplugin) {
window.plugins.customplugin = new CustomPlugin();
}
但是,在这段代码中,我得到'未捕获的TypeError:无法调用方法'startActivity“未定义。” 请帮我一些示例代码示例。提前谢谢。
CustomPlugin.java
package org.apache.cordova.example;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import android.util.Log;
public class CustomPlugin extends CordovaPlugin
{
@Override
public boolean execute(String action, final JSONArray args, CallbackContext callbackContext) throws JSONException
{
if ("launchActivity".equals(action))
{
String goClass = null; if(args.length() > 0) goClass = args.getString(0);
Log.i("NATIVE", "Launch class : " + goClass);
return true;
}
else
{
return false;
}
}
答案 0 :(得分:3)
在您的代码段中,我看不到您实际执行launchActivity()
的位置。最好将它添加到问题中,因为它似乎存在问题。
首先,请确保您的页面中已加载 cordova.js 。然后您使用cordova.exec
(这是使用require.js的示例,但这不是必需的,您也可以使用cordova.exec()
define(['cordova'], function (cordova) {
'use strict';
var exec = cordova.require('cordova/exec');
return {
changeBackground : function (color) {
exec(function () {}, function () {}, 'Navbar', 'changeBackground', [color]);
}
};
});
确保在 res / xml / plugins.xml 中添加插件:
<plugin name="Navbar" value="my.package.NavbarPlugin"/>
要创建插件,只需展开org.apache.cordova.api.CordovaPlugin
。
public class NavbarPlugin extends CordovaPlugin {
@Override
public boolean execute(String action, final JSONArray args, CallbackContext callbackContext) throws JSONException {
if ("changeBackground".equals(action)) { ... }
}
}
修改强>
它无效的问题是因为您正在执行window.customplugin.launchActivity(...)
。 window.customplugin
不存在,因此您无法致电launchActivity
undefined
。
您需要致电window.plugins.customplugin.launchActivity(...)
答案 1 :(得分:2)
请找到下面的代码片段,为Android创建一个本机Cordova插件,并为网络端获取响应所需的配置。
示例:使用Cordova原生插件的Android设备的当前位置纬度和经度。
Android代码:
package com.sample.activity;
import android.app.Activity;
import android.content.Context
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
public class LocationTrackPlugin extends CordovaPlugin implements LocationListener {
public static final String ACTION_START = "GetLocation";
public static CallbackContext callbackContext;
public static Activity activity;
@Override
public boolean execute(String action, final JSONArray jArray,
final CallbackContext callbackContext) throws JSONException {
activity = this.cordova.getActivity();
boolean result = false;
if (ACTION_START.equalsIgnoreCase(action)) {
LocationTrackPlugin.callbackContext = callbackContext;
LocationManager locationManager = (LocationManager) activity
.getSystemService(activity.LOCATION_SERVICE);
if (!locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
PluginResult pluginResult = new PluginResult(
PluginResult.Status.OK, "false");
pluginResult.setKeepCallback(true);
try {
callbackContext.sendPluginResult(pluginResult);
} catch (Exception e) {
e.printStackTrace();
}
} else {
result = true;
Location location = getCurrentDeviceLocation(activity);
String my_location = location.getLatitude() + ":" + location.getLongitude();
PluginResult pluginResult = new PluginResult(
PluginResult.Status.OK, my_location);
pluginResult.setKeepCallback(true);
try {
callbackContext.sendPluginResult(pluginResult);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return result;
}
public Location getCurrentDeviceLocation(Context contxt) {
LocationManager locationManager;
String provider;
Location location = null;
locationManager = (LocationManager) contxt
.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
if (provider != null && !provider.equals("")) {
location = getCurrentLocation(provider, locationManager);
if (location != null) {
locationManager.removeUpdates(this);
return location;
} else {
location = getCurrentLocation(LocationManager.NETWORK_PROVIDER,
locationManager);
if (location != null) {
locationManager.removeUpdates(this);
return location;
} else {
locationManager.removeUpdates(this);
}
}
} else
Log.d("Location", "No Provider Found");
return location;
}
public Location getCurrentLocation(String provider,
LocationManager locationManager) {
Location newlocation = null;
if (locationManager.isProviderEnabled(provider)) {
locationManager.requestLocationUpdates(provider, 1000, 1, this);
if (locationManager != null) {
newlocation = locationManager.getLastKnownLocation(provider);
return newlocation;
}
}
return newlocation;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
Manifestfile.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Config.xml (文件位于appname / cordova / Config.xml)
<feature name="LocationTrackPlugin">
<param name="android-package" value=com.sample.activity.LocationTrackPlugin" />
</feature>
<强> ShowLocation.js 强>
callGetlocationPlugin: function() {
if (Ext.os.is('Android')) {
cordova.exec(
function(result) {
console.log("Native call success", result);
},
function() {
console.log('Native call failed');
},
'LocationTrackPlugin', 'GetLocation', null);
}
}