Android USB アクセサリのドキュメントには、アクティビティがインテント フィルターによって USB_ACCESSORY_ATTACHED をキャッチする Android マニフェストの例があります。Service/IntentService のインテントフィルターで同じインテントをキャッチすることは可能ですか?
編集
私はこれを試しましたが、成功しませんでした:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.entreprise.ws.main"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
<activity
android:name="com.entreprise.ws.main.WeatherStationClientActivity"
android:exported="true"
android:screenOrientation="portrait"
>
<!-- <intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter> -->
</activity>
<activity android:name=".EntryPointActivity"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.entreprise.ws.main.WiFiListActivity"
android:label="@string/appname_wifilist" >
</activity>
<uses-library android:name="com.android.future.usb.accessory" />
<activity
android:screenOrientation="portrait"
android:name=".WSInstallatorActivity"
android:exported="true"
>
</activity>
<service class="services.MyService" android:name="services.MyService">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
<action android:name="android.hardware.usb.action.USB_ACCESSORY_DETACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
android:resource="@xml/accessory_filter" />
</service>
</application>
</manifest>
サービス:
package services;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
final static String TAG = "MyService";
@Override
public void onCreate() {
super.onCreate();
Log.i("TAG", "onCreate");
Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("TAG", "onDestroy");
Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent intent) {
Log.i("TAG", "onBind");
// TODO Auto-generated method stub
return null;
}
}
活動のためのPS、これはうまくいきます
EDIT2
インテント フィルターとメタデータをブロードキャスト レシーバーに移動して実験しています。これ以上添付イベントを受信しません。面白いことに、detach イベントを受け取り続けます。ドキュメントの「ブロードキャスト アクション」の分類にもかかわらず、USB_ACCESSORY_ATTACHED はアクティビティでのみ機能するように見えます。
EDIT3: 最終的な結論
USB_ACCESSORY_ATTACHED は、アクティビティでのみキャッチできるように見えます(ユーザーとの対話が可能であるという理由で ?! )。デタッチ イベントはレシーバーでキャッチできます。