做手機(jī)開(kāi)發(fā),想必都希望獲取手機(jī)號(hào)碼吧,android中有一個(gè)類android.telephony.TelephonyManager提供這個(gè)功能。 TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE); 紅色標(biāo)記函數(shù)能夠獲取本機(jī)號(hào)碼。 除此之外,TelephonyManager類還提供了多種獲取手機(jī)信息的函數(shù),入imei、imsi等。、 看代碼: package com.pingan.innovation; import android.app.Activity; public class PhoneInfo extends Activity { 以上幾個(gè)函數(shù)分別獲取的是: 手機(jī)號(hào)碼、imei、運(yùn)營(yíng)商名稱、sim卡序列號(hào)、IMSI、sim卡所在國(guó)家、運(yùn)營(yíng)商編號(hào)。
對(duì)了,AndroidManifest.xml里面還需加上:<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 這樣才能有對(duì)手機(jī)信息的訪問(wèn)權(quán)限。
不過(guò),經(jīng)過(guò)在HTC Hero真機(jī)上的測(cè)試,發(fā)現(xiàn)獲取不到手機(jī)號(hào)碼。 在Ophone系統(tǒng)的多普達(dá)A6188上測(cè)試,同樣也取不到手機(jī)號(hào)碼。
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); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas./apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/tvBatteryLevel" android:text="Battery level:"/> </LinearLayout>
Android獲取手機(jī)卡相關(guān)信息<!-- 聯(lián)網(wǎng)權(quán)限 -->
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() { // 獲取手機(jī)號(hào)、手機(jī)串號(hào)信息 TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); String imei = tm.getDeviceId(); String tel = tm.getLine1Number(); textView01.setText("手機(jī)串號(hào):" + imei + "\n" + "手機(jī)號(hào):" + 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中以上機(jī)型信息在手機(jī)狀態(tài)Status里
android 獲取當(dāng)前手機(jī)型號(hào): Build bd = new Build();
手機(jī)的品牌: Build.MODEL android.os.Build.MODEL
【Android】獲取手機(jī)中已安裝apk文件信息(PackageInfo、ResolveInfo)(應(yīng)用圖片、應(yīng)用名、包名等) 收藏眾所周知,通過(guò)PackageManager可以獲取手機(jī)端已安裝的apk文件的信息,具體代碼如下 view plaincopy to clipboardprint? 1. PackageManager packageManager = this.getPackageManager(); 2. List<PackageInfo> packageInfoList = packageManager.getInstalledPackages(0);
通過(guò)以上方法,可以得到手機(jī)中安裝的所有應(yīng)用程序,既包括了手動(dòng)安裝的apk包的信息,也包括了系統(tǒng)預(yù)裝的應(yīng)用軟件的信息,要區(qū)分這兩類軟件可使用以下方法: a.從packageInfoList獲取的packageInfo,再通過(guò)packageInfo.applicationInfo獲取applicationInfo。 b.判斷(applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)的值,該值大于0時(shí),表示獲取的應(yīng)用為系統(tǒng)預(yù)裝的應(yīng)用,反之則為手動(dòng)安裝的應(yīng)用。 大家可以看下代碼,說(shuō)明已經(jīng)寫在注釋中。 獲取應(yīng)用的代碼: view plaincopy to clipboardprint? 1. /** 2. * 查詢手機(jī)內(nèi)非系統(tǒng)應(yīng)用 3. * @param context 4. * @return 5. */ 6. public static List<PackageInfo> getAllApps(Context context) { 7. List<PackageInfo> apps = new ArrayList<PackageInfo>(); 8. PackageManager pManager = context.getPackageManager(); 9. //獲取手機(jī)內(nèi)所有應(yīng)用 10. List<PackageInfo> paklist = pManager.getInstalledPackages(0); 11. for (int i = 0; i < paklist.size(); i++) { 12. PackageInfo pak = (PackageInfo) paklist.get(i); 13. //判斷是否為非系統(tǒng)預(yù)裝的應(yīng)用程序 14. if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) { 15. // customs applications 16. apps.add(pak); 17. } 18. } 19. return apps; 20. }
獲取圖片、應(yīng)用名、包名:
view plaincopy to clipboardprint? 1. PackageManager pManager = MessageSendActivity.this.getPackageManager(); 2. List<PackageInfo> appList = Utils.getAllApps(MessageSendActivity.this); 3. 4. for(int i=0;i<appList.size();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 類是我本地自定義的,各位可以忽略! 另為,有人可能在找 分享應(yīng)用列表,在此一并說(shuō)下吧。 獲取支持分享的應(yīng)用的代碼: view plaincopy to clipboardprint? 1. /** 2. * 查詢手機(jī)內(nèi)所有支持分享的應(yīng)用 3. * @param context 4. * @return 5. */ 6. public static List<ResolveInfo> getShareApps(Context context){ 7. List<ResolveInfo> mApps = new ArrayList<ResolveInfo>(); 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 對(duì)象。而是 ResolveInfo。因此獲取圖片、應(yīng)用名、包名的方法不一樣,如下: view plaincopy to clipboardprint? 1. PackageManager pManager = MessageSendActivity.this.getPackageManager(); 2. 3. /*********************查詢手機(jī)內(nèi)所有支持分享的應(yīng)用*********************/ 4. List<ResolveInfo> resolveList = Utils.getShareApps(MessageSendActivity.this); 5. 6. for(int i=0;i<resolveList.size();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. }
總結(jié): 通過(guò) PackageInfo 獲取具體信息方法:
包名獲取方法:packageInfo.packageName icon獲取獲取方法:packageManager.getApplicationIcon(applicationInfo) 應(yīng)用名稱獲取方法:packageManager.getApplicationLabel(applicationInfo) 使用權(quán)限獲取方法:packageManager.getPackageInfo(packageName,PackageManager.GET_PERMISSIONS) .requestedPermissions
通過(guò) ResolveInfo 獲取具體信息方法:
包名獲取方法:resolve.activityInfo.packageName icon獲取獲取方法:resolve.loadIcon(packageManager) 應(yīng)用名稱獲取方法:resolve.loadLabel(packageManager).toString()
Android獲取手機(jī)和系統(tǒng)版本等信息的代碼有時(shí)候需要統(tǒng)計(jì)手機(jī)的型號(hào)和版本號(hào),利用程序可以獲取到相應(yīng)的手機(jī)信息,對(duì)比兩部手機(jī)發(fā)現(xiàn),廠商不同,某個(gè)信息顯示方式也不盡相同,具體見(jiàn):
String phoneInfo = "Product: " + android.os.Build.PRODUCT;
android 獲取手機(jī)信息(device ip,os version etc.)好久沒(méi)有寫東西了,今天在做pv log記錄,需要記錄機(jī)器的os version, 機(jī)器的wifi IP address, 當(dāng)前時(shí)間等等。 調(diào)查了一下,分享分享:
在android系統(tǒng)中獲取imei號(hào)和其他手機(jī)信息如果需要通過(guò)android SDK獲取手機(jī)相關(guān)信息。需要通過(guò)如下方式獲?。?/span> TelephonyManager telephonyManager=(TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
String imei=telephonyManager.getDeviceId();
不過(guò),光這么寫,會(huì)有類似如何查看android產(chǎn)生的異常的報(bào)錯(cuò),主要是因?yàn)?span lang="EN-US">android的權(quán)限需要打開(kāi),在AndroidManifest.xml文件中增加: <uses-permission android:name="android.permission.READ_PHONE_STATE" />
就可以拿到手機(jī)的imei號(hào)了。 TelephonyManager還有其他方法可以調(diào)用,獲取手機(jī)的其他信息。 比如獲取手機(jī)號(hào)碼,可以這樣: telephonyManager.getLine1Number(); 不過(guò)在g1手機(jī)上測(cè)試,使用移動(dòng)全球通的sim卡,無(wú)法得到手機(jī)號(hào)碼,是個(gè)空字符串。 獲取手機(jī)的sim卡號(hào): telephonyManager.getSimSerialNumber(); 這個(gè)可以在上述環(huán)境下得到。 獲取客戶id,在gsm中是imsi號(hào): telephonyManager.getSubscriberId(); 這個(gè)也能在商戶環(huán)境得到。
android手機(jī)獲取手機(jī)唯一識(shí)別號(hào)在開(kāi)發(fā)手機(jī)應(yīng)用時(shí),開(kāi)發(fā)者最關(guān)心的是開(kāi)發(fā)的手機(jī)軟件有多少用戶安裝,并且正在使用。這就需要識(shí)別手機(jī)的唯一性。目前我了解的有兩種方法,
第一種方法: android獲取手機(jī)imei號(hào)的方法在在android系統(tǒng)中獲取imei號(hào)和其他手機(jī)信息一文中做了詳細(xì)的介紹。其中android在2.0以上的版本中,獲取手機(jī)imei號(hào)已經(jīng)不需要相應(yīng)的讀取手機(jī)信息的權(quán)限了。 <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 但是目前1.6以下的版本用戶占用戶的近50%,如果想要讓1.6以下版本的用戶也可以獲取imei號(hào),那就要添加權(quán)限。 好處是唯一標(biāo)識(shí)手機(jī),缺點(diǎn)是可能需要相應(yīng)的用戶權(quán)限。 第二種方法: 可以獲取android id號(hào),如果一個(gè)手機(jī)設(shè)備第一次啟動(dòng)隨即產(chǎn)生的一個(gè)數(shù)字,如果系統(tǒng)改變,該號(hào)可能會(huì)改變。
android id號(hào)解釋:http://androidappdocs-staging./reference/android/provider/Settings.Secure.html#ANDROID_ID
獲取方法: String t=android.provider.Settings.System.getString(getContentResolver(), "android_id");
Ms測(cè)試結(jié)果: 07-13 13:12:49.835: INFO/huilurry(17455): android_id=20014289e714f20c 好處是所有版本都試用,缺點(diǎn)是可能會(huì)和其他機(jī)子重復(fù)或本機(jī)改變。 |
|