2013-01-12 32 views
2

我正在嘗試爲Cordova - android創建自定義插件。插件的目的是通過單擊HTML5屏幕上的按鈕來引發本機android活動的意圖。所以最初,我會在一個帶有按鈕的HTML5屏幕上。點擊按鈕,我應該被重定向到一個原生的android活動屏幕。如何爲Cordova2.3創建自定義插件 - 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(); 
} 

在這段代碼,但是,我得到「遺漏的類型錯誤:無法調用未定義的方法‘launchActivity’ 「。 請幫我看一些示例代碼示例。提前致謝。

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; 
      } 
    } 

回答

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不存在,因此你得到你不能撥打launchActivityundefined

你需要調用window.plugins.customplugin.launchActivity(...)

+0

嘿,我所做的更改如你所說,但它仍然沒有工作。我仍然'Uncaught TypeError:不能調用未定義的方法launchActivity「。
順便說一句,在我給出的問題的代碼片段中,我沒有收到警報「@@@啓動插件啓動本機活動。」在任何時間點。這是否意味着控制永遠不會進入代碼的那一部分? – Tony

+0

你可以添加你執行launchActivity()到你的問題的代碼嗎?問題在那裏。 – asgoth

+0

Go Native
<腳本類型= 「文本/ JavaScript的」 SRC = 「科爾多瓦-2.3.0.js」> <腳本類型= 「文本/ JavaScript的」 SRC = 「JS/index.js」> Tony

2

請找到下面的代碼片段創建一個本地科爾多瓦插件,Android和需要調用的配置,並從網絡側響應。

示例:使用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(文件位於應用程序的名字/科爾多瓦/配置。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); 
    } 
} 
相關問題