我想在 Android 中编写一个基于 USB_DEVICE_ATTACHED 意图启动的服务。所以,基本上我的服务应该在连接特定的 USB 设备(FT232C - VID:PID 0403:6010)时启动,并在断开该 USB 设备时停止。是否可以这样做,或者我是否应该始终有一个启动该服务的活动,以防它尚未启动?该服务的最终目的是根据该 USB 设备提供的位置,使用 TEST_PROVIDER 更新 LocationProvider 上的位置。
我已经尝试在 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 包含这个
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device vendor-id="0403" product-id="6010"/>
</resources>
我的 LocationService 类已覆盖处理 USB_DEVICE_ATTACHED 意图的 onStartCommand()