Android 19: Android Studio Export jar package (Module) and get Mobile Information

Source: Internet
Author: User

As does not have an independent module project, but you can add a module to normal project. The so-called module is a modular unit that we usually refer to, and is often present in the form of a jar package. The following is a demonstration of the modularity of the as in an example of getting cell phone information.

First, the new module in the project

File->new Module, see details.


Second, new Java class

Create a new Phoneinfo class with the following contents:

 PackageCom.linc.mylibrary;ImportAndroid.content.Context;ImportAndroid.net.wifi.WifiInfo;ImportAndroid.net.wifi.WifiManager;ImportAndroid.os.Build;ImportAndroid.telephony.TelephonyManager;ImportAndroid.text.format.Formatter;ImportAndroid.util.Log;ImportJava.io.BufferedReader;ImportJava.io.FileReader;ImportJava.io.IOException;/** * Created by Linc on 15-3-19. * * Public  class phoneinfo {    PrivateString TAG ="Phoneinfo";PrivateContext Mcontext;PrivateTelephonymanager Mphonemanager; Public Phoneinfo(Context context)        {Mcontext = context;    Mphonemanager = (Telephonymanager) mcontext.getsystemservice (Context.telephony_service); } PublicStringGetdeviceid() {returnMphonemanager.getdeviceid (); } PublicStringGetphonemodule() {returnBuild.model; } PublicStringGetserialnumber() {returnbuild.serial; } PublicStringGetphonenumber() {returnMphonemanager.getline1number (); } PublicStringgetmacaddress() {String result ="";        Wifimanager Wifimanager = (wifimanager) mcontext.getsystemservice (Context.wifi_service);        Wifiinfo wifiinfo = Wifimanager.getconnectioninfo ();        result = Wifiinfo.getmacaddress (); LOG.I (TAG,"Macadd:"+ result);returnResult } PublicString[]Getcpuinfo() {String str1 ="/proc/cpuinfo"; String str2 =""; String[] CpuInfo = {"",""};//1-CPU model//2-CPU frequencyString[] arrayofstring;Try{FileReader FR =NewFileReader (STR1); BufferedReader Localbufferedreader =NewBufferedReader (FR,8192);            STR2 = Localbufferedreader.readline (); arrayofstring = Str2.split ("\\s+"); for(inti =2; i < arrayofstring.length; i++) {cpuinfo[0] = cpuinfo[0] + Arrayofstring[i] +" ";            } str2 = Localbufferedreader.readline (); arrayofstring = Str2.split ("\\s+"); cpuinfo[1] + = arrayofstring[2];        Localbufferedreader.close (); }Catch(IOException e) {} log.i (TAG,"Cpuinfo:"+ cpuinfo[0] +" "+ cpuinfo[1]);returnCpuInfo; } PublicStringgettotalmemory() {String str1 ="/proc/meminfo";//system memory information fileString str2; String[] arrayofstring;LongInitial_memory =0;Try{FileReader Localfilereader =NewFileReader (STR1); BufferedReader Localbufferedreader =NewBufferedReader (Localfilereader,8192); STR2 = Localbufferedreader.readline ();//Read Meminfo first line, total system memory sizearrayofstring = Str2.split ("\\s+"); for(String num:arrayofstring) {LOG.I (str2, num +"\ T"); } initial_memory = Integer.valueof (arrayofstring[1]). Intvalue () *1024x768;//Get total system memory, in kilobytes, multiplied by 1024 to byteLocalbufferedreader.close (); }Catch(IOException e) {        }returnFormatter.formatfilesize (Mcontext, initial_memory);//byte converted to KB or MB, memory size normalized}}

and add two permissions to the module's Androidmanifest file:

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
Third, the introduction of this module in the app

Add the dependency of this module to the app's Build.gradle, as follows:

{    compile ‘com.android.support:appcompat-v7:21.0.3‘    compile project(‘:mylibrary‘)}

Add the test code to the app's mainactivity:

@Override protected void OnCreate (Bundle savedinstancestate) {Super. OnCreate(savedinstancestate);Setcontentview (R. Layout. Activity_main);Phoneinfo info = new Phoneinfo (this);Log. D(TAG,"Devices ID:"+info. Getdeviceid());Log. D(TAG,"Getphonemodule:"+info. Getphonemodule());Log. D(TAG,"Getserialnumber:"+info. Getserialnumber());Log. D(TAG,"Getphonenumber:"+info. Getphonenumber());Log. D(TAG,"getmacaddress:"+info. Getmacaddress());Log. D(TAG,"Getcpuinfo:"+info. Getcpuinfo());Log. D(TAG,"Gettotalmemory:"+info. Gettotalmemory());}
Iv. Generation of Jars

After the project is compiled, the jar is found in the following directory:

./mylibrary/build/intermediates/bundles/debug/classes.jar./mylibrary/build/intermediates/bundles/release/classes.jar
V. Removal of module

In the File->project structure, the module "minus" is removed before the right-click Delete key in the project is available.

Reference:
Http://www.cnblogs.com/wuya/p/android-studio-gradle-export-jar-assets.html
Http://www.cnblogs.com/helloandroid/articles/2210334.html
http://blog.csdn.net/hyr83960944/article/details/37519299

Android 19: Android Studio Export jar package (Module) and get Mobile Information

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.