【问题标题】:How to pass an activity context in an application to a service in an other application如何将应用程序中的活动上下文传递给其他应用程序中的服务
【发布时间】:2012-09-20 21:13:44
【问题描述】:

所以我在启动服务的应用程序中有一个活动:

private void startService() {
    if (started) {
        Toast.makeText(Main.this, "Service already started",
                Toast.LENGTH_SHORT).show();
    } else {
        Intent i = new Intent();
        i.setClassName("com.enorbitas.daemon.service",
                "com.enorbitas.daemon.service.DaemonService");
        startService(i);
        started = true;
        updateServiceStatus();
        Log.d(getClass().getSimpleName(), "startService()");
    }

}

活动由以下意图启动:

<intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
        </intent-filter>

然后该服务会进行日志记录并与此自定义 USB 设备连接。为此,它需要活动上下文:

        mUsbManager = (UsbManager) parent.getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        while(deviceIterator.hasNext()){
            UsbDevice device = deviceIterator.next();
        }

        Intent intent = parent.getIntent();
        String action = intent.getAction();
        UsbDevice device = (UsbDevice) intent
                .getParcelableExtra(UsbManager.EXTRA_DEVICE);
        if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
            setDevice(device);
            Log.i(TAG, "usb conectado");
        } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
            if (mDevice != null && mDevice.equals(device)) {
                setDevice(null);
                Log.i(TAG, "usb NO conectado");
            }
        }

parent 将是启动服务的活动。这种方法曾经有效,因为该代码曾经在同一个应用程序中,但现在我希望它成为一项服务,以便其他应用程序可以连接到它。

有没有办法将活动的上下文传递给服务?我阅读了很多关于意图和捆绑、可打包和序列化的内容,但没有一个对我有用。我需要传递上下文。

有什么想法吗?

【问题讨论】:

    标签: android service android-context


    【解决方案1】:

    每个Service 都有自己的上下文,只需使用它即可。您不需要将 Service 传递给 Activity 的上下文。

    我不明白为什么您需要特定 Activity 的 Context 来调用 getSystemService() 并且服务将像任何 Activity 一样容易地从 BroadcastReceiver 接收 Intent。

    另外,如果在这个Service运行的时候原始的Activity被销毁了,那么Context就会失效或者Activity会被泄露。

    【讨论】:

    • 是的,我知道。但问题是,当我从父母那里获得意图以获取 .getParcelableExtra 并要求它返回 null 的设备时。对我来说,这是因为捕获 USB 附件的意图是来自活动的意图,而不是服务。我认为这就是正在发生的事情。你明白我的意思吗?
    • 但是你的BroadcastReceiver可以直接把这些信息传递给Service,不需要存储在Activity中。
    • 因为我尝试了服务的上下文,并且在检索 USB 设备对象时总是得到空指针。你会怎么做?如果将服务设置为捕获 USB 附件的意图,则它永远不会被调用。我不知道为什么 Android 的特定意图过滤器存在问题。
    • 但是 BroadcastReceiver 永远不会收到来自 usb attach 的意图。 Android有一个错误或类似的东西。它仅适用于活动中的意图过滤器。
    • "我尝试了服务的上下文,但在检索 USB 设备对象时总是得到空指针。"很有趣(我自己试一试。)但是你总是可以在一个新的 Intent 中传递额外的 Parcelable 数据,或者你可以使用UsbManager#getDeviceList()
    【解决方案2】:

    如何将应用程序中的活动上下文传递给其他应用程序中的服务

    这是不可能的。这些应用程序在不同应用程序的不同虚拟机中运行——您不能跨越这些边界传递对象。

    【讨论】:

      【解决方案3】:

      也许进程间通信可能会对您有所帮助。

      Some randomly googled Android IPC slides here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-12
        • 1970-01-01
        • 1970-01-01
        • 2019-01-17
        相关资源
        最近更新 更多