Android的USB主机模式的服务 - 启动基于USB_DEVICE_ATTACHED [英] Android USB Host mode service - Start based on USB_DEVICE_ATTACHED

查看:1148
本文介绍了Android的USB主机模式的服务 - 启动基于USB_DEVICE_ATTACHED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写在Android的服务可开始基于USB_DEVICE_ATTACHED意图。连接并且当USB设备断开停止 - 所以,基本上,当一个特定的USB设备(:PID 0403 6010 VID FT232C)我的服务应该启动。是否有可能做到这一点,或者我应该始终有这情况下启动该服务尚未启动的活动?到底该服务的目的是基于从该USB设备提供了什么位置上用TEST_PROVIDER的LocationProvider更新的位置。

I want to write a service in Android which starts based on USB_DEVICE_ATTACHED intent. So, basically my service should start when a specific USB Device(FT232C - VID:PID 0403:6010) is connected and stop when that USB device is detached. Is it possible to do that or should I always have an Activity which starts this service in case it is not already started? The intent of the service in the end is to update the location on the LocationProvider with a TEST_PROVIDER based on what location is provided from this USB device.

我已经试过在AndroidManifest.xml该配置。

I already tried creating a service with this configuration in AndroidManifest.xml

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.testlocservice"
        android:versionCode="1"
        android:versionName="1.0">

        <uses-sdk android:minSdkVersion="12" android:targetSdkVersion="15" />
        <uses-feature android:name="android.hardware.usb.host"/>
        <supports-screens android:resizeable="true" android:smallScreens="true" android:anyDensity="true" android:largeScreens="true" android:xlargeScreens="true" android:normalScreens="true"/>
        <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
        <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

        <application android:label="@string/app_name"
            android:icon="@drawable/ic_launcher"
            android:theme="@style/AppTheme">
                <service android:name="com.testlocservice.LocationService"         android:process=":LocService" android:enabled="true" android:exported="true">
                    <intent-filter>
                        <action         android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
                    </intent-filter>
                    <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
                android:resource="@xml/device_filter" />                
                </service>
        </application>
    </manifest>

在XML / device_filter.xml包含此

The xml/device_filter.xml contains this

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
            <usb-device vendor-id="0403" product-id="6010"/>
    </resources>

我LocationService类已覆盖onStartCommand(),它处理USB_DEVICE_ATTACHED意图

My LocationService class has overridden onStartCommand() which handles the USB_DEVICE_ATTACHED intent

推荐答案

从我的经验和LT;服务>无法接收USB意图。我通过创建一个隐藏的活动接收的意图和克服了这个重新播放。当然,这也是活动能够处理启动/停止服务。

From my experience a <service> cannot receive the USB intents. I overcame this by creating a hidden activity to receive the intent and re-broadcast it. Of course this activity could also handle starting/stopping your service.

我已经提出了一些工作code在这里:结果
http://stackoverflow.com/a/15151075/588476 结果
你只需要改变它,所以它启动和自动停止服务。

I have already put up some working code here:
http://stackoverflow.com/a/15151075/588476
You will just have to change it so it starts and stops your service automatically.

这篇关于Android的USB主机模式的服务 - 启动基于USB_DEVICE_ATTACHED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆