-2

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)
Community
  • 1
  • 1
user3588565
  • 3
  • 1
  • 6
  • which error you get? – Jordi Castilla Jun 11 '15 at 08:10
  • I cant attach screen shot...so kindly refer to this: 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"],nativeLibraryDirectories=[/data/app-lib/com.example.shilpi.androidtelephonymanager-2, /system/lib]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) – user3588565 Jun 11 '15 at 09:18
  • 1
    edit your question, don't post the stacktrace as a comment... :) – Jordi Castilla Jun 11 '15 at 09:19
  • but what I can see is your problem is with your Layout, nothing related to IMEI reading – Jordi Castilla Jun 11 '15 at 09:20
  • This is my Layout code: – user3588565 Jun 11 '15 at 09:36
  • 1
    Put all in your question and add oncreate method ;) – Jordi Castilla Jun 11 '15 at 09:39
  • protected void onCreate(Bundle savedInstanceState) { TextView textDeviceID = (TextView)findViewById(R.id.deviceid); String identifier = null; 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); } – user3588565 Jun 11 '15 at 09:52
  • please, update your question with all the code to make it readable and avoid possible information lost with `XML` code. That will improve the question and will attract more answerers – Jordi Castilla Jun 11 '15 at 10:07

1 Answers1

1

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..

sahu
  • 1,188
  • 10
  • 19