不废话
java代码
package com.example.test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.format.Formatter;
import android.util.DisplayMetrics;
import android.widget.TextView;
public class MainActivity extends Activity {
TelephonyManager myTelephonyManager;
private TextView phone_number;
private TextView screen_size;
private TextView apps;
private TextView TotalMemory;
private TextView AvailMemory;
private TextView MacAddress;
private TextView IMEI;
private TextView IMSI;
private TextView TYPE;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int width=getWindowManager().getDefaultDisplay().getWidth();//另外一种获取屏幕宽高方式,就当是附送的啦。
int height=getWindowManager().getDefaultDisplay().getHeight();
screen_size = (TextView) findViewById(R.id.screen_size);
screen_size.setText(getWidthHeight());
phone_number = (TextView) findViewById(R.id.phone_number);
phone_number.setText(getInfoNumer());
apps = (TextView) findViewById(R.id.apps);
apps.setText(getAllApp());
TotalMemory = (TextView) findViewById(R.id.TotalMemory);
TotalMemory.setText(getTotalMemory());
MacAddress = (TextView) findViewById(R.id.MacAddress);
MacAddress.setText(getMacAddress());
AvailMemory = (TextView) findViewById(R.id.AvailMemory);
AvailMemory.setText(getAvailMemory());
IMEI = (TextView) findViewById(R.id.IMEI);
IMEI.setText(getInfoIMEI());
IMSI = (TextView) findViewById(R.id.IMSI);
IMSI.setText(getInfoIMSI());
TYPE = (TextView) findViewById(R.id.TYPE);
TYPE.setText(getInfoTYPE());
}
/****************** 获取手机的电话号码 *****************************/
private String getInfoNumer() {
myTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
// 手机号码,有的可得,有的不可得
String numer = myTelephonyManager.getLine1Number();
numer = "手机的号码是:" + numer;
System.out.println(numer);
return numer;
}
/****************** 获取手机的宽度与高度 *****************************/
private String getWidthHeight() {
// 获取屏幕宽度
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels;
String text_wh = "当前手机屏幕 宽度是:" + screenWidth + " 高度是:"
+ screenHeight;
System.out.println(text_wh);
return text_wh;
}
/************ 获取手机安装的应用信息(排除系统自带) ************/
private String getAllApp() {
String result = "";
List<PackageInfo> packages = getPackageManager()
.getInstalledPackages(0);
for (PackageInfo i : packages) {
if ((i.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
result += i.applicationInfo.loadLabel(getPackageManager())
.toString() + ",";
}
}
int num = packages.size();
result = "当前手机的软件共有" + num + "个\n" + "手机的安装应用信息如下:\n"
+ result.substring(0, result.length() - 1);
System.out.println(result);
return result;
}
/************** 获取手机 总内存 ********************/
private String getTotalMemory() {
String[] result = { "", "" }; // 1-total 2-avail
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
// myActivityManager.getMemoryInfo(mi);
long mTotalMem = 0;
long mAvailMem = mi.availMem;
String str1 = "/proc/meminfo";
String str2;
String[] arrayOfString;
try {
FileReader localFileReader = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(
localFileReader, 8192);
str2 = localBufferedReader.readLine();
arrayOfString = str2.split("\\s+");
mTotalMem = Integer.valueOf(arrayOfString[1]).intValue() * 1024;
localBufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
result[0] = Formatter.formatFileSize(this, mTotalMem);
result[1] = Formatter.formatFileSize(this, mAvailMem);
result[0] = "手机 总内存:" + result[0];
System.out.println(result[0]);
return result[0];
}
/****************** 获取手机可用内存 **********************/
private String getAvailMemory() {
String[] result = { "", "" }; // 1-total 2-avail
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
// myActivityManager.getMemoryInfo(mi);
long mTotalMem = 0;
long mAvailMem = mi.availMem;
String str1 = "/proc/meminfo";
String str2;
String[] arrayOfString;
try {
FileReader localFileReader = new FileReader(str1);
BufferedReader localBufferedReader = new BufferedReader(
localFileReader, 8192);
str2 = localBufferedReader.readLine();
arrayOfString = str2.split("\\s+");
mTotalMem = Integer.valueOf(arrayOfString[1]).intValue() * 1024;
localBufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
result[0] = Formatter.formatFileSize(this, mTotalMem);
result[1] = Formatter.formatFileSize(this, mAvailMem);
result[1] = "手机 可用内存:" + result[1];
System.out.println(result[1]);
return result[1];
}
/************ 获取手机MAC地址 *************/
private String getMacAddress() {
String result = "";
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
result = wifiInfo.getMacAddress();
result = "手机的MAC地址是:" + result;
System.out.println(result);
return result;
}
/************** IMEI号 *************/
private String getInfoIMEI() {
myTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
String imei = myTelephonyManager.getDeviceId();
imei = "手机的IMEI号码是:" + imei;
System.out.println(imei);
return imei;
}
/************** IESI号 ****************/
private String getInfoIMSI() {
myTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
String imsi = myTelephonyManager.getSubscriberId();
imsi = "手机的IMSI号码是:" + imsi;
System.out.println(imsi);
return imsi;
}
/************* 手机型号 ***************/
private String getInfoTYPE() {
myTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
String mtype = android.os.Build.MODEL; // 手机型号
mtype = "手机的MTYPE号码是:" + mtype;
System.out.println(mtype);
return mtype;
}
}
xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/phone_number"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/screen_size"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/apps"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/TotalMemory"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/AvailMemory"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/MacAddress"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/IMEI"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/IMSI"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/TYPE"
android:text="@string/hello_world" />
</LinearLayout>
最后还需要配置权限啊
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>