我想传递 android 设备 ID 或 imei 号码,但是当我从一个类传递到另一个类时,应用程序崩溃。但是当我在同一个班级打电话时它工作正常请帮助我是android的新手。
package com.example.deviceid;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class GetId extends Activity {
String deviceId;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public String AndroidId(){
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String imei=telephonyManager.getDeviceId();
return imei;
}
}
这是我称为imei号码的主要课程。
package com.example.deviceid;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class ShowDeviceInfo extends Activity {
GetId gi;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gi = new GetId();
String imei = gi.AndroidId();
Toast.makeText(this, imei, Toast.LENGTH_SHORT).show();
}
}
XML 文件
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.deviceid.ShowDeviceInfo"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.deviceid.GetId"
android:label="@string/app_name" >
</activity>
</application>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
</manifest>