2012-09-20 186 views
1

我試圖讓使用下面的代碼的電話號碼,Android的 - 沒有得到電話號碼

TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
phoneImei = tm.getDeviceId(); 
phoneNo = tm.getLine1Number(); 

但是,它返回我空值。當我檢查設置時,電話號碼是未知的。我正在使用星系連結。任何想法如何我可以解決這個問題。?

回答

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; 
}  

}

您清單:

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

這會工作得很好。

+0

不能看到任何不同於我的代碼..你仍然使用getLine1Number()... –

+0

但它在我的模擬器上正常工作.. –

+0

嘗試使用手機而不是模擬器...把SIM放在手機上,並嘗試.. –

1

您需要在您的AndroidManifest.xml文件

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

是的...我已經給予許可。但它仍然相同... –

+0

你的設備中有SIM嗎? – Lucifer

+0

是的..我試了2個SIM卡..給我空值... –

3

這裏是我的代碼添加以下權限:

import java.lang.reflect.Method; 

import android.app.NotificationManager; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.database.Cursor; 
import android.media.AudioManager; 
import android.os.Bundle; 
import android.telephony.TelephonyManager; 

import com.android.internal.telephony.ITelephony; 

    public class BlockBlackListIncomingNumber extends BroadcastReceiver { 
private Context cont; 
private Transactions transactions; 
private ITelephony itelephony;; 

int i = 1; 


    /* When any incoming calls come to our cellphone number */ 
@Override 
public void onReceive(Context context, Intent intent) 
      { 
    transactions = new Transactions(context); 
       // Create an object of transaction class. 
    this.cont = context; 
       // Refer to current object of Context. 
    Bundle extras = intent.getExtras(); 
       // Getting Bundles in extras. 
    String iNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER); 
       // Getting Incoming Number by which call comes that’s why we used TelephoyManager.EXTRA_INCOMING_NUMBER. 

i編號將來電號碼。

0

嘗試使用實施AIDL

+0

這將適用於所有版本的Android?這是SDK的一部分嗎? – wojciii

+0

我想是的,我已經在Android 2.2上實現了它。請測試最新版本,並讓我們知道您的發現。 – yogeshhkumarr

+0

我不認爲你是對的。請參閱http://stackoverflow.com/a/9034931/1525300。 – wojciii

4

代替電話號碼使用的SIM卡序列號,它總是存在,始終是一個有效值ITelephony接口。

telephonyManager.getSimSerialNumber(); 

SIM序列號在操作員中至少是唯一的。因此,與

telephonyManager.getNetworkOperator(); 

結合你就會有一個country.This內的用戶的唯一標識符與

telephonyManager.getNetworkCountryIso(); 

結合你就會有一個用戶的全局唯一標識符。

希望這會有所幫助。

乾杯。

+0

添加3個不同答案的原因是什麼? – Lucifer

+0

你的回答非常有用,但不是他所問的。 ;) – wojciii

0

在一些國家使用 代碼

TelephonyManager TM =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE),你不會得到的電話號碼; phoneImei = tm.getDeviceId(); phoneNo = tm.getLine1Number();

你將不得不從用戶那裏得到它的輸入。但是,要驗證該號碼,您需要在清單文件中包含發送SMS和接收SMS權限。

使用發件人和收件人的號碼和加密的郵件(您編寫/編碼的)進行驗證,您可以驗證用戶的電話號碼。 Whatsapp這樣做。