How to get an instance of ServiceState?

I am trying to get a ServiceState instance in my Activity . But how am I supposed to do this? There is no static method to get an instance or any method for any service that returns an instance of ServiceState .
There is a call to TelephonyManager.listen() . But I want to get an instance of ServiceState when I want, and not when Android calls my listener, because something has changed.

ServiceState documentation can be found here: http://developer.android.com/reference/android/telephony/ServiceState.html

+4
source share
6 answers

I think I really did not understand your question, because, as far as I can tell, there was an empty ServiceState constructor with the API level 1, which is publicly available. Therefore, if you need an instance, all you have to do is:

 ServiceState serviceState = new ServiceState(); 

Edit: Now that I understand your situation, the only thing I can think of is that after registering PhoneStateListener you will receive an update in PhoneState (it may not have changed, but the event is fired so that you know the current value that I assume) . Therefore, the only workaround I can think of would be to continue registering phoneStateListener when you want to know the current value and cancel it when you no longer need it.

Hope that helps

+3
source

What information do you want from ServiceState ? If you want to get only the current registered operator, you can do this by calling the getNetworkOperatorName() method in TelephonyManager . But if you want to get the phone service status, you need to implement PhoneStateListener with the overridden onServiceStateChanged method. Then do the following:

 telephonyManager.listen(yourListener, PhoneStateListener.LISTEN_SERVICE_STATE); 

When you call telephonyManager.listen , then the onServiceStateChanged method is onServiceStateChanged , and you get the current state of the phone service at that time.

+6
source

I have not tried this yet, but I will need to, since this function is the key to my application. I will let you know how this happens, but here is the idea:

From the API ( TelephonyManager instance method):

public void listen (PhoneStateListener listener, int events)

Registers a listener object to receive notifications of changes in the specified telephony states.

To register a listener, pass PhoneStateListener and specify at least one state of telephony that interests you in the event argument. When registering and when changing the specified state of telephony, the telephony manager calls the appropriate callback method in the listener object and transmits the current (deliberate) values.

You can simply register the listener and save it, and then receive a callback to save the reported status in your Activity with each change, so that your application can simply refer to the "Activity" field every time you need to check it. Alternatively, you can keep registering and throwing out listeners every time you want to measure the state, but I think it would lose more system resources than just saving it.

+3
source

Some calls are not available to the developer. Period.

If you want such information, you will have a PhoneStateListener , like this example .

+1
source

use this:

 private PhoneStateListener mPhoneListener = new PhoneStateListener() { @Override public void onServiceStateChanged(ServiceState serviceState) { Log.d(TAG, "Phone State: " + serviceState.getState()); phone_state = serviceState.getState(); super.onServiceStateChanged(serviceState); } }; 

and in OnCreate:

 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); tm.listen(mPhoneListener, PhoneStateListener.LISTEN_SERVICE_STATE); 
+1
source

In Android 8, you have the following:

 tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); tm.getServiceState(); 

don't forget to add permission:

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

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


All Articles