Use the air native extension to obtain SIM card information of the mobile phone in the-air program 01

Source: Internet
Author: User

After several days of effort, finally completed this complex operation process, in Android get SIM card phone number and carrier information, see my blog http://blog.csdn.net/peijiangping1989/article/details/7344925

First, we need to write the code for getting SIM card information in the android project,

Import the flashruntimeextensions. jar file in the android directory under the bin directory under the air installation directory to the android project. Create a lib folder under the project, copy the jar to the folder, right-click the jar package build
Path-add to build path.

The code for the four classes is as follows:

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 () {// exposes the function name that can be called in air. getphonenum can be used in air. The corresponding class is 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: interface class and air program connection <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: Read SIM card information <br> * PS: You must add various permissions <br> * Date: 2012-3-12 <br> ** @ Version 1.00 * @ author codyy) peijiangping */public class simcardinfo {/*** telephonymanager provides the entry for obtaining Communication Service Information on the device. Applications can use this method to determine the access information of telecom service providers and countries and some types of users. * The application can also register a listener to change the phone receiving status. You do not need to directly instantiate this class * use context. getsystemservice (context. telephony_service) to obtain the instance of this class. */Private telephonymanager;/*** International Mobile User Identification Code */private string imsi; Public simcardinfo (context) {telephonymanager = (telephonymanager) context. getsystemservice (context. telephony_service);}/*** role: Get the current phone number <br> * Date: 2012-3-12 <br> ** @ author codyy) peijiangping */Public String getnativephonenumber () {string nativephonenumber = NULL; nativephonenumber = telephonymanager. Getline1number (); Return nativephonenumber;}/*** role: telecom service providers get mobile phone service provider Information <br> * permission required <uses-Permission * Android: Name = "android. permission. read_phone_state "/> <br> * Date: 2012-3-12 <br> ** @ author codyy) peijiangping */Public String getprovidersname () {string providersname = NULL; // return the Unique User ID. the ID of this card is imsi = telephonymanager. getsubscriberid (); // the first three digits of imsi are countries, followed by two digits of 00 02 are China Mobile, 0 1 is China Unicom and 03 is China Telecom. System. out. println (imsi); If (imsi. startswith ("46000") | imsi. startswith ("46002") {providersname = "China Mobile";} else if (imsi. startswith ("46001") {providersname = "China Unicom";} else if (imsi. startswith ("46003") {providersname = "China Telecom";} 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: write the code for calling the Java and Android interfaces in the call method here. <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 = pcontext. getactivity (); // used to obtain the context object in the air program // intent = new intent (intent. action_view); simcardinfo siminfo = new simcardinfo (context); // you can use pparams [Index] To set pparams as the parameter group passed in by air. getxx () to get; string OK = siminfo. getnativephonenumber () + pparams [0]. getasstring () + siminfo. getprovidersname (); // string OK = "AAAAA" + pparams [0]. getasstring (); // activity. startactivity (intent); Return freobject. newobject (OK); // return an object to air} catch (exception e) {e. printstacktrace ();} return NULL ;}}

2. Right-click the com. Pei. Air package and select export to export the JAR file.

Select next,

Save the jar package to the desktop to get an air3.jar file.

3. Open FB and create a new Flex library project. Remember to hook up the Adobe AIR library. Our name is testku.

4. Create packages and classes in the library project as follows:

Intentnativeutil. As code

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 "; // consistent with the key in map in Java end public static const extension_id: String = "com. pei. air "; // and extension. the ID labels in XML are consistent with those in private var extcontext: extensioncontext; Public Function intentnativeutil () {extcontext = extensioncontext. createextensioncontext (extension_id, ""); // The second parameter is used to pass in the createcontext method of freextension in Java code} 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>

After writing the code, save it, refresh the project, and run testku in the bin directory. copy the SWC file and create a folder to copy air3, jar, and testku. put SWC together.

5. in the libs directory of your flex mobile phone project. copy the SWC file to your project, and then introduce the library into your project. Similar to importing jar files in Java, We can compile the following interface in the project,

<? 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 = "homepage View"> <FX: SCRIPT> <! [CDATA [import COM. codyy. ppmeet. util. intentnativeutil; import MX. events. flexevent; private var phonenum: string; private var helloext: intentnativeutil; protected function button#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 (); // create extensi On}]> </FX: SCRIPT> <FX: declarations> <! -- Place non-visual elements (such as services and value objects) Here --> </FX: declarations> <s: button x = "173" Y = "179" width = "127" label = "button" Click = "button#clickhandler (event)"/> <s: label id = "label01" x = "65" Y = "45" width = "375" Height = "42" text = "tag"/> <s: textinput id = "input01" x = "122" Y = "117" width = "237" Height = "42"/> </S: View>

Of course, you can also use the getphonenum method that calls intentnativeutil in the class file.

In this way, all things have been solved. All the code has been written. Of course, you cannot directly run the program, because currently, air does not support native extension packaging.

See how to package the native extension air project in the next blog.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.