1

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

This is my java code

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

Saurabh
  • 386
  • 3
  • 11
diema
  • 29
  • 2
  • 5

2 Answers2

3

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

Alex Orlov
  • 17,801
  • 7
  • 53
  • 44
  • 2
    I also try : if (telephonyManager.getDeviceId() != null) imeiid = telephonyManager.getDeviceId(); //*** use for mobiles else imeiid = Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID); //*** use for tablets – diema Nov 05 '12 at 13:11
  • I have solved! I use Log and the problem wasn't telephonyManager but json to send params to index.php – diema Nov 05 '12 at 17:40
  • Nice. But you should stick to the code you've written in the comments, since deviceId can be null – Alex Orlov Nov 06 '12 at 07:48
0

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

tony gil
  • 9,063
  • 6
  • 72
  • 89