Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain an instance of ServiceState?

Tags:

android

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

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

like image 288
Goddchen Avatar asked Jun 26 '10 10:06

Goddchen


5 Answers

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

telephonyManager.listen(yourListener, PhoneStateListener.LISTEN_SERVICE_STATE);

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

like image 147
UranWare Avatar answered Nov 14 '22 08:11

UranWare


I think I did't really understand your question since as far as I can tell, ever since API level 1, there's been an empty constructor on ServiceState which is public. So if you want an instance of it, 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 you register a PhoneStateListener you will get an update on the PhoneState (it may not have actually changed but an event is fired to let you know the current value I guess). So the only workaround I can think of would be to keep registering a phoneStateListener when you want to know the current value and deregistering it when you don't need it anymore.

Hope that helps

like image 38
rjam Avatar answered Nov 14 '22 09:11

rjam


I haven't tried this yet but i'm going to have to since this sort of funcitonality is key to my app. I'll let you know how it goes, but here's the idea:

From the API (instance method of TelephonyManager):

public void listen (PhoneStateListener listener, int events)

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

To register a listener, pass a PhoneStateListener and specify at least one telephony state of interest in the events argument. At registration, and when a specified telephony state changes, the telephony manager invokes the appropriate callback method on the listener object and passes the current (udpated) values.

You could just register a listener and keep it, and then get the callback to save the reported state in your Activity upon each change so that your app could just refer to the Activity field every time it needs to check. Alternatively you could keep registering and throwing out listeners every time you want a state measurement, but I think that would waste more system resources than just keeping one on.

like image 33
Sam Svenbjorgchristiensensen Avatar answered Nov 14 '22 07:11

Sam Svenbjorgchristiensensen


Some calls are not available for the developer. Period.

If you want that kind of info, you will have a PhoneStateListener like this example.

like image 2
Macarse Avatar answered Nov 14 '22 07:11

Macarse


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);
like image 1
Omid Omidi Avatar answered Nov 14 '22 07:11

Omid Omidi