AIR Native Extension的使用–AIR程式中擷取手機SIM卡資訊01

來源:互聯網
上載者:User

經過幾天的努力,終於完成了這個複雜的操作過程,在Android中擷取SIM卡中的電話號碼和電訊廠商資訊,見我的部落格http://blog.csdn.net/peijiangping1989/article/details/7344925

首先1.我們要在Android項目中寫好擷取SIM卡資訊的代碼,

需要先匯入AIR安裝目錄下的bin目錄下android目錄下的FlashRuntimeExtensions.jar檔案到Android項目中。在項目下建立lib檔案夾,把jar拷到該檔案夾下,右鍵jar包build
path-add to build path即可。

四個類的代碼如下:

AS3COntext.java

package com.pei.air;import java.util.HashMap;import java.util.Map;import com.adobe.fre.FREContext;import com.adobe.fre.FREFunction;/** * class name:AS3Context<BR> * class description:<BR> * PS: <BR> * Date:2012-3-13<BR> *  * @version 1.00 * @author CODYY)peijiangping */public class AS3Context extends FREContext {@Overridepublic void dispose() {}@Overridepublic Map<String, FREFunction> getFunctions() {// 暴露air裡能調用的函數名,getPhoneNum在AIR中可以使用,對應的類是AS3FunctionHashMap<String, FREFunction> hashmap = new HashMap<String, FREFunction>();hashmap.put("getPhoneNum", new AS3Function());return hashmap;}}


AS3Extension.java

package com.pei.air;import com.adobe.fre.FREContext;import com.adobe.fre.FREExtension;/** * class name:AS3Extension<BR> * class description:介面類和AIR程式對接<BR> * PS: <BR> * Date:2012-3-15<BR> *  * @version 1.00 * @author CODYY)peijiangping */public class AS3Extension implements FREExtension {@Overridepublic FREContext createContext(String arg0) {return new AS3Context();}@Overridepublic void dispose() {}@Overridepublic void initialize() {}}


SIMCardInfo.java

package com.pei.air;import android.content.Context;import android.telephony.TelephonyManager;/** * class name:SIMCardInfo<BR> * class description:讀取Sim卡資訊<BR> * PS: 必須在加入各種許可權 <BR> * Date:2012-3-12<BR> *  * @version 1.00 * @author CODYY)peijiangping */public class SIMCardInfo {/** * TelephonyManager提供裝置上擷取通訊服務資訊的入口。 應用程式可以使用這個類方法確定的電信服務商和國家 以及某些類型的使用者訪問資訊。 * 應用程式也可以註冊一個監聽器到電話收狀態的變化。不需要直接執行個體化這個類 * 使用Context.getSystemService(Context.TELEPHONY_SERVICE)來擷取這個類的執行個體。 */private TelephonyManager telephonyManager;/** * 國際移動使用者識別碼 */private String IMSI;public SIMCardInfo(Context context) {telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);}/** * Role:擷取當前設定的電話號碼 <BR> * Date:2012-3-12 <BR> *  * @author CODYY)peijiangping */public String getNativePhoneNumber() {String NativePhoneNumber = null;NativePhoneNumber = telephonyManager.getLine1Number();return NativePhoneNumber;}/** * Role:Telecom service providers擷取手機服務商資訊 <BR> * 需要加入許可權<uses-permission * android:name="android.permission.READ_PHONE_STATE"/> <BR> * Date:2012-3-12 <BR> *  * @author CODYY)peijiangping */public String getProvidersName() {String ProvidersName = null;// 返回唯一的使用者ID;就是這張卡的編號神馬的IMSI = telephonyManager.getSubscriberId();// IMSI號前面3位460是國家,緊接著後面2位00 02是中國移動,01是中國聯通,03是中國電信。System.out.println(IMSI);if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {ProvidersName = "中國移動";} else if (IMSI.startsWith("46001")) {ProvidersName = "中國聯通";} else if (IMSI.startsWith("46003")) {ProvidersName = "中國電信";}return ProvidersName;}}


AS3Function.java

package com.pei.air;import com.adobe.fre.FREContext;import com.adobe.fre.FREFunction;import com.adobe.fre.FREObject;import android.content.Context;/** * class name:AS3Function<BR> * class description:在這裡的call方法裡面寫調用java和android介面的代碼<BR> * PS: <BR> * Date:2012-3-15<BR> * @version 1.00 * @author CODYY)peijiangping */public class AS3Function implements FREFunction {@Overridepublic FREObject call(FREContext pContext, FREObject[] pParams) {try {Context context = pContext.getActivity();//用來取得AIR程式中的Context對象//Intent intent = new Intent(Intent.ACTION_VIEW);SIMCardInfo siminfo = new SIMCardInfo(context);//pParams為AIR傳進來的參數組可以通過pParams[index].getXX()來擷取;String ok = siminfo.getNativePhoneNumber()+pParams[0].getAsString()+siminfo.getProvidersName();//String ok ="AAAAA"+pParams[0].getAsString();//activity.startActivity(intent);return FREObject.newObject(ok);//返回一個Object對象給AIR} catch (Exception e) {e.printStackTrace();}return null;}}

2.右鍵點擊com.pei.air包,選擇Export匯出jar檔案

選擇下一步,

把jar包儲存到案頭這樣就得到了一個air3.jar檔案。

3.開啟FB,建立一個Flex庫項目,記得勾住Adobe AIR庫,,我們取名叫TestKu

4.在庫項目中建立包和類如下:

IntentNativeUtil.as代碼

package com.codyy.ppmeet.util{import flash.events.EventDispatcher;import flash.events.StatusEvent;import flash.external.ExtensionContext;public class IntentNativeUtil extends EventDispatcher  {  private var context:ExtensionContext;  public static const HELLO_WORLD_FUNCTION:String = "getPhoneNum";//與java端中Map裡的key一致public static const EXTENSION_ID:String = "com.pei.air";//與extension.xml中的id標籤一致private var extContext:ExtensionContext;public function IntentNativeUtil(){extContext = ExtensionContext.createExtensionContext(EXTENSION_ID,"");//第二個為參數,會傳入java代碼中的FREExtension的createContext方法}public function getPhoneNum(name:String):String{if(extContext){return extContext.call(HELLO_WORLD_FUNCTION,name) as String;}return "failed";}}}  

extension.xml

<?xml version="1.0" encoding="UTF-8"?><extension xmlns="http://ns.adobe.com/air/extension/3.1">  <id>com.pei.air</id>  <versionNumber>1</versionNumber>  <platforms>    <platform name="Android-ARM">      <applicationDeployment>        <nativeLibrary>air3.jar</nativeLibrary>        <initializer>com.pei.air.AS3Extension</initializer>        <finalizer>com.pei.air.AS3Extension</finalizer>      </applicationDeployment>    </platform>  </platforms></extension>

寫好代碼以後,我們儲存,重新整理項目,然後把bin目錄下的TestKu.swc檔案拷出來,可以建立一個檔案夾把前面的air3,jar和這個TestKu.swc放一起。

5.然後在你的Flex手機項目中的libs目錄下,把TestKu.swc拷進去,這樣就成功的把庫引入了你的項目中了,跟java匯入jar性質一樣,我們可以項目中編寫一個如下的介面,

<?xml version="1.0" encoding="utf-8"?><s:View xmlns:fx="http://ns.adobe.com/mxml/2009"xmlns:s="library://ns.adobe.com/flex/spark"creationComplete="init(event)" title="首頁視圖"><fx:Script><![CDATA[import com.codyy.ppmeet.util.IntentNativeUtil;import mx.events.FlexEvent;private var PhoneNum:String;private var helloExt:IntentNativeUtil;protected function button1_clickHandler(event:MouseEvent):void{PhoneNum=helloExt.getPhoneNum(input01.text);label01.text=PhoneNum;}protected function init(event:FlexEvent):void{// TODO Auto-generated method stubhelloExt = new IntentNativeUtil();//建立extension}]]></fx:Script><fx:Declarations><!-- 將非可視元素(例如服務、值對象)放在此處 --></fx:Declarations><s:Button x="173" y="179" width="127" label="按鈕" click="button1_clickHandler(event)"/><s:Label id="label01" x="65" y="45" width="375" height="42" text="標籤"/><s:TextInput id="input01" x="122" y="117" width="237" height="42"/></s:View>

當然你也可以在類檔案中使用只需調用IntentNativeUtil的getPhoneNum方法就可以。

這樣所有的事情都已經解決。所有的代碼都已經寫好了,當然你不可以直接運行程式,因為目前AIR還不支援引用了Native Extension的打包。

請看下篇部落格如何進行使用了Native Extension的AIR項目的打包。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.