1. 程式人生 > >Framework7 + cordova +AS 混合開發安卓app(二)

Framework7 + cordova +AS 混合開發安卓app(二)

四、cordova建立專案

建立專案

 cordova create hello com.example.hello HelloWorld

如果一切正常,本條命令將建立一名為hello的專案資料夾,com.example.hello是你的專案包名,它將生成一個基於web的應用程式,其主頁是專案的www/index.html檔案 新增平臺

cd hello

顯示切換至專案(hello)資料夾,此時你需要新增應用的操作平臺,這裡給出Android與ios的命令操作

cordova platform add ios
cordova platform add android

你也可以通過命令查詢其他可新增操作平臺的命令

cordova platform ls

此時,你的cordova專案已經建立完成,你可以在你的電腦上看到生成的資料夾,其結構如圖: 這裡寫圖片描述 如果你按照如圖所示點選開啟platforms資料夾,然後你會看到在命令列中所新增的操作平臺,譬如我這裡顯示有Android和ios,接著我們開啟Android資料夾,一直點選開啟,直到你看到圖片所示的目錄結構,你會注意到assets下的www資料夾,它與我們前面在wamp軟體資料夾下的www資料夾有著很多相似之處,事實上,我們需要將前面我們編寫的HTML,Css,JavaScript等檔案拷貝到這個地方,你會看到圖片所示的結果 這裡寫圖片描述

五、AS匯入專案

操作步驟如下: 這裡寫圖片描述

這裡寫圖片描述 這裡寫圖片描述 點選OK,你將看到如下介面 這裡寫圖片描述 匯入成功後,你將看到 這裡寫圖片描述

六、cordova外掛呼叫

在我們開發的過程中,我們需要使用者手機允許APP呼叫一些程式,此時我們就不得不借助cordova外掛,這裡給出三個常用外掛的說明 1、cordova-Camera外掛 安裝

cordova plugin add cordova-plugin-camera

呼叫

//圖片來源
var pictureSource;

//設定返回值格式
var destinationType;

//當前請求呼叫硬體的頁面標識
let g_camera_caller;

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
  //載入後執行
  pictureSource = navigator.camera.PictureSourceType;
  destinationType = navigator.camera.DestinationType;
}


//當成功獲取圖片時以base64編碼格式顯示
function onPhotoDataSuccess(imageData) {
  console.log(imageData);
  let image_src = "data:image/jpeg;base64," + imageData;
}


//錯誤返回
function onFail(message) {
  app.dialog.alert('圖片獲取失敗: ' + message);
}


/*
 *拍照函式
 */
function capturePhotoViaDevice() {
  navigator.camera.getPicture(onPhotoDataSuccess, onFail, {
    quality: 50,
    destinationType: destinationType.DATA_URL,
    allowEdit: true,
    correctOrientation: true,
    saveToPhotoAlbum: true
  });
}


/*
 *從圖片庫選擇函式
 */
function getPhotoFromDevice() {
  navigator.camera.getPicture(onPhotoDataSuccess, onFail, {
    quality: 50,
    destinationType:destinationType.DATA_URL,
    allowEdit: true,
    correctOrientation: true,
    sourceType: pictureSource.PHOTOLIBRARY
  });
}

說明

2、cordova-Splashscreen外掛 安裝

cordova plugin add cordova-plugin-splashscreen

呼叫 一般來說,可以直接修改res資料夾下drawable-screen和mipmap-icon資料夾裡面的圖片,然後直接對應呼叫即可,需要注意的是,照片的畫素一定要按規定對應 這裡寫圖片描述 在AS的config.xml中配置以下資訊

<platform name="android">  
    <icon density="ldpi" src="res/icon/android/drawable-ldpi/icon.png" />
    <icon density="mdpi" src="res/icon/android/drawable-mdpi/icon.png" />
    <icon density="hdpi" src="res/icon/android/drawable-hdpi/icon.png" />
    <icon density="xhdpi" src="res/icon/android/drawable-xhdpi/icon.png" />
    <icon density="xxhdpi" src="res/icon/android/drawable-xxhdpi/icon.png" />
    <!-- 以下是啟動螢幕頁面,可根據需要進行新增 -->
    <splash density="land-hdpi" src="res/screen/android/splash-land-hdpi.png" />  
    <splash density="land-ldpi" src="res/screen/android/splash-land-ldpi.png" />  
    <splash density="land-mdpi" src="res/screen/android/splash-land-mdpi.png" />  
    <splash density="land-xhdpi" src="res/screen/android/splash-land-xhdpi.png" />  
    <splash density="port-hdpi" src="res/screen/android/splash-port-hdpi.png" />  
    <splash density="port-ldpi" src="res/screen/android/splash-port-ldpi.png" />  
    <splash density="port-mdpi" src="res/screen/android/splash-port-mdpi.png" />  
    <splash density="port-xhdpi" src="res/screen/android/splash-port-xhdpi.png" />  
</platform>  
<platform name="ios">  
    <!-- iOS 8.0+ -->  
    <!-- iPhone 6 Plus  -->  
    <icon src="res/icon/ios/[email protected]" width="180" height="180" />  
    <!-- iOS 7.0+ -->  
    <!-- iPhone / iPod Touch  -->  
    <icon src="res/icon/ios/icon-60.png" width="60" height="60" />  
    <icon src="res/icon/ios/[email protected]" width="120" height="120" />  
    <!-- iPad -->  
    <icon src="res/icon/ios/icon-76.png" width="76" height="76" />  
    <icon src="res/icon/ios/[email protected]" width="152" height="152" />  
    <!-- iOS 6.1 -->  
    <!-- Spotlight Icon -->  
    <icon src="res/icon/ios/icon-40.png" width="40" height="40" />  
    <icon src="res/icon/ios/[email protected]" width="80" height="80" />  
    <!-- iPhone / iPod Touch -->  
    <icon src="res/icon/ios/icon.png" width="57" height="57" />  
    <icon src="res/icon/ios/[email protected]" width="114" height="114" />  
    <!-- iPad -->  
    <icon src="res/icon/ios/icon-72.png" width="72" height="72" />  
    <icon src="res/icon/ios/[email protected]" width="144" height="144" />  
    <!-- iPhone Spotlight and Settings Icon -->  
    <icon src="res/icon/ios/icon-small.png" width="29" height="29" />  
    <icon src="res/icon/ios/[email protected]" width="58" height="58" />  
    <!-- iPad Spotlight and Settings Icon -->  
    <icon src="res/icon/ios/icon-50.png" width="50" height="50" />  
    <icon src="res/icon/ios/[email protected]" width="100" height="100" />  
    <!-- 以下是歡迎頁面,可根據需要進行新增 -->
    <splash src="res/screen/ios/Default~iphone.png" width="320" height="480"/>  
    <splash src="res/screen/ios/[email protected]~iphone.png" width="640" height="960"/>  
    <splash src="res/screen/ios/Default-Portrait~ipad.png" width="768" height="1024"/>  
    <splash src="res/screen/ios/[email protected]~ipad.png" width="1536" height="2048"/>  
    <splash src="res/screen/ios/Default-Landscape~ipad.png" width="1024" height="768"/>  
    <splash src="res/screen/ios/[email protected]~ipad.png" width="2048" height="1536"/>  
    <splash src="res/screen/ios/[email protected]~iphone.png" width="640" height="1136"/>  
    <splash src="res/screen/ios/Default-667h.png" width="750" height="1334"/>  
    <splash src="res/screen/ios/Default-736h.png" width="1242" height="2208"/>  
    <splash src="res/screen/ios/Default-Landscape-736h.png" width="2208" height="1242"/>  
</platform>

這裡需要注意的是,檔案路徑填寫你的AS專案介面下顯示的檔案路徑; 你還可以新增

<preference name="SplashScreenDelay" value="3000" />

設定啟動頁的時長,預設是3000,即3秒; 或者新增

<preference name="SplashScreen" value="screen" />

有時你可能想自己命名你的啟動頁檔名,這條程式碼可以幫你對應呼叫。

3、cordova-WeChat外掛 準備 在安裝呼叫之前,你需要建立一個安卓簽名檔案 你可以用命令列建立,檢視這位博主的分享說明命令列建立安卓簽名檔案 博主使用的是AS建立簽名檔案,操作步驟如下: 這裡寫圖片描述 這裡寫圖片描述 選擇檔案存放路徑: 選擇檔案路徑 這裡寫圖片描述 填寫好相關資訊後,點選OK即可建立成功。 建立成功後,你可以使用DOS命令將目錄切換成donkor.jks檔案目錄下,輸入keytool -list -v -keystore donkor.jks,按下回車鍵。輸入keystore密碼,獲取到簽名信息 當然你還有另一種方法,你可以通過一個軟體輸入包名自動生成簽名信息 點選下載 它們一般是32位; 接下來,你需要去微信開放平臺,獲取你的APPID,你會看到如下介面 這裡寫圖片描述 選擇移動應用開發,再選擇建立應用,你會看到 這裡寫圖片描述 以及 這裡寫圖片描述 注意:這裡的應用包名一定要和cordova建立專案的包名一致,應用簽名與打包時使用的簽名檔案一致 提交成功後,你可以在管理中心檢視到介面資訊 這裡寫圖片描述 需要什麼樣的功能介面就去申請,會有一定的稽核時間,你將獲取到你的APPID,在接下來的步驟中它很重要

安裝

cordova plugin add cordova-plugin-wechatv2 --variable wechatappid=YOUR_WECHAT_APPID

呼叫 微信外掛的呼叫功能有很多,貼出常見的幾種 檢查安裝

Wechat.isInstalled(function (installed) {
    alert("Wechat installed: " + (installed ? "Yes" : "No"));
}, function (reason) {
    alert("Failed: " + reason);
});

分享媒體

Wechat.share({
    message: {
        title: "Hi, there",
        description: "This is description.",
        thumb: "www/img/thumbnail.png",
        mediaTagName: "TEST-TAG-001",
        messageExt: "這是第三方帶的測試欄位",
        messageAction: "<action>dotalist</action>",
        media: "YOUR_MEDIA_OBJECT_HERE"
    },
    scene: Wechat.Scene.TIMELINE   // share to Timeline
}, function () {
    alert("Success");
}, function (reason) {
    alert("Failed: " + reason);
});

傳送支付請求

var params = {
    partnerid: '10000100', // merchant id
    prepayid: 'wx201411101639507cbf6ffd8b0779950874', // prepay id
    noncestr: '1add1a30ac87aa2db72f57a2375d8fec', // nonce
    timestamp: '1439531364', // timestamp
    sign: '0CB01533B8C1EF103065174F50BCA001', // signed string
};
 
Wechat.sendPaymentRequest(params, function () {
    alert("Success");
}, function (reason) {
    alert("Failed: " + reason);
});

3、cordova-Crosswalkt外掛 安裝

cordova plugin add cordova-plugin-crosswalk-webview

七、AS配置檔案

1.config.xml檔案 一般來說,你會在config.xml檔案看到如下程式碼

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.hemaapp.cjzx" version="1.0.2">
    <feature name="Whitelist">
        <param name="android-package" value="org.apache.cordova.whitelist.WhitelistPlugin" />
        <param name="onload" value="true" />
    </feature>
    <name>
       testapp
    </name>
    <description>
     just a test
    </description>
    <author email="[email protected]" href="http://cordova.io">
       Ian Team
    </author>
    <content src="index.html" />
    <access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <allow-intent href="market:*" />
    <preference name="loglevel" value="DEBUG" />
    <feature name="Camera">
        <param name="android-package" value="org.apache.cordova.camera.CameraLauncher" />
    </feature>
    <feature name="SplashScreen">
        <param name="android-package" value="org.apache.cordova.splashscreen.SplashScreen" />
        <param name="onload" value="true" />
    </feature>

你可以在這裡配置你的APP資訊 version是你的APP當前版本號,你可以通過在以後對APP進行區分 name是你的APP稱 description是你的APP描述 author 是你的資訊 這裡推薦官方文件說明(https://cordova.apache.org/docs/en/latest/config_ref/index.html) 2.string.xml檔案 你會在string.xml中看到如下程式碼:

<?xml version='1.0' encoding='utf-8'?>
<resources>
    <string name="app_name">
        testapp
    </string>
    <string name="launcher_name">@string/app_name</string>
    <string name="activity_name">@string/launcher_name</string>
</resources>

它在很多地方都進行了呼叫,所以建議修改其中的app_name為你的APP名稱。

3.AndroidManifes.xml檔案 在cordova打包的專案中,我們不需要對本檔案做過多的配置,不過注意的是以下程式碼:

android:versionCode="1" android:versionName="1.0.0" 

說明如下: android:versionCode 是用於版本升級的,int型別,一般我們會把第一個版本定義為1,以此遞增,通過判斷該值確定是否需要升級更新APP,不對使用者顯示; android:versionName 是APP版本號,string型別,一般定義為1.0/1.0.0等,面向使用者顯示。

八、AS打包apk

操作步驟如下: 這裡寫圖片描述 點選選擇已存在的key,進入到key的檔案路徑 注意:這裡的簽名檔案一定要使用在微信開放平臺登記的簽名檔案,否則會呼叫微信外掛失敗 這裡寫圖片描述 這裡寫圖片描述 打包成功後,可以看到 這裡寫圖片描述 點選locate,彈出apk所在位置 這裡寫圖片描述 至此專案結束,特別鳴謝Owen Tsai的指導,以及CSDN中眾多前輩的文件的指導,有不對之處請多多指教。 聯絡我:[email protected]