android - 自定义系统服务上下文错误

标签 android android-service android-source android-service-binding

我创建了一个与 zigbee 设备通信的系统服务。更新 API 并创建 system.img 并在设备上闪烁,并为 android 编程制作自定义 SDK。

在Mainactivity类中,

try {
        ZigbeeManager zm = (ZigbeeManager) getSystemService(Context.ZIGBEE_SERVICE);
        zm.write("Hello World!\r");
    } catch (Exception ex) {
        Log.d(TAG, "Error in Zigbee Service: " + ex.toString());
    }

我在“Context.ZIGBEE_SERVICE”这个词上遇到错误。当我将光标放在它上面时,它说“必须是:所有系统服务列表之一”。 使用其他系统服务有效。与其他系统服务一样,Zigbee 服务也可在 Context.class 文件中使用。

...
public static final String WIFI_SERVICE = "wifi";
public static final String WINDOW_SERVICE = "window";
public static final String ZIGBEE_SERVICE = "zigbee";

现在在设备上运行它。我在 logcat 上收到此错误消息。

01-01 12:09:24.126 6874-6874/com.jcxmej.uartcontrol D/UARTControl: Error in Zigbee Service: java.lang.NullPointerException: Attempt to invoke interface method 'int android.os.IZigbeeService.write(java.lang.String)' on a null object reference

对象为空。是否需要初始化,但如何初始化?但我认为它是 Null 因为上下文没有正确链接。如何摆脱上下文错误。或者在Context中适当添加Zigbee服务。

我用这个作为引用。 https://github.com/nayobix/add-new-hardware-hal-android-5-lollipop

最佳答案

当在框架内注册一个系统服务时(假设你已经构建了服务)你需要做的不仅仅是添加服务的String名称,你需要执行后续步骤:

  1. SystemServer.java 中注册您的服务(挖到代码,加入到所有服务的try/catch列表中)。
  2. 将您的 AIDL 添加到 frameworks/base/core/java/android/os/
  3. 将您的服务文件名添加到 frameworks/base/Android.mk 中的构建。

您可以在 this 中查看更多信息很棒的文章。

关于android - 自定义系统服务上下文错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39102750/

相关文章:

android - 独立的 Android HTML5 应用程序

android - Visual Studio 2015 Cordova - 未签名 APK

android - 通过 Intent 启动 Android 服务与通过实例创建

android - 如何在 Android 上启动 NotificationListenerService

android - 检测 Android L 省电模式

android - OnePlus One/Two/Three 上的漫游检测

android - 大屏幕或 hdpi 的主屏幕小部件大小?

带有两台显示器的 Android Studio

Android,CMDTool,标准输出重定向, "Fatal signal 11 (SIGSEGV)"

android - CyanogenMod 或 AOSP : Compile a single project