English 中文(简体)
The import android.os. 服务经理不能解决
原标题:The import android.os.ServiceManager cannot be resolved
  • 时间:2010-12-15 03:38:05
  •  标签:
  • android
  • aidl

Im利用辅助器自动解救,其代码如下:

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

I import ServiceManager.

import android.os.ServiceManager;

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

我怎么能做到这一点? 增 编

问题回答

android.os. ServiceManager 是一个隐蔽的类别(即@hide)和隐蔽的班级(即使这些班级在 Java意义上是公开的)被从roid中删除。 因此,在你试图进口<条码>时,你就错了。 谷歌不希望成为有文件记载的公众人物。

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

虽然这是老的,但没有人回答。 任何隐蔽的班级都可以使用反射镜。 这里的一个例子是,通过反转录计划,利用服务经理获得服务:

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

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;
    }




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签