-1

I am developing an app which sends some params to server like key, request type etc,and I've set the common params in the NetworkManager where I have all the connectivity code.There are some common params like generating random trid,getMac,getImei. The problem is since my NetworkManager is a class I am getting an error here:

I have the following code in my NetworkManger's AsyncTask:

inputParams.put("imei", getIMEINumber());//for adding the param 



 private String getIMEINumber() {
            // TODO Auto-generated method stub
            TelephonyManager telephonymanager = (TelephonyManager)main
                    .getSystemService(Context.TELEPHONY_SERVICE);
            String uid = telephonymanager.getDeviceId();
            // telephonymanager.getSimSerialNumber()
            Log.v("vas", "DeviceId: " + uid);
            return uid;
        }

The method getActivity() is undefined for the type.

And when I convet NetworkManager into a Fragment,I am getting a runtime exception.

E/AndroidRuntime(624): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()

Any help would be much appreciated.

  • where did you put this `TelephonyManager telephonymanager = (TelephonyManager)getActivity() .getSystemService(Context.TELEPHONY_SERVICE);` in activity or fragment? – Rajesh Nov 02 '15 at 05:56
  • No in my NetworkManager.class where I have put all the common params – Vinay Pandravada Nov 02 '15 at 05:57
  • man i said in which method? could you please put your activity or fragment? – Rajesh Nov 02 '15 at 05:58
  • i'm not asking for method i mean where did you call from this method? `getIMEINumber()` for i.e onCreate, onResume where? – Rajesh Nov 02 '15 at 06:03
  • bro just call that method in onCreate(); solve! – Rajesh Nov 02 '15 at 06:31
  • hey bro i did used to call it in the oncreate() in my fragment code,but since I have common params in multiple fragments, i thought of calling them in my networkmanager itself before sending them to the server...thanks anyway – Vinay Pandravada Nov 02 '15 at 06:36

1 Answers1

0

Inside you NetworkManager class write this method for imei number

public static String getIMEInumber(Context context) {
    String identifier = null;
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    if (tm != null)
        identifier = tm.getDeviceId();
    if (identifier == null || identifier.length() == 0)
        identifier = Secure.getString(context.getContentResolver(),
                Secure.ANDROID_ID);

    return identifier;
}

and call this method like this where you need. here context is very important.. make sure you have a context that is not null..

String imei=NetworkManager.getIMEInumber(context); 

hope it works..

Tanim reja
  • 2,120
  • 1
  • 16
  • 23