Android中TelephonyManager類的用法案例詳解
本文以案例形式分析了Android中TelephonyManager類的用法。分享給大家供大家參考。具體如下:
目錄結(jié)構(gòu):

main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView android:fillViewport="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</ScrollView>
</LinearLayout>
array.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="listItem">
<item>設(shè)備編號(hào)</item>
<item>SIM卡國(guó)別</item>
<item>SIM卡序列號(hào)</item>
<item>SIM卡狀態(tài)</item>
<item>軟件版本</item>
<item>網(wǎng)絡(luò)運(yùn)營(yíng)商代號(hào)</item>
<item>網(wǎng)絡(luò)運(yùn)營(yíng)商名稱</item>
<item>手機(jī)制式</item>
<item>設(shè)備當(dāng)前位置</item>
</string-array>
<string-array name="simState">
<item>狀態(tài)未知</item>
<item>無(wú)SIM卡</item>
<item>被PIN加鎖</item>
<item>被PUK加鎖</item>
<item>被NetWork PIN加鎖</item>
<item>已準(zhǔn)備好</item>
</string-array>
<string-array name="phoneType">
<item>未知</item>
<item>GSM</item>
<item>CDMA</item>
</string-array>
</resources>
清單文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ljq.activity" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".TelephonyManagerActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
</manifest>
TelephonyManagerActivity類:
package com.ljq.activity;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class TelephonyManagerActivity extends Activity {
private ListView listView=null;
private TelephonyManager tm=null;
private String[] phoneType=null;
private String[] simState=null;
private String[] listItems=null;
ArrayList<String> listValues=new ArrayList<String>();
BaseAdapter adapter=new BaseAdapter(){
public int getCount() {
return listItems.length;
}
public Object getItem(int position) {
return listItems[position];
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout ll=new LinearLayout(TelephonyManagerActivity.this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView tvItem=new TextView(TelephonyManagerActivity.this);
tvItem.setTextSize(24);
tvItem.setText(listItems[position]);
tvItem.setGravity(Gravity.LEFT);//設(shè)置在父容器中的對(duì)齊方式
ll.addView(tvItem);
TextView tvValue=new TextView(TelephonyManagerActivity.this);
tvValue.setTextSize(18); //設(shè)置字體大小
tvValue.setText(listValues.get(position)); //設(shè)置顯示的內(nèi)容
tvValue.setPadding(0, 0, 10, 10); //設(shè)置四周邊界
tvValue.setGravity(Gravity.RIGHT);
ll.addView(tvValue);
return ll;
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listItems=getResources().getStringArray(R.array.listItem);
simState=getResources().getStringArray(R.array.simState);
phoneType=getResources().getStringArray(R.array.phoneType);
tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
listView=(ListView)findViewById(R.id.listView);
initListValues();
listView.setAdapter(adapter);
}
/**
* 獲取各個(gè)數(shù)據(jù)項(xiàng)的值
*/
public void initListValues(){
listValues.add(tm.getDeviceId());//獲取設(shè)備編號(hào)
listValues.add(tm.getSimCountryIso());//獲取SIM卡國(guó)別
listValues.add(tm.getSimSerialNumber());//獲取SIM卡序列號(hào)
listValues.add(simState[tm.getSimState()]);//獲取SIM卡狀態(tài)
listValues.add((tm.getDeviceSoftwareVersion()!=null?tm.getDeviceSoftwareVersion():"未知")); //獲取軟件版本
listValues.add(tm.getNetworkOperator());//獲取網(wǎng)絡(luò)運(yùn)營(yíng)商代號(hào)
listValues.add(tm.getNetworkOperatorName());//獲取網(wǎng)絡(luò)運(yùn)營(yíng)商名稱
listValues.add(phoneType[tm.getPhoneType()]);//獲取手機(jī)制式
listValues.add(tm.getCellLocation().toString());//獲取設(shè)備當(dāng)前位置
}
}
運(yùn)行結(jié)果:

希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
android中Intent傳值與Bundle傳值的區(qū)別詳解
本篇文章是對(duì)android中Intent傳值與Bundle傳值的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android編程開發(fā)實(shí)現(xiàn)帶進(jìn)度條和百分比的多線程下載
這篇文章主要介紹了Android編程開發(fā)實(shí)現(xiàn)帶進(jìn)度條和百分比的多線程下載,總結(jié)了前面關(guān)于Java多線程下載的技巧,實(shí)例分析了Android實(shí)現(xiàn)帶百分比和進(jìn)度條的多線程下載技巧,需要的朋友可以參考下2015-12-12
Android自定義View實(shí)現(xiàn)餅狀圖帶動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)餅狀圖帶動(dòng)畫效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
android 實(shí)現(xiàn)控件左右或上下抖動(dòng)教程
這篇文章主要介紹了android 實(shí)現(xiàn)控件左右或上下抖動(dòng)教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android Service總結(jié)及詳細(xì)介紹
android廣角相機(jī)畸變校正算法和實(shí)現(xiàn)示例
完美解決安卓jni項(xiàng)目會(huì)刪除其他so文件的問(wèn)題

