使用Android代码捕获平板电脑的IMEI号码

时间:2015-06-11 08:06:10

标签: java android

我有一个要求,我必须捕获平板电脑的IMEI号码。 我浏览了某些链接“How to get the device's IMEI/ESN programmatically in android?” 但是,应用程序崩溃了以下错误。 请建议我获得IMEI号的最佳方式。

MainActivity.Java

public class MainActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            TextView textDeviceID = (TextView)findViewById(R.id.deviceid);
            String identifier = null;
            //retrieve a reference to an instance of TelephonyManager
            TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
            if (telephonyManager != null)
                identifier = telephonyManager.getDeviceId();
            if (identifier == null || identifier .length() == 0)
                identifier = Settings.Secure.getString(activity.getContentResolver(), Settings.Secure.ANDROID_ID);
        }
}}

Activity_Main.XML

<Linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
    <textview android:id="@+id/deviceid" android:layout_height="wrap_content" android:layout_width="fill_parent">
    </textview>
</Linearlayout>

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.shilpi.androidtelephonymanager" >

    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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>
    </application>
</manifest>

错误:

Caused by: java.lang.ClassNotFoundException: 
        Didn't find class "android.view.Linearlayout" on path:
        DexPathList[[zip file "/data/app/com.example.shilpi.androidtelephonymanager-2.apk"],
        nativeLibraryDirec‌​tories=[/data/app-lib/com.example.shilpi.androidtelephonymanager-2, /system/lib]] 
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)

1 个答案:

答案 0 :(得分:1)

这是在平板电脑和手机中获取Imei号码的完整代码。

import android.content.Context;
import android.os.Bundle;
import android.app.Activity;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView tv_tm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_tm = (TextView) findViewById(R.id.tv_tm);
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        if (telephonyManager != null) {
            String imei =telephonyManager.getDeviceId();

            if(imei==null){
                tv_tm.setText("no imei available");
            }else{
                tv_tm.setText(imei);
            }
        }
    }   
}

希望这会有所帮助..