Import android.os.ServiceManager could not be allowed

I use helpl to automatically answer a call, the code as follows:

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

I import ServiceManager.class

import android.os.ServiceManager;

but there is a problem: import android.os.ServiceManager could not be resolved.

How can I make it work? Thanks

+3
source share
3 answers

android.os.ServiceManageris a hidden class (i.e. @hide), and hidden classes (even if they are publicly available in the sense of Java) are deleted from android.jar, so you get an error when trying to import ServiceManager. Hidden classes are those that Google does not want to be part of the documented API.

, API, , .

+6

, . API- . Service Manager API- :

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");
        }
+2

, - Android N . , ServiceManager, , Android

  @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;
    }
+1

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


All Articles