編輯:關於 Android 開發
因為工作需要在專案裡加一個自訂插件,折騰了好一陣子終於做出最簡單的版本,平台是 Android。寫部落格雖然耗時間,但以後回頭查閱能省不少力,也夠本了。廢話不多說,直接進主題。
到官網(https://nodejs.org/)下載安裝即可。在 DOS 視窗輸入 npm,能看到說明資訊就代表安裝成功。

npm install -g cordova
從 Android 官網(https://developer.android.com/sdk/index.html)下載 SDK,然後設定下列環境變數:

到這裡開發環境就準備好了。
cordova create hello com.cool.hello HelloWorld
hello:在目前目錄下建立名為 hello 的資料夾。com.cool.hello:套件名稱(反向網域),用來標識不同的 App。HelloWorld:專案名稱,之後可在 config.xml 中修改。cd hello # 進入專案目錄 cordova platforms list # 查看已加入的平台 cordova platform add android # 加入 Android 平台
若要移除已加入的平台:cordova platform remove android 或 cordova platform rm android。
cordova build android cordova run android
產生的專案可以匯入 Eclipse 開啟:

npm install -g plugman
plugman create --name <pluginName> --plugin_id <pluginId> --plugin_version <version> [--path <directory>] # 參數說明 # pluginName :插件名稱 # pluginId :插件 ID,例如 coolPlugin # version :版本,例如 0.0.1 # directory :絕對或相對路徑,插件專案將建立於此 # variable NAME=VALUE:額外的描述資訊,如作者等 # 範例 plugman create --name CoolPlugin --plugin_id coolPlugin --plugin_version 0.0.1
產生的目錄結構如下:

不過依慣例,比較規範的做法是在 src 下再建一層 android 目錄,把 Java 類別放進去:


這個檔案是插件的說明書,Cordova 靠它知道要把哪些檔案複製到哪裡、要加哪些權限:
<plugin id="coolPlugin" version="0.0.1"
xmlns="https://apache.org/cordova/ns/plugins/1.0"
xmlns:android="https://schemas.android.com/apk/res/android">
<name>CoolPlugin</name>
<js-module name="CoolPlugin" src="www/CoolPlugin.js">
<clobbers target="cool.toast" />
</js-module>
<!-- android -->
<platform name="android">
<config-file parent="/*" target="res/xml/config.xml">
<feature name="CoolToast">
<param name="android-package" value="com.cool.toast.ShowToast" />
</feature>
</config-file>
<source-file src="src/android/ShowToast.java"
target-dir="src/com/cool/toast" />
<config-file parent="/*" target="AndroidManifest.xml">
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
</config-file>
<source-file src="src/android/TestActivity.java"
target-dir="src/com/example/hello" />
<source-file src="src/android/layout/activity_text.xml"
target-dir="res/layout" />
</platform>
</plugin>
幾個重點:js-module 裡的 clobbers 決定 JS 端呼叫時的命名空間(這裡是 cool.toast);feature 的 name 會成為 JS 呼叫 exec 時的第三個參數,android-package 則指向實作的 Java 類別全名。
var exec = require('cordova/exec');
var myFunc = function(){};
// arg1:成功回呼
// arg2:失敗回呼
// arg3:plugin.xml 中 feature 配置的標識
// arg4:要呼叫的原生方法名
// arg5:參數,JSON 格式
myFunc.prototype.showToast = function(success, error) {
exec(success, error, "CoolToast", "showToast", []);
};
myFunc.prototype.showshowToast = function(text, lenth, success, error) {
exec(success, error, "CoolToast", "showshowToast", [text, lenth]);
};
myFunc.prototype.openActivity = function() {
exec(null, null, "CoolToast", "openActivity", []);
};
var showt = new myFunc();
module.exports = showt;
繼承 CordovaPlugin 並覆寫 execute(),用 action 字串分派不同功能,做完記得呼叫 callbackContext.success() 或 error():
package com.cool.toast;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import com.example.hello.TestActivity;
import android.content.Intent;
import android.widget.Toast;
public class ShowToast extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext)
throws JSONException {
if ("showToast".equals(action)) {
Toast.makeText(cordova.getActivity(), "show...", Toast.LENGTH_SHORT).show();
callbackContext.success("success");
return true;
} else if ("showshowToast".equals(action)) {
String str = args.getString(0);
int len = args.getInt(1);
if (len == 0) {
Toast.makeText(cordova.getActivity(), str, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(cordova.getActivity(), str, Toast.LENGTH_LONG).show();
}
callbackContext.success("success" + str);
return true;
} else if ("openActivity".equals(action)) {
openActivity();
callbackContext.success("success");
return true;
}
callbackContext.error("error");
return false;
}
private void openActivity() {
Intent intent = new Intent(cordova.getActivity(), TestActivity.class);
cordova.getActivity().startActivity(intent);
}
}
假設插件放在 F:\CoolPlugin,回到最初建立的 hello 目錄後執行:
cd hello cordova plugin add F:\CoolPlugin
編輯 F:\hello\platforms\android\assets\www\index.html,直接透過 clobbers 定義的命名空間呼叫:
cool.toast.showToast();
cool.toast.showshowToast("hello", 0, function(msg) {
alert(msg);
}, function(msg) {
alert(msg);
});
cool.toast.openActivity();
整個流程走完,自訂插件就能在 Cordova 專案裡正常運作。關鍵其實只有兩件事:plugin.xml 把檔案與權限送對位置,以及 JS 的 exec 參數與 Java 的 action 名稱對得起來。
Android 彈窗重構:打造易維護的 Dialog 與 WindowManager 彈窗模組
Android 彈窗重構:打造易維護的 Dialog 與 WindowManager 彈窗模組 在大型專案中,若每個功能都自行建立 Dialog,容易造成樣式分散、程式碼
Android Intent 比對 action、category 與 data 的原則
Intent 比對的基本概念 在 Android 中,當使用隱式 Intent 啟動元件時,系統會比對 Intent 中的 action、category 與 data,
Android 自定義 View 造成 Activity 洩漏的原因與修正
自定義 View 與 Activity 記憶體洩漏 在維護 Android 專案時,若自定義 View 在非 UI 執行緒建立,或在尚未附加到視窗前就呼叫 postDel
騰訊 Bugly 乾貨:RxJava 結合 MVP 的實戰分層與線程調度
本文整理自騰訊 Bugly 開發者社群的技術分享,主講者為騰訊 IEG Android 開發工程師戴俊,主題是《基於 RxJava 的一種 MVP 實現》。 RxJava