Question

I have the below class which is set only to get the device id.

package com.example.bootstart;


import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;

public class IMEI extends Activity {
    public String get_dev_id(){

        //Getting the Object of TelephonyManager 
        TelephonyManager tManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

        //Getting IMEI Number of Devide
        String Imei=tManager.getDeviceId();

        return Imei;
    }
}

I have below permission set in my manifest.

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

I try to access the get_dev_id function from another class through

package com.example.common;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.example.bootstart.IMEI;

import android.os.AsyncTask;
import android.util.Log;

public class AsyncSendData extends AsyncTask<String, Void, Boolean> {

    private static final String LOGTAG = "SENDSERVER";

    @Override
    protected Boolean doInBackground(String... params) {
        Log.i(LOGTAG, "FUNction Called");
        String url = params[0]; // URL.
        String lati = params[1];
        String longi = params[2];
        String welawa = params[3];

        HttpClient httpClient = new DefaultHttpClient();
        // Creating HTTP Post
        HttpPost httpPost = new HttpPost(url);
//              "http://www.domain.com/nv/maps/track_post");

        // Building post parameters
        // key and value pair
        Log.i(LOGTAG, "Before Imei");
        IMEI imei = new IMEI();
        String imei_num = imei.get_dev_id(); //***************IMEI Called Here****
        Log.i(LOGTAG, "After Imei");

        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(4);
        nameValuePair.add(new BasicNameValuePair("imei", imei_num));
        nameValuePair.add(new BasicNameValuePair("welawa", welawa));
        nameValuePair.add(new BasicNameValuePair("lat", lati));
        nameValuePair.add(new BasicNameValuePair("long", longi));
        Log.i(LOGTAG, "After Name Value");
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
            Log.i(LOGTAG, "After Response");
            HttpResponse response = httpClient.execute(httpPost);
            Log.i(LOGTAG, "Response " + response);
            if (response != null) {
                return response.getStatusLine().getStatusCode() == 200;
            }
        } catch (Exception e) {
            //
            Log.e(LOGTAG, e.getMessage());
        }
        return false;
    }

}

The log doesn't put any out put after "Log.i(LOGTAG, "Before Imei");". Any idea on what could be going on?

Your help is greatly appreciated.

Was it helpful?

Solution

//you need to change the IMEI activity to class

public static class IMEI {

    public static String get_dev_id(Context ctx){

        //Getting the Object of TelephonyManager 
        TelephonyManager tManager = (TelephonyManager)ctx.getSystemService(Context.TELEPHONY_SERVICE);

        //Getting IMEI Number of Devide
        String Imei=tManager.getDeviceId();

        return Imei;
    }
}

// And you need to call it with

String imei_num = IMEI.get_dev_id(context);

EDITED:

You can use static instead..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top