Android TelephonyManager教程含代碼

Android TelephonyManager教程

android.telephony.TelephonyManager 類提供關于電信服務的信息,例如用戶ID、SIM卡序列號、手機網絡類型等。愛掏網 - it200.com此外,您還可以確定手機狀態等。愛掏網 - it200.com

Android TelephonyManager示例

看一個簡單的TelephonyManager示例,打印出電信服務的信息。愛掏網 - it200.com

activity_main.xml

從組件庫中拖動一個textview,現在xml文件將如下所示:

<RelativeLayout xmlns:androclass="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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="38dp"
        android:layout_marginTop="30dp"
        android:text="Phone Details:" />

</RelativeLayout>

Activity類

現在,編寫代碼以顯示有關電話服務的信息。愛掏網 - it200.com

package com.javatpoint.telephonymanager;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
   TextView textView1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView1=(TextView)findViewById(R.id.textView1);

        //Get the instance of TelephonyManager
        TelephonyManager  tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

        //Calling the methods of TelephonyManager the returns the information
        String IMEINumber=tm.getDeviceId();
        String subscriberID=tm.getDeviceId();
        String SIMSerialNumber=tm.getSimSerialNumber();
        String networkCountryISO=tm.getNetworkCountryIso();
        String SIMCountryISO=tm.getSimCountryIso();
        String softwareVersion=tm.getDeviceSoftwareVersion();
        String voiceMailNumber=tm.getVoiceMailNumber();

        //Get the phone type
        String strphoneType="";

        int phoneType=tm.getPhoneType();

        switch (phoneType) 
        {
                case (TelephonyManager.PHONE_TYPE_CDMA):
                           strphoneType="CDMA";
                               break;
                case (TelephonyManager.PHONE_TYPE_GSM): 
                           strphoneType="GSM";              
                               break;
                case (TelephonyManager.PHONE_TYPE_NONE):
                            strphoneType="NONE";              
                                break;
         }

        //getting information if phone is in roaming
        boolean isRoaming=tm.isNetworkRoaming();

        String info="Phone Details:\n";
        info+="\n IMEI Number:"+IMEINumber;
        info+="\n SubscriberID:"+subscriberID;
        info+="\n Sim Serial Number:"+SIMSerialNumber;
        info+="\n Network Country ISO:"+networkCountryISO;
        info+="\n SIM Country ISO:"+SIMCountryISO;
        info+="\n Software Version:"+softwareVersion;
        info+="\n Voice Mail Number:"+voiceMailNumber;
        info+="\n Phone Network Type:"+strphoneType;
        info+="\n In Roaming? :"+isRoaming;

        textView1.setText(info);//displaying the information in the textView
    }


}

AndroidManifest.xml

您需要在AndroidManifest.xml文件中提供 READ_PHONE_STATE 權限。愛掏網 - it200.com

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:androclass="http://schemas.android.com/apk/res/android"
    package="com.javatpoint.telephonymanager"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.javatpoint.telephonymanager.MainActivity"
            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>

</manifest>

輸出:

聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
發表評論
更多 網友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: jizz18高清视频| 日本chinese人妖video| 欧美性大战久久久久久久| 男女午夜免费视频| 免费A级毛片无码久久版| 日韩人妻无码专区精品| www卡一卡二卡三| 7x7x7x免费在线观看| 999在线视频精品免费播放观看| a级毛片免费网站| aⅴ在线免费观看| 9久9久热精品视频在线观看| 一区二区三区www| xxxcom在线观看| hdmaturetube熟女xx视频韩国| yellow中文字幕在线高清| ljr绿巨人地址| 99久热只有精品视频免费看| 99久久免费观看| 18级成人毛片免费观看| 1000部国产成人免费视频| 国产精品亚洲综合五月天| 香蕉狠狠再啪线视频| 色戒7分27秒大尺度在线| 美妇与子伦亲小说| 精品一区二区三区波多野结衣| 精品爆乳一区二区三区无码av | 国产色诱视频在线观看| 国自产拍亚洲免费视频| 国产精品福利久久香蕉中文| 国产精品日本一区二区不卡视频| 国产精品无码无卡无需播放器 | 国产综合激情在线亚洲第一页| 国产精品视频网| 国产欧美国产精品第一区| 国产农村妇女毛片精品久久| 国产亚洲精品国产福利在线观看| 四虎www成人影院| 免费人成动漫在线播放r18| 亚洲欧美另类久久久精品能播放的| 国产一区二区不卡老阿姨|