Android:deviceId null(TelephonyManager)

时间:2012-11-05 12:33:06

标签: android deviceid

我的TelephonyManager有问题。 DeviceId为空/空。 这是我的代码。哪里错了?

这是我的java代码

RegistryActivity.java

package android.tennis.app;

import android.tennis.library.UserFunctions;    
//import android.tennis.library.InfoPhoneAndroid;   
import android.content.Context;     
import android.telephony.TelephonyManager;  
import android.provider.Settings.Secure;    

[...]   

public class RegisterActivity extends Activity {    

private String deviceID = null, androidID = null;   

@Override   
public void onCreate(Bundle savedInstanceState) {    
    super.onCreate(savedInstanceState);     

    setContentView(R.layout.register);

    [..]    

    final TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
    deviceID = telephonyManager.getDeviceId(); 

    [..]    

    btnRegister.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {

    [..]

    UserFunctions userFunction = new UserFunctions();
    JSONObject json = userFunction.registerUser(nome, email, password, username, citta, data_di_nascita, cognome, deviceID);
}

当registerUser调用index.php时,deviceId为null。为什么呢?

我的清单文件是:

<uses-sdk android:minSdkVersion="8" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name=".DashboardActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <!--  Login Activity -->
    <activity
        android:label="Login"
        android:name=".LoginActivity">

    </activity>

    <!--  Register Activity -->
    <activity
        android:label="Register New Account"
        android:name=".RegisterActivity"></activity>
</application>

<!-- Allow to connect with internet -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

我使用模拟器。如果我数字*#60#,则输出为:00000000

2 个答案:

答案 0 :(得分:3)

并非所有的Android设备都有3G模块,所以没有它们有telephonyManager.getDeviceId(); 你应该使用其他一些uid

答案 1 :(得分:0)

这有效:

public String getUniqueID(){    
    String myAndroidDeviceId = "";
    TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    if (mTelephony.getDeviceId() != null){
    myAndroidDeviceId = mTelephony.getDeviceId(); 
    }else{
     myAndroidDeviceId = Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID); 
    }
    return myAndroidDeviceId;
}

所有信用额this answer