Android实战技巧之十九:android studio导出jar包(Module)并获得手机信息
AS中并没有独立的Module 工程,可是能够在普通的Project中增加Module。所谓的Module就是我们通常所指的模块化的一个单元。并经常以jar包的形式存在。以下以一个获取手机信息的样例演示AS中的模块化。
一、项目中新建Module
File—>New Module,具体见下图。
二、新建Java类
新建一个PhoneInfo类,内容例如以下:
package com.linc.mylibrary;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.format.Formatter;
import android.util.Log;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
* Created by linc on 15-3-19.
*/
public class PhoneInfo {
private String TAG = "PhoneInfo";
private Context mContext;
private TelephonyManager mPhoneManager;
public PhoneInfo(Context context) {
mContext = context;
mPhoneManager = (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);
}
public String getDeviceId() {
return mPhoneManager.getDeviceId();
}
public String getPhoneModule() {
return Build.MODEL;
}
public String getSerialNumber() {
return Build.SERIAL;
}
public String getPhoneNumber() {
return mPhoneManager.getLine1Number();
}
public String getMacAddress(){
String result = "";
WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
result = wifiInfo.getMacAddress();
Log.i(TAG, "macAdd:" + result);
return result;
}
public String[] getCpuInfo() {
String str1 = "/proc/cpuinfo";
String str2 = "";
String[] cpuInfo = {"", ""}; //1-cpu型号 //2-cpu频率
String[] arrayOfString;
try {
FileReader fr = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
str2 = localBufferedReader.readLine();
arrayOfString = str2.split("\\s+");
for (int i = 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]);
return cpuInfo;
}
public String getTotalMemory() {
String str1 = "/proc/meminfo";// 系统内存信息文件
String str2;
String[] arrayOfString;
long initial_memory = 0;
try {
FileReader localFileReader = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(
localFileReader, 8192);
str2 = localBufferedReader.readLine();// 读取meminfo第一行。系统总内存大小
arrayOfString = str2.split("\\s+");
for (String num : arrayOfString) {
Log.i(str2, num + "\t");
}
initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 获得系统总内存,单位是KB,乘以1024转换为Byte
localBufferedReader.close();
} catch (IOException e) {
}
return Formatter.formatFileSize(mContext, initial_memory);// Byte转换为KB或者MB。内存大小规格化
}
}
并在Module的AndroidManifest文件里增加两个权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
三、app中引入此module
在app的build.gradle中增加此module的dependency。例如以下:
dependencies {
compile 'com.android.support:appcompat-v7:21.0.3'
compile project(':mylibrary')
}
在app的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());
}
四、jar的生成
项目编译之后jar会在以下的文件夹找到:
./mylibrary/build/intermediates/bundles/debug/classes.jar
./mylibrary/build/intermediates/bundles/release/classes.jar
五、Module的移除
先要在File—>Project Structure中将此module“减“掉后才干在项目中Module右键的Delete键可用。
參考:
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