The TELEPHONY_SERVICE constant is not recognized in one class, but is in another

This is very similar to another case of lack of forest for trees.

I wrote a small (non-expandable) class for my application, which at the moment contains only one method to clear the provided URL and return the string. I was hoping to include the device identifier at the end of the query string, so I added the following lines:

import android.telephony.TelephonyManager;
import android.content.Context;
...
TelephonyManager m_tmgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

However, the constant TELEPHONY_SERVICE "cannot be resolved by a variable", according to Eclipse. If I copy and paste these lines into another class in the package (all the other classes are actions at the moment that may have something to do with this?), Everything works fine.

What is the obvious thing I'm missing?

+3
source share
5

TELEPHONY_SERVICE Context, Activity. , Activity Context.

http://developer.android.com/reference/android/content/Context.html#TELEPHONY_SERVICE

, Context.TELEPHONY_SERVICE

+6

:


jsut change


TelephonyManager m_tmgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);


 TelephonyManager m_tmgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
+1

It seems you have a static import constant. Find the line starting with import staticand copy it to another class. More details here .

0
source

replace

import android.content.Context;

import static android.content.Context.*;
0
source

Use this:

TelephonyManager tManager = (TelephonyManager) 
    getActivity().getSystemService(Context.TELEPHONY_SERVICE);
0
source

Source: https://habr.com/ru/post/1787413/


All Articles