ionic3如何編寫插件,我這里就不詳細(xì)描述了,給出一個(gè)參考地址參考這里。
重點(diǎn)說的是如何調(diào)用aar以及關(guān)于activity 的問題。
1.使用plugman生成對(duì)應(yīng)的插架的目錄。
? ??cd ionic-how-to-create-cordova-plugin
????mkdir plugins_src
????cd plugins_src
????plugman create --name HelloWorld --plugin_id cordova-plugin-hello-world --plugin_version 0.0.1
????cd HelloWorld
????plugman platform add --platform_name android
????plugman platform add --platform_name ios
????cd ..
????cd ..
????ionic cordova plugin add plugins_src/HelloWorld
最后一步基本都會(huì)報(bào)錯(cuò),因?yàn)槲覀內(nèi)鄙僖粋€(gè) packagejson的文件,plugman提供了這個(gè)功能。
2.使用 plugman 生成配置文件
? ??plugman createpackagejson .
簡(jiǎn)單回答一些問題,生成的packagejson 看起來有點(diǎn)像這樣
? ??{
????"name": "HelloWorld",
????"version": "0.0.1",
????"description": "HelloWorld",
????"cordova": {
????"id": "cordova-plugin-hello-world",
????"platforms": [
????"android",
????"ios"
????]
????},
????"keywords": [
????"ecosystem:cordova",
????"cordova-android",
????"cordova-ios"
????],
????"author": "Garfield",
????"license": "ISC"
????}
**這里面有個(gè)坑,雖然我的例子里面是用了 cordova-plugin-hello-world? 這樣的插件名字,但是我不建議你這么做,在我的編譯環(huán)境下,java 沒有辦法認(rèn)識(shí)帶有中橫線的包,我索性都改成了 . **
3. 目錄結(jié)構(gòu)
拷貝你的.aar到你的 plugins_src 下面,然后建立一個(gè)空白的build.gradle,這里的內(nèi)容我們待會(huì)兒在添加。
4 利用你的Android Studio,拷貝需要的gradle
在AS下如何使用aar ,這個(gè)應(yīng)該就相對(duì)簡(jiǎn)單了吧,新建一個(gè)工程,然后新建模塊,導(dǎo)入aar,就行了。
如果你的aar 有需要的依賴,也一起填進(jìn)來。這個(gè)時(shí)候查看你的工程的build.gradle ,我們需要拷貝其中
????repositories{
? ? jcenter()
? ? flatDir{
? ? ? dirs 'libs'
? ? }
? ? }
? ? dependencies {
? ? compile(name:'youraarfile', ext:'aar')
? ? compile 'com.google.code.gson:gson:2.2.4'
? ? compile 'com.android.support:support-v13:26.1.0'
? ? }
? android {
? ? ? defaultConfig {
? ? ? ? ? minSdkVersion 16
? ? ? ? ? targetSdkVersion 22
? ? ? }
? ? ? packagingOptions {
? ? ? ? ? exclude 'META-INF/NOTICE'
? ? ? ? ? exclude 'META-INF/LICENSE'
? ????}
? ????}
其中重要的地方是:? ?
? ? compile(name:'youraarfile', ext:'aar')
5? 修改plugin.xml
這里面有幾個(gè)重點(diǎn),
添加
第二個(gè)重點(diǎn)是 如果你的aar 里面有activity ,也需要添加進(jìn)來。
如果你不知道aar 里面有什么,把a(bǔ)ar的后綴改成zip,然后解壓縮,同樣可以看到?AndroidManifest.xml。
其中關(guān)于Activity 的部分拷貝到你的plugin.xml 中。
6 編寫代碼
插件.java 中如果用到了 activity ,會(huì)出現(xiàn)一個(gè)提示,
????Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
大致的意思是 你想從一個(gè)?Activity 的外部呼叫這個(gè)?Activity的話 ,需要一個(gè)?FLAG_ACTIVITY_NEW_TASK 標(biāo)志。
//下面最關(guān)鍵,利用intent啟動(dòng)新的Activity
? ? ? ? ? ? Intent intent = new Intent(cordova.getActivity(), youaarfile.Activity.class);
? ? ? ? ? ? intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
? ? ? ? ? ? intent.putExtra("mode","auth");
? ? ? ? ? ? intent.setPackage(this.cordova.getActivity().getApplicationContext().getPackageName());
? ? ? ? ? ? if (this.cordova != null) {
? ? ? ? ? ? ? ? this.cordova.startActivityForResult((CordovaPlugin) this, intent, 0);
? ? ? ? ? ? }
7 至于在page里面如何調(diào)用plugin 類似下面就可以了
????declare var cordova: any;
????cordova.plugins.yourplugin.coolMethod({
? ? ? ????_sMessage: "Hello World"
? ????? }, success, failure);