2

I have been searching and found this post useful in getting the telephone number of my device. In my main service file, I have a public static variable that is set to the phone number found using the method in the aforementioned post.

class MyService extends Service implements... {
    //Phone number of the device
    public static String phoneNumber;

    public void onCreate() {
        //Grab phone number of device
        TelephonyManager tMgr = (TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
        String mPhoneNumber = tMgr.getLine1Number();

        phoneNumber=mPhoneNumber;
    }
}

I am by no means an advanced software developer but I know that using a public static variable is not the best solution since it can be changed from outside the class. This public static phone number is being accessed in another class which sends it to a server. And it is being accessed like so:

String devicePhoneNumber;
devicePhoneNumber = MyService.phoneNumber;

I wouldn't have to worry about this problem if there a way to get the phone number and IMEI without using TelephonyManager, or more specifically without needing the application context. If that is not possible, perhaps there is a workaround for using a public static variable?

Thanks for all the help

Community
  • 1
  • 1
Alex
  • 3,441
  • 4
  • 18
  • 30
  • 1
    The fact that you use `MyService.phoneNumber();` implies that you have a getter method. So why not make `phoneNumber` private if it's never accessed directly outside of `MyService`? By the way, having a local variable named `mPhoneNumber` looks a bit odd. Typically the `m` prefix is used (if at all) for non-public class fields. – Michael Jun 09 '15 at 17:02
  • 2
    Short answer is no, you need a Context object. I think the question you should be asking is: "how do I get a Context object wherever I need to use IMEI/phone number?" – Sam Dozor Jun 09 '15 at 17:31
  • @Michael I meant to type MyService.phoneNumber; since it's a field. I will fix this in an edit. – Alex Jun 09 '15 at 18:42
  • @SamDozor sure, I think it would be good to have that and I have considered passing the Context object as an argument but it gets messy as the method that needs to access the IMEI/phone number is not directly called by MyService. Any suggestions around this? Thanks. – Alex Jun 09 '15 at 18:46
  • in android you pretty much always need to have a context accessible somehow. – njzk2 Jun 09 '15 at 18:51
  • @njzk2 so let's say I have a class called UploadPhoneInfo which has a method called getPhoneInfo() which in turn needs to access the IMEI/Phone number. The only way I know of doing this now is using TelephonyManager, which requires application context. How would I be able to access this context without passing it from MyService? – Alex Jun 09 '15 at 19:05
  • you wouldn't. just pass it from your service. (or inject it with dagger, or define a static accessor in a custom Application, or in even in your service.). But basically just pass the context along. – njzk2 Jun 09 '15 at 19:14
  • or find the phone number in the service, and pass that as a parameter to your other class methods. – njzk2 Jun 09 '15 at 19:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80103/discussion-between-lpbug-and-njzk2). – Alex Jun 09 '15 at 19:25
  • Okay I see that as a potential solution: but just to make sure... in android development, if there is a method that is not directly called by the main Service and needs the application context, the general solution is to keep passing that context down, from the parent method in service to the method that is being called at the end? – Alex Jun 09 '15 at 19:28

1 Answers1

2

There isn't a workaround for getting phonenumbers without the Context because the Context is your gateway into the users phone. It gives you access to storage, Location, display, almost everything.

If you use the phonenumber a lot I think you should make a getter for it in the mainActivity, that's where your getting it from anyway. In your mainActivity onCreate() method initialize

private String phoneNumber;

to be the phone number and in your mainActivity make a

public String getPhoneNumber()

To access it from your Service, and in other parts of the app you can call

((MainActivityNameHere) getApplicationContext()).getPhoneNumber()

unless your working in a static method. You'll still need access to the MainActivity though. I usually do this in the mainActivity:

private static MainActivity context;
public void onCreate() {
    context = this;
}

public static getContext() { return context; }

hopefully that syntax is right, try merging the above into your mainActivity and calling it from your Service with

MainActivity.getContext().getPhoneNumber()
firescar96
  • 419
  • 3
  • 8
  • How would you be able to access getPhoneNumber() from another class? getPhoneNumber is an instance method and I find it weird to have multiple MyService services running? – Alex Jun 09 '15 at 19:32
  • it's an instance method of your Activity, which is a child of Context. Every part of your app can get the same instace of your activity by calling getApplicationContext() – firescar96 Jun 09 '15 at 19:34