android 获取手机相关信息

时间:2022-06-02 19:04:10

android.jar里有一些类提供了手机相关的信息,比如 android.telephony.TelephonyManager,android.os.Build等,如果想查看更多的信息,可以去frameworks里查看更详细的类关系。现在拷贝一段代码,简单介绍下手机的一些信息,代码如下:

import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.TextView;

public class PhoneInfoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.text);
tv.setText(fetch_status());
getLocalMsg();
getPhoneInfo();
}

public String fetch_status() {

TelephonyManager tm = (TelephonyManager) this

.getSystemService(Context.TELEPHONY_SERVICE);//

String str = "";

str += "DeviceId(IMEI) = " + tm.getDeviceId() + "\n";

str += "DeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion()
+ "/n";

str += "Line1Number = " + tm.getLine1Number() + "\n";

str += "NetworkCountryIso = " + tm.getNetworkCountryIso() + "\n";

str += "NetworkOperator = " + tm.getNetworkOperator() + "\n";

str += "NetworkOperatorName = " + tm.getNetworkOperatorName() + "\n";

str += "NetworkType = " + tm.getNetworkType() + "\n";

str += "honeType = " + tm.getPhoneType() + "\n";

str += "SimCountryIso = " + tm.getSimCountryIso() + "\n";

str += "SimOperator = " + tm.getSimOperator() + "\n";

str += "SimOperatorName = " + tm.getSimOperatorName() + "\n";

str += "SimSerialNumber = " + tm.getSimSerialNumber() + "\n";

str += "SimState = " + tm.getSimState() + "\n";

str += "SubscriberId(IMSI) = " + tm.getSubscriberId() + "\n";

str += "VoiceMailNumber = " + tm.getVoiceMailNumber() + "\n";
return str;

}

// 获取手机当前系统的信息
public void getLocalMsg() {

String str = Locale.getDefault().getLanguage();// 获取手机的当前系统语言
Log.v("TAG", "language" + str);
str = Locale.getDefault().getCountry();// 获取国家代号
Log.v("TAG", "countryNumber" + str);

}

// 获取手机信息
private void getPhoneInfo() {
StringBuffer sb = new StringBuffer();
String language = getResources().getConfiguration().locale.getCountry();
sb.append(language).append("\n");
DisplayMetrics dm = getResources().getDisplayMetrics();

int w = dm.widthPixels;

int h = dm.heightPixels;

String screen_size = w + "*" + h;
sb.append(screen_size).append("\n");
String device = Build.MODEL;// 手机型号
sb.append(device).append("\n");
String osver = android.os.Build.VERSION.SDK;// 手机SDK版本 - level - 4
sb.append(osver).append("\n");
String relea = android.os.Build.VERSION.RELEASE;// 手机SDK版本 1.6
sb.append(relea).append("\n");
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

String imei = telephonyManager.getDeviceId();// 手机IMEI
sb.append(imei).append("\n");
String mobileno = telephonyManager.getLine1Number();// 手机号码
sb.append(mobileno).append("\n");
Log.v("TAG", "countryNumber" + sb.toString());
}
}
关于TelephonyManager的作用也比较容易理解,见下:

/**
* Provides access to information about the telephony services on
* the device. Applications can use the methods in this class to
* determine telephony services and states, as well as to access some
* types of subscriber information. Applications can also register
* a listener to receive notification of telephony state changes.
* <p>
* You do not instantiate this class directly; instead, you retrieve
* a reference to an instance through
* {@link android.content.Context#getSystemService
* Context.getSystemService(Context.TELEPHONY_SERVICE)}.
* <p>
* Note that access to some telephony information is
* permission-protected. Your application cannot access the protected
* information unless it has the appropriate permissions declared in
* its manifest file. Where permissions apply, they are noted in the
* the methods through which you access the protected information.
*/
基本意思:该类提供了访问手机的电话服务的一些信息和状态,比如可以注册监听器来获取电话状态。一般不直接实例化该类,而是通过Context.getSystemService(Context.TELEPHONY_SERVICE)来获取服务。

因为电话服务属于手机核心功能服务,所以获取很多服务需要特定权限,因此使用的时候需要声明权限才能使用。

本例则需要添加权限:

    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>