Android: deviceId null (TelephonyManager)

i have a problem with TelephonyManager. DeviceId is null/blank. This's my code. Where I wrong?

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

}

When registerUser calls index.php, deviceId is null. Why?

My manifest file is:

<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" />

I use an emulator. If I digit *#60#, the outupt is: 00000000


Not all android devices have 3G module, so not of them have telephonyManager.getDeviceId(); You should use some other uid


This works:

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

all credits to this answer

链接地址: http://www.djcxy.com/p/26226.html

上一篇: 带菜单项的标签栏应用程序

下一篇: Android:deviceId null(TelephonyManager)