1

我对 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 为空。为什么?

我的清单文件是:

<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

4

2 回答 2

3

并不是所有的安卓设备都有 3G 模块,所以没有一个有 telephonyManager.getDeviceId(); 您应该使用其他一些 uid

于 2012-11-05T12:53:51.247 回答
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;
}

此答案的所有学分

于 2017-09-29T10:17:46.007 回答