android设备imei传递到另一个类
我想传递android设备id或imei号码,但当我从一个类传递到另一个类时,应用程序崩溃。 但当我在同一类plz帮助中被呼叫时,它工作得很好,我是android新手android设备imei传递到另一个类,android,Android,我想传递android设备id或imei号码,但当我从一个类传递到另一个类时,应用程序崩溃。 但当我在同一类plz帮助中被呼叫时,它工作得很好,我是android新手 package com.example.deviceid; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.provider.Settings.Secure; i
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;
}
}
<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>
这是我被称为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();
}
}
<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>
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>
在您的第一次活动中,您尚未设置内容
<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>
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // missing this statement
}
您没有导航到第二个活动。在第一个活动中不调用Android()
<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>
可以使用意图在活动之间传递值
<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>
在你的第一次活动中
<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>
Intent i= new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("imeid",imei);// key value
startActivity(i);
第二次行动
<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>
Bundle extras= getIntent().getExtras();
if(extras!=null)
{
String IMEI= extras.getString("imeid");// retrieve using key
}
编辑:
<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>
不确定为什么要将imei传递给另一个类。您可以在mainactivity中获取imei编号并使用相同的编号
<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>
请阅读活动生命周期
<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>
<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>
activity_main.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>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_alignRight="@+id/textView1"
android:layout_marginBottom="66dp"
android:layout_marginRight="35dp"
android:text="Button" />
</RelativeLayout>
second.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>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
Manifest.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>
<activity
android:name="com.example.testintent.MainActivity"
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=".SecondActivity"
android:label="@string/app_name" >
</activity>
//我们只在getid类中实现getid mtd,所以不需要扩展活动。 //getid类
<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>
public class GetId{
String deviceId;
public String AndroidId(){
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String imei=telephonyManager.getDeviceId();
return imei;
}
}
//主要活动
<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>
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();
}
}
<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>
//manifest.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>
<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>
为什么需要进行其他活动您可以在第一个活动中直接调用它。显示您的AndroidManifest.xml
文件。。可能你还没有宣布第二项活动。。。