十分鐘開發一個調用Activity的PhoneGap外掛程式,activityphonegap

來源:互聯網
上載者:User

十分鐘開發一個調用Activity的PhoneGap外掛程式,activityphonegap

      在HybridApp開發中,很多業務我們是沒有辦法通過HTML5+js實現的,比如調用第三方的包含Activity的jar包,一些必須使用原生代碼才能實現的功能,比如複雜的UI的效果,調用通訊相關的協議棧,第三方支付sdk等。這種情況我們需要通過寫cordova外掛程式的方式,封裝android原生代碼為外掛程式,供上層javascript調用。本文講解一個調用Activity並返回Activity結果的cordova外掛程式例子,協助大家掌握cordova plugin的開發 。

     Cordova外掛程式是通過cordova plugin add (外掛程式包名需要在cordova plugins官方註冊,或外掛程式代碼存放的git地址)添加,通過cordova plugin rm 外掛程式包名刪除。一個外掛程式主要檔案包括外掛程式設定檔plugin.xml,外掛程式描述檔案package.json,平台相關的資源和源碼,可以包括圖片,xml,java源碼,jar包,so靜態庫等。下面是我做的android圖案鎖屏cordova外掛程式的目錄結構。

外掛程式目錄結構

     

       可以看到這個外掛程式支援ios和android平台,包含源碼和圖片資源,還是挺複雜的。

       我們今天介紹的不會這麼麻煩了,只是簡單的調用Activity,先看config.xml

設定檔config.xml代碼

<?xml version="1.0" encoding="UTF-8"?><plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"    id="com.qianmi.cordova.exitapp"    version="0.0.3">    <name>DemoPlugin</name>    <description>Qianmi <span style="font-family: Arial, Helvetica, sans-serif;">DemoPlugin </span><span style="font-family: Arial, Helvetica, sans-serif;"> Plugin</description></span>    <license>Apache 2.0</license>    <keywords>Qianmi,<span style="font-family: Arial, Helvetica, sans-serif;">DemoPlugin</span><span style="font-family: Arial, Helvetica, sans-serif;"></keywords></span>  <js-module src="www/demo.js" name="demo">    <clobbers target="cordova.plugins.demo" />    <!--js中調用的名字啊 -->  </js-module>       <!-- android -->    <platform name="android">        <config-file target="res/xml/config.xml" parent="/*">            <feature name="DemoApp">                <param name="android-package" value="com.qianmi.cordova.d<span style="font-family: Arial, Helvetica, sans-serif;">emoapp</span><span style="font-family: Arial, Helvetica, sans-serif;">" /></span>            </feature>        </config-file>           <source-file src="src/android/DemoApp.java" target-dir="src/com/qianmi/cordova/d<span style="font-family: Arial, Helvetica, sans-serif;">emoapp</span><span style="font-family: Arial, Helvetica, sans-serif;">" /></span>    </platform></plugin>

外掛程式www/js的代碼

var argscheck = require('cordova/argscheck'),    utils = require('cordova/utils'),    exec = require('cordova/exec');var DemoApp = function () {};//ExitAppExitApp.exit = function () {    console.log('----exit');    exec(null, null, "DemoApp", "method", [null, null, null]);};module.exports = ExitApp;


外掛程式java代碼     重點的外掛程式java代碼(只是樣本啊,不完整的)給Activity傳值,接受返回結果,回傳給js都在這裡了
public class LockPattern extends CordovaPlugin {private static final String TAG = "LockPattern";private static final String NICK_NAME = "nickName";private static final String MODE = "mode";public static final int REQUEST_CODE_SET_LOCK_PATTERN = 10001;public static final int REQUEST_CODE_VERIFY_LOCK_PATTERN = 10002; private CallbackContext mCallbackContext;@Overridepublic boolean execute(String action, JSONArray args,CallbackContext callbackContext) throws JSONException {Log.i(TAG, action + "   " + args);mCallbackContext = callbackContext;Intent intent = new Intent().setClass(cordova.getActivity(),LockPatternActivity.class);intent.putExtra(MODE, LockPatternActivity.MODE_STEP_1);intent.putExtra(NICK_NAME, nickName);this.cordova.startActivityForResult(this, intent,REQUEST_CODE_SET_LOCK_PATTERN);return false;}@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {Log.i(TAG, "---onActivityResult:" + requestCode + "   " + resultCode);switch (requestCode) {case REQUEST_CODE_SET_LOCK_PATTERN:if (Activity.RESULT_OK ==  resultCode && null != mCallbackContext) {mCallbackContext.success(LockPatternUtils.loadFromPreferences(cordova.getActivity()));}break;}}}

一個cordova 外掛程式就這麼簡單,大家可以輕鬆的從js調用activity了


android phonegap自訂外掛程式中在activity中將資料返回給javascript端

這個你可以去網上找一正步看的
 
android phonegap 怎在activity調用頁面js

如果你說的是AndroidManifest.xml裡的activity,我就費解了, 設定檔怎麼可能調用js檔案...
如果你需要後台調用, 一個簡單的例子:
public class Main extends DroidGap
{
private MyClass mc;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

super.init();

mc = new MyClass(this, appView);
appView.addJavascriptInterface(mc, "MyCls");

super.loadUrl(getString(R.string.url));
}
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.