Android開発 - 電話番号やネットワーク回線などの端末情報を取得

2011/07/12 23:12Update
TAGS: Android | 開発 | 電話番号 | ネットワーク | 回線 | TelephonyManager | SIM | デバイス

Androidでは、TelephonyManagerというクラスで電話番号やネットワーク回線、通話状況などの端末情報を取得します。

次はその取得方法(例)です。

レイアウト定義



systeminfo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <TextView android:text="TextView" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    
</LinearLayout>


Activityクラス



GetTelephonyActivity.java
package com.syboos.android;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.TextView;

public class GetTelephonyActivity extends Activity  {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.systeminfo);
        
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

        StringBuffer sb = new StringBuffer();
        sb.append("電話状況:").append(telephonyManager.getCallState());
        //TelephonyManager.CALL_STATE_IDLE;    通話可能状態
        //TelephonyManager.CALL_STATE_OFFHOOK;    通話状態
        //TelephonyManager.CALL_STATE_RINGING;    着信状態
        
        sb.append("\nデータ(トラフィック) アクティブ 状況:").append(telephonyManager.getDataActivity());
        //TelephonyManager.DATA_ACTIVITY_DORMANT    データ接続はアクティブ状況ですが、物理リンクはダウンしている
        //TelephonyManager.DATA_ACTIVITY_IN    受け取ったIP PPPトラフィック
        //TelephonyManager.DATA_ACTIVITY_INOUT    発送/受け取った IP PPPトラフィック
        //TelephonyManager.DATA_ACTIVITY_NONE    トラフィックなし
        //TelephonyManager.DATA_ACTIVITY_OUT    発送した IP PPPトラフィック
        
        sb.append("\nデータ(回線)接続状況:").append(telephonyManager.getDataState());
        //TelephonyManager.DATA_CONNECTED    接続中
        //TelephonyManager.DATA_CONNECTING    接続中
        //TelephonyManager.DATA_DISCONNECTED    未接続
        //TelephonyManager.DATA_SUSPENDED    一時中断
        
        sb.append("\nデバイスID:").append(telephonyManager.getDeviceId());
        sb.append("\nデバイス・ソフトウェア・バージョン:").append(telephonyManager.getDeviceSoftwareVersion());
        sb.append("\n電話番号:").append(telephonyManager.getLine1Number());
        sb.append("\nNetwork Country Iso:").append(telephonyManager.getNetworkCountryIso());
        sb.append("\nNetwork Operator:").append(telephonyManager.getNetworkOperator());
        sb.append("\nNetwork Operator名:").append(telephonyManager.getNetworkOperatorName());
        
        sb.append("\nネットワーク回線種類:").append(telephonyManager.getNetworkType());
        //TelephonyManager.NETWORK_TYPE_1xRTT
        //TelephonyManager.NETWORK_TYPE_CDMA
        //TelephonyManager.NETWORK_TYPE_EDGE
        //TelephonyManager.NETWORK_TYPE_xxx
        
        sb.append("\n電話種類:").append(telephonyManager.getPhoneType());
        //TelephonyManager.PHONE_TYPE_CDMA
        //TelephonyManager.PHONE_TYPE_GSM
        //TelephonyManager.PHONE_TYPE_SIP
        //TelephonyManager.PHONE_TYPE_NONE
        
        sb.append("\nSIMプロバイダーの国コード:").append(telephonyManager.getSimCountryIso());
        sb.append("\nSIM Operator:").append(telephonyManager.getSimOperator());
        sb.append("\nSIM Operator名:").append(telephonyManager.getSimOperatorName());
        sb.append("\nSIM シリア ナンバー:").append(telephonyManager.getSimSerialNumber());
        
        sb.append("\nSIM 状態:").append(telephonyManager.getSimState());
        //TelephonyManager.SIM_STATE_ABSENT    SIMカードなし
        //TelephonyManager.SIM_STATE_NETWORK_LOCKED    SIMがロックされた状態。ロック解除するには、ネットワークPINが必要
        //TelephonyManager.SIM_STATE_PIN_REQUIRED    SIMがロックされた状態。ロック解除するには、SIM ユーザPINが必要
        //TelephonyManager.SIM_STATE_PUK_REQUIRED    SIMがロックされた状態。ロック解除するには、SIM ユーザPUKが必要
        //TelephonyManager.SIM_STATE_READY    使用可能な状態
        //TelephonyManager.SIM_STATE_UNKNOWN
        
        sb.append("\nSubscriber ID:").append(telephonyManager.getSubscriberId());
        sb.append("\nVoice Mail Alpha Tag:").append(telephonyManager.getVoiceMailAlphaTag());
        sb.append("\nVoice Mail Number:").append(telephonyManager.getVoiceMailNumber());
        //sb.append("\nデバイス・ロケーション:").append(telephonyManager.getCellLocation());
                
        TextView textView1 = (TextView)findViewById(R.id.textView1);
        TextView textView2 = (TextView)findViewById(R.id.textView2);
        
        textView1.setText("端末情報取得:");
        textView2.setText(sb.toString());
    }
}


AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.syboos.android"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="12" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="GetTelephonyActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"></action>
                <category android:name="android.intent.category.LAUNCHER"></category>
            </intent-filter>
        </activity>
    </application>
    
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- TelephonyManager.getCellLocation() 時
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
-->
</manifest>


ご覧のように、電話番号などを取得するのに、AndroidManifest.xmlにREAD_PHONE_STATEパーミッションを定義する必要があります。
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

結果確認


有关作者
Syboos.jp編集長AJavaやオープンソース情報の執筆、Webサイトの開発や運営全般の業務に携わる。

Sponsored Link


Comments

用户名 (required)

Email (will not be published) (required)

URL

Evaluation