Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The import android.os.ServiceManager cannot be resolved

Tags:

android

aidl

I'm using aidl to answer call automagically, code as following:

ITelephony.Stub.asInterface(ServiceManager.getService("phone"))
    .answerRingingCall();

I import ServiceManager.class

import android.os.ServiceManager;

but there's a problem:The import android.os.ServiceManager cannot be resolved

How can I make it work? Thanks

like image 960
Sean Avatar asked Dec 15 '10 03:12

Sean


3 Answers

android.os.ServiceManager is a hidden class (i.e., @hide) and hidden classes (even if they are public in the Java sense) are removed from android.jar, hence you get the error when you try to import ServiceManager. Hidden classes are those that Google does not want to be part of the documented public API.

Applications using non-public API cannot be compiled easily, there will be different platform versions of this class.

like image 196
Chaitali Avatar answered Oct 01 '22 15:10

Chaitali


Though it is old one, but no one has answered it yet. Any hidden classes can be used using reflection APIs. Here is an example to acquire a service using Service Manager via reflection APIs:

if(mService == null) {
            Method method = null;
            try {
                method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
                IBinder binder = (IBinder) method.invoke(null, "My_SERVICE_NAME");
                if(binder != null) {
                    mService = IMyService.Stub.asInterface(binder);
                }

                if(mService != null)
                    mIsAcquired = true;

            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }

        } else {
            Log.i(TAG, "Service is already acquired");
        }
like image 28
sgupta Avatar answered Oct 01 '22 16:10

sgupta


As said above these methods work only on System apps or framework apps from Android N on words. Still we can code for System app for ServiceManager usage as below using reflection of Android Code

  @SuppressLint("PrivateApi")
    public IMyAudioService getService(Context mContext) {
        IMyAudioService mService = null;
        Method method = null;
        try {
            method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
            IBinder binder = (IBinder) method.invoke(null, "YOUR_METHOD_NAME");
            if (binder != null) {
                mService = IMyAudioService .Stub.asInterface(binder);
            }
        } catch (NoSuchMethodException | ClassNotFoundException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return mService;
    }
like image 34
Vinayak Avatar answered Oct 01 '22 16:10

Vinayak