做手机开发,想必都希望获取手机号码吧,android中有一个类android.telephony.TelephonyManager提供这个功能。
TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
numberText.setText(tm.getLine1Number());
红色标记函数能够获取本机号码。
除此之外,TelephonyManager类还提供了多种获取手机信息的函数,入imei、imsi等。、
看代码:
package com.pingan.innovation;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.TextView;
public class PhoneInfo extends Activity {
private TextView numberText;
private TextView imeiText;
private TextView onText;
private TextView snText;
private TextView imsiText;
private TextView ssText;
private TextView ntText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
numberText = (TextView) findViewById(R.id.numberText);
imeiText = (TextView) findViewById(R.id.imeiText);
onText = (TextView) findViewById(R.id.onText);
snText = (TextView) findViewById(R.id.snText);
imsiText = (TextView) findViewById(R.id.imsiText);
ssText = (TextView) findViewById(R.id.ssText);
ntText = (TextView) findViewById(R.id.ntText);
TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
numberText.setText(tm.getLine1Number());
imeiText.setText(tm.getDeviceId());
onText.setText(tm.getNetworkOperatorName());
snText.setText(tm.getSimSerialNumber());
imsiText.setText(tm.getSubscriberId());
ssText.setText(tm.getNetworkCountryIso());
ntText.setText(tm.getNetworkOperator());
}
}
以上几个函数分别获取的是:
手机号码、imei、运营商名称、sim卡序列号、IMSI、sim卡所在国家、运营商编号。
对了,AndroidManifest.xml里面还需加上:
这样才能有对手机信息的访问权限。
不过,经过在HTC Hero真机上的测试,发现获取不到手机号码。
在Ophone系统的多普达A6188上测试,同样也取不到手机号码。
Build bd = new Build();
String model = bd.MODEL;
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
StringBuilder sb = new StringBuilder();
sb.append("/nDeviceId(IMEI) = " + tm.getDeviceId());
sb.append("/nDeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion());
sb.append("/nLine1Number = " + tm.getLine1Number());
sb.append("/nNetworkCountryIso = " + tm.getNetworkCountryIso());
sb.append("/nNetworkOperator = " + tm.getNetworkOperator());
sb.append("/nNetworkOperatorName = " + tm.getNetworkOperatorName());
sb.append("/nNetworkType = " + tm.getNetworkType());
sb.append("/nPhoneType = " + tm.getPhoneType());
sb.append("/nSimCountryIso = " + tm.getSimCountryIso());
sb.append("/nSimOperator = " + tm.getSimOperator());
sb.append("/nSimOperatorName = " + tm.getSimOperatorName());
sb.append("/nSimSerialNumber = " + tm.getSimSerialNumber());
sb.append("/nSimState = " + tm.getSimState());
sb.append("/nSubscriberId(IMSI) = " + tm.getSubscriberId());
sb.append("/nVoiceMailNumber = " + tm.getVoiceMailNumber());
Log.v("DeviceInfo", sb.toString());
Log.v("Model", model);
this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));//获得电量
BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {//广播出电量值
@Override
public void onReceive(Context arg0, Intent intent) {
batterylevel = intent.getIntExtra("level", 0);
updateBatteryStatsBylevel(batterylevel);
TitleInfo.batterylevel = batterylevel;
Log.i("batterylevel_value", String.valueOf(batterylevel));
}
};
package maximyudin.BatteryLevel;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;
public class BatteryLevel extends Activity {
private TextView tvBatteryLevel;
private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
int level = intent.getIntExtra("level", 0);
int scale = intent.getIntExtra("scale", 100);
tvBatteryLevel.setText("Battery level: "
+ String.valueOf(level * 100 / scale) + "%");
}
}
};
@Override
public void onResume() {
super.onResume();
registerReceiver(mBatteryInfoReceiver, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
}
@Override
public void onPause() {
super.onPause();
unregisterReceiver(mBatteryInfoReceiver);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvBatteryLevel = (TextView) findViewById(R.id.tvBatteryLevel);
}
}
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tvBatteryLevel" android:text="Battery level:"/>
package com.mc;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.TextView;
public class TelManager extends Activity {
TextView textView01 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView01 = (TextView) this.findViewById(R.id.textView01);
// getPhoneNumber();
fetch_status();
System.out.println("-------------------onCreate");
}
public void getPhoneNumber() {
// 获取手机号、手机串号信息
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String imei = tm.getDeviceId();
String tel = tm.getLine1Number();
textView01.setText("手机串号:" + imei + "/n" + "手机号:" + tel + "/n");
}
public void 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";
textView01.setText(str);
}
}
在Android中以上机型信息在手机状态Status里
Settings->About Phone->Status
各个栏目的功能介绍如下:
Battery status:电池充电/未充电状态
Battery level:电池剩余电量
Phone number:手机序列号
Network:所处的移动网络
Signal strength:信号度
Network type:网络制式
Service state:所在服务区
Roaming:漫游/未漫游
Data access:共访问的数据大小
IMEI:IMEI码
IMEI SV:IMEI码的版本
IMSI:国际移动用户识别码
Wi-Fi Mac address:G1无线Wi-Fi网络的Mac地址。
Bluetooth address:蓝牙地址
Up time:正常运行时间
Awake Time:手机唤醒时间
android 获取当前手机型号:
Build bd = new Build();
Log.i("info", bd.MODEL+":"+bd.DEVICE+":"+bd.PRODUCT);
手机的品牌:
Build.MODEL android.os.Build.MODEL
众所周知,通过PackageManager可以获取手机端已安装的apk文件的信息,具体代码如下
view plaincopy to clipboardprint?
1. PackageManager packageManager = this.getPackageManager();
2. List
通过以上方法,可以得到手机中安装的所有应用程序,既包括了手动安装的apk包的信息,也包括了系统预装的应用软件的信息,要区分这两类软件可使用以下方法:
a.从packageInfoList获取的packageInfo,再通过packageInfo.applicationInfo获取applicationInfo。
b.判断(applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)的值,该值大于0时,表示获取的应用为系统预装的应用,反之则为手动安装的应用。
大家可以看下代码,说明已经写在注释中。
获取应用的代码:
view plaincopy to clipboardprint?
1. /**
2. * 查询手机内非系统应用
3. * @param context
4. * @return
5. */
6. public static List
7. List
8. PackageManager pManager = context.getPackageManager();
9. //获取手机内所有应用
10. List
11. for (int i = 0; i < paklist.size(); i++) {
12. PackageInfo pak = (PackageInfo) paklist.get(i);
13. //判断是否为非系统预装的应用程序
14. if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
15. // customs applications
16. apps.add(pak);
17. }
18. }
19. return apps;
20. }
获取图片、应用名、包名:
view plaincopy to clipboardprint?
1. PackageManager pManager = MessageSendActivity.this.getPackageManager();
2. List
3.
4. for(int i=0;i
5. PackageInfo pinfo = appList.get(i);
6. ShareItemInfo shareItem = new ShareItemInfo();
7. //set Icon
8. shareItem.setIcon(pManager.getApplicationIcon(pinfo.applicationInfo));
9. //set Application Name
10. shareItem.setLabel(pManager.getApplicationLabel(pinfo.applicationInfo).toString());
11. //set Package Name
12. shareItem.setPackageName(pinfo.applicationInfo.packageName);
13.
14. }
其中 ShareItemInfo 类是我本地自定义的,各位可以忽略!
另为,有人可能在找 分享应用列表,在此一并说下吧。
获取支持分享的应用的代码:
view plaincopy to clipboardprint?
1. /**
2. * 查询手机内所有支持分享的应用
3. * @param context
4. * @return
5. */
6. public static List
7. List
8. Intent intent=new Intent(Intent.ACTION_SEND,null);
9. intent.addCategory(Intent.CATEGORY_DEFAULT);
10. intent.setType("text/plain");
11. PackageManager pManager = context.getPackageManager();
12. mApps = pManager.queryIntentActivities(intent,PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
13.
14. return mApps;
15. }
由于该方法,返回的并不是 PackageInfo 对象。而是 ResolveInfo。因此获取图片、应用名、包名的方法不一样,如下:
view plaincopy to clipboardprint?
1. PackageManager pManager = MessageSendActivity.this.getPackageManager();
2.
3. /*********************查询手机内所有支持分享的应用*********************/
4. List
5.
6. for(int i=0;i
7. ResolveInfo resolve = resolveList.get(i);
8. ShareItemInfo shareItem = new ShareItemInfo();
9. //set Icon
10. shareItem.setIcon(resolve.loadIcon(pManager));
11. //set Application Name
12. shareItem.setLabel(resolve.loadLabel(pManager).toString());
13. //set Package Name
14. shareItem.setPackageName(resolve.activityInfo.packageName);
15.
16. }
总结:
通过 PackageInfo 获取具体信息方法:
包名获取方法:packageInfo.packageName
icon获取获取方法:packageManager.getApplicationIcon(applicationInfo)
应用名称获取方法:packageManager.getApplicationLabel(applicationInfo)
使用权限获取方法:packageManager.getPackageInfo(packageName,PackageManager.GET_PERMISSIONS)
.requestedPermissions
通过 ResolveInfo 获取具体信息方法:
包名获取方法:resolve.activityInfo.packageName
icon获取获取方法:resolve.loadIcon(packageManager)
应用名称获取方法:resolve.loadLabel(packageManager).toString()
有时候需要统计手机的型号和版本号,利用程序可以获取到相应的手机信息,对比两部手机发现,厂商不同,某个信息显示方式也不尽相同,具体见:
String phoneInfo = "Product: " + android.os.Build.PRODUCT;
phoneInfo += ", CPU_ABI: " + android.os.Build.CPU_ABI;
phoneInfo += ", TAGS: " + android.os.Build.TAGS;
phoneInfo += ", VERSION_CODES.BASE: " + android.os.Build.VERSION_CODES.BASE;
phoneInfo += ", MODEL: " + android.os.Build.MODEL;
phoneInfo += ", SDK: " + android.os.Build.VERSION.SDK;
phoneInfo += ", VERSION.RELEASE: " + android.os.Build.VERSION.RELEASE;
phoneInfo += ", DEVICE: " + android.os.Build.DEVICE;
phoneInfo += ", DISPLAY: " + android.os.Build.DISPLAY;
phoneInfo += ", BRAND: " + android.os.Build.BRAND;
phoneInfo += ", BOARD: " + android.os.Build.BOARD;
phoneInfo += ", FINGERPRINT: " + android.os.Build.FINGERPRINT;
phoneInfo += ", ID: " + android.os.Build.ID;
phoneInfo += ", MANUFACTURER: " + android.os.Build.MANUFACTURER;
phoneInfo += ", USER: " + android.os.Build.USER;
// Toast.makeText(this, phoneInfo, Toast.LENGTH_LONG).show();
TextView t = (TextView) findViewById(R.id.main_phoneinfo);
t.setText(phoneInfo);
好久没有写东西了,今天在做pv log记录,需要记录机器的os version, 机器的wifi IP address, 当前时间等等。 调查了一下,分享分享:
package com.osip;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.TextView;
public class Osip extends Activity {
private TextView osVersion, clientIp, date;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
osVersion = (TextView) findViewById(R.id.os_version);
clientIp = (TextView) findViewById(R.id.client_ip);
date = (TextView) findViewById(R.id.date);
String format = "yyyyMMdd.HHmmss.SSSZ";// 带毫秒和时区的时间格式
String version = getosVersion();
String ip = getIp();
String cDate = getDate(new Date(), format);
osVersion.setText(version);
clientIp.setText(ip);
date.setText(cDate.substring(0, 22));
}
private String getDate(Date date, String format) {
DateFormat dateFormat = new SimpleDateFormat(format);
return dateFormat.format(date);
}
// 取得device的IP address
private String getIp() {
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
// 格式化IP address,例如:格式化前:1828825280,格式化后:192.168.1.109
String ip = String.format("%d.%d.%d.%d",
(ipAddress & 0xff),
(ipAddress >> 8 & 0xff),
(ipAddress >> 16 & 0xff),
(ipAddress >> 24 & 0xff));
return ip;
}
//获取device的os version
private String getosVersion() {
String version = android.os.Build.VERSION.RELEASE;
return version;
}
}
如果需要通过android SDK获取手机相关信息。需要通过如下方式获取:
TelephonyManager telephonyManager=(TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
String imei=telephonyManager.getDeviceId();
不过,光这么写,会有类似如何查看android产生的异常的报错,主要是因为android的权限需要打开,在AndroidManifest.xml文件中增加:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
就可以拿到手机的imei号了。
TelephonyManager还有其他方法可以调用,获取手机的其他信息。
比如获取手机号码,可以这样:
telephonyManager.getLine1Number();
不过在g1手机上测试,使用移动全球通的sim卡,无法得到手机号码,是个空字符串。
获取手机的sim卡号:
telephonyManager.getSimSerialNumber();
这个可以在上述环境下得到。
获取客户id,在gsm中是imsi号:
telephonyManager.getSubscriberId();
这个也能在商户环境得到。
在开发手机应用时,开发者最关心的是开发的手机软件有多少用户安装,并且正在使用。这就需要识别手机的唯一性。目前我了解的有两种方法,
第一种方法:
android获取手机imei号的方法在在android系统中获取imei号和其他手机信息一文中做了详细的介绍。其中android在2.0以上的版本中,获取手机imei号已经不需要相应的读取手机信息的权限了。
但是目前1.6以下的版本用户占用户的近50%,如果想要让1.6以下版本的用户也可以获取imei号,那就要添加权限。
好处是唯一标识手机,缺点是可能需要相应的用户权限。
第二种方法:
可以获取android id号,如果一个手机设备第一次启动随即产生的一个数字,如果系统改变,该号可能会改变。
android id号解释:http://androidappdocs-staging.appspot.com/reference/android/provider/Settings.Secure.html#ANDROID_ID
获取方法:
String t=android.provider.Settings.System.getString(getContentResolver(), "android_id");
Log.i("huilurry","android_id="+t);
Ms测试结果:
07-13 13:12:49.835: INFO/huilurry(17455): android_id=20014289e714f20c
好处是所有版本都试用,缺点是可能会和其他机子重复或本机改变。