获取手机相关信息的Demo

先测试之前,始终不要忘了权限问题:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />



直接给出代码:
GetPhoneNumber.java文件
package cn.com;

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

public class GetPhoneNumber extends Activity {
	TextView text;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        text = (TextView)findViewById(R.id.text);
        //getPhoneNumber();
        fetch_status();
    }
    
    public void getPhoneNumber(){
        // 获取手机号、手机串号信息
        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        String imei = tm.getDeviceId();
        String tel = tm.getLine1Number();
     
        text.setText("手机串号:" + imei + "\n"+"手机号:" + 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";    
        text.setText(str);  
    }  
}


上面是得到手机相关信息的一个方法;
截个图给看下效果:

获取手机相关信息的Demo
不过我在真机上测试的时候,得不到手机号码,不知道怎么处理,希望路过的能给点思路和指点下原因

你可能感兴趣的:(android,OS)