简体   繁体   中英

Capture IMEI number for tablet using android code

I have a requirement where I have to capture IMEI number of the tablet. I went through certain links " How to get the device's IMEI/ESN programmatically in android? " but, the app got crashed with following error. Please suggest me the best possible way to get IMEI no.

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>

Error:

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)

This is Full Code for getting Imei Number in Tablet and also in Mobile.

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);
            }
        }
    }   
}

Hope this will helpful..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM