Android实战技巧之十九 android studio导出jar包 Module 并获得手机信息
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
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,内存大小规格化 }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
并在Module的AndroidManifest文件中加入两个权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
- 1
- 2
三、app中引入此module
在app的build.gradle中加入此module的dependency,如下:
dependencies { compile 'com.android.support:appcompat-v7:21.0.3' compile project(':mylibrary')}
- 1
- 2
- 3
- 4
在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()); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
四、jar的生成
项目编译之后jar会在下面的目录找到:
./mylibrary/build/intermediates/bundles/debug/classes.jar./mylibrary/build/intermediates/bundles/release/classes.jar
- 1
- 2
五、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
给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
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,内存大小规格化 }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
并在Module的AndroidManifest文件中加入两个权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
- 1
- 2
三、app中引入此module
在app的build.gradle中加入此module的dependency,如下:
dependencies { compile 'com.android.support:appcompat-v7:21.0.3' compile project(':mylibrary')}
- 1
- 2
- 3
- 4
在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()); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
四、jar的生成
项目编译之后jar会在下面的目录找到:
./mylibrary/build/intermediates/bundles/debug/classes.jar./mylibrary/build/intermediates/bundles/release/classes.jar
- 1
- 2
五、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