android - 没有获取电话号码

chi*_*_82 1 android telephony

我正在尝试使用以下代码获取电话号码,

TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
phoneImei = tm.getDeviceId();
phoneNo = tm.getLine1Number();
Run Code Online (Sandbox Code Playgroud)

但是,它返回null值.当我检查设置时,电话号码未知.我正在使用galaxy nexus.知道如何解决这个问题.

The*_*der 10

试试这个 :

  import android.app.Activity;  
  import android.content.Context;  
  import android.os.Bundle;  
  import android.telephony.TelephonyManager;  
  import android.util.Log;  
  import android.widget.Toast;  

  public class GetMyPhoneNoActivity extends Activity {  
   /** Called when the activity is first created. */  
   @Override  
   public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main);          

    String number =getMyPhoneNO();  
    Toast.makeText(getApplicationContext(), "My Phone No is: "  
    +number, Toast.LENGTH_SHORT).show();  
    Log.v("Debug", number);          
   }  

private String getMyPhoneNO(){  
    TelephonyManager mTelephonyMgr;    
   mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);     

    String yourNumber = mTelephonyMgr.getLine1Number();  
 //return yourNumber;   
}       
Run Code Online (Sandbox Code Playgroud)

}

在你的清单中:

   <uses-permission android:name="android.permission.READ_PHONE_STATE">  
    </uses-permission>
Run Code Online (Sandbox Code Playgroud)

这将很好地工作.