Android擷取手機資訊

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   os   

做手機開發,想必都希望擷取手機號碼吧,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裡面還需加上:<uses-permission android:name="android.permission.READ_PHONE_STATE" />

這樣才能有對手機資訊的存取權限。

 

不過,經過在HTC Hero真機上的測試,發現擷取不到手機號碼。

在Ophone系統的多普達A6188上測試,同樣也取不到手機號碼。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.