2

This question may sprout from the fact that I'm still confused about "context" and it's usage in Android and because I'm still pretty new to Android. Thanks in advance.

I'm just trying to make a class with a function to get the MDN or MEID of a phone (I know this won't work on tablets). I'm only getting an error on this line:

TelephonyManager tManager = (TelephonyManager) Test.getSystemService(Context.TELEPHONY_SERVICE);

Eclipse is giving me an error on "Context". I've looked up on what Context is here: What is 'Context' on Android? and found the first comment extremely helpful, but it doesn't tell you how to get the context when not in an activity class.

Any suggestions?

Full code below:

package com.test.app;

import java.util.UUID;

import android.content.Context;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;

public class Test {

    public String getMDN_or_MEID() {

        // getSystemService is a method from the Activity class. getDeviceID()
        // will return the MDN or MEID of the device depending on which radio
        // the phone uses (GSM or CDMA).
        TelephonyManager tManager = (TelephonyManager) Test
                .getSystemService(Context.TELEPHONY_SERVICE);
        String uid = tManager.getDeviceId();
        return uid;

    }
}
Community
  • 1
  • 1
EGHDK
  • 17,818
  • 45
  • 129
  • 204

1 Answers1

1

Change your getMDN_or_MEID() method to getMDN_or_MEID(Context context) and pass a Context into it.

Then all you need to do is use context.getSystemService(Context.TELEPHONY_MANAGER) to get the TelephonyManager...

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
Squonk
  • 48,735
  • 19
  • 103
  • 135