我想在拔掉USB插頭時停止正在運行的服務。USB設備拔出時停止服務
我的活動onCreate
裏面我查的意圖其action
if (getIntent().getAction().equals(UsbManager.ACTION_USB_DEVICE_DETACHED)) {
Log.d(TAG, "************** USB unplugged stopping service **********");
Toast.makeText(getBaseContext(), "usb was disconneced", Toast.LENGTH_LONG).show();
stopService(new Intent(this, myService.class));
} else {
init();
}
而且我manifest
裏面我有另一個intent filter
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
</intent-filter>
這intent filter
以及它被調用。
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
但是detach沒有被調用。
OK,但是當活動是發生了什麼在後臺,還是被摧毀? – user1940676
註冊接收器則用於服務;) – Dediqated
如果你不想從活動開始廣播接收器 - 在AndroidManifest.xml – msh