下面列出了android.hardware.usb.UsbManager#ACTION_USB_DEVICE_ATTACHED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public synchronized void onReceive(Context context, Intent intent) {
if (intent.getAction() == null)
return;
switch (intent.getAction()) {
case UsbManager.ACTION_USB_DEVICE_ATTACHED: {
showNotification(context, intent.getParcelableExtra(UsbManager.EXTRA_DEVICE));
break;
}
case UsbManager.ACTION_USB_DEVICE_DETACHED: {
hideNotification(context);
break;
}
case NsConstants.SERVICE_TRANSFER_TASK_FINISHED_INTENT:
String issues = intent.getStringExtra("ISSUES");
if (issues != null) {
Toast.makeText(context, context.getString(R.string.transfers_service_stopped) + " " + issues, Toast.LENGTH_LONG).show();
break;
}
Toast.makeText(context, context.getString(R.string.transfers_service_stopped), Toast.LENGTH_SHORT).show();
break;
}
}
/**
* Registers receiver on usb connection event
* @param usbConfiguration contains information if device manager also registers receiver on permissions grant from user
*/
public void enable(final UsbConfiguration usbConfiguration) {
disable();
configuration = usbConfiguration;
isPermissionRequired = usbConfiguration.isHandlePermissions();
receiver = new UsbBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED);
intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
context.registerReceiver(receiver, intentFilter);
List<UsbDevice> devices = findDevices();
for (UsbDevice device : devices) {
checkPermissions(device, isPermissionRequired);
}
}
@Override
public void onStart() {
super.onStart();
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
ListView listView = getRootView().findViewById(R.id.connected_usb_devices_list);
listView.setAdapter(usbArrayAdapter);
for (UsbDevice device : deviceList.values()) {
usbArrayAdapter.add(getDisplayableUsbDeviceName(device));
}
usbArrayAdapter.notifyDataSetChanged();
if (usbArrayAdapter.getCount() == 0) {
updateScreenBasedOnIfUsbDeviceIsConnected(false);
}
IntentFilter intent = new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED);
intent.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
getActivity().registerReceiver(broadcastReceiver, intent);
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == null)
return;
switch (intent.getAction()) {
case UsbManager.ACTION_USB_DEVICE_ATTACHED:
usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
if (usbManager == null)
break; // ???
if (usbManager.hasPermission(usbDevice))
isUsbDeviceAccessible = true;
else {
isUsbDeviceAccessible = false;
usbManager.requestPermission(usbDevice, PendingIntent.getBroadcast(context, 0, new Intent(NsConstants.REQUEST_NS_ACCESS_INTENT), 0));
}
break;
case NsConstants.REQUEST_NS_ACCESS_INTENT:
isUsbDeviceAccessible = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
break;
case UsbManager.ACTION_USB_DEVICE_DETACHED:
usbDevice = null;
isUsbDeviceAccessible = false;
stopService(new Intent(context, CommunicationsService.class));
break;
case NsConstants.SERVICE_TRANSFER_TASK_FINISHED_INTENT:
ArrayList<NSPElement> nspElements = intent.getParcelableArrayListExtra(NsConstants.SERVICE_CONTENT_NSP_LIST);
if (nspElements == null)
break;
for (int i=0; i < mDataset.size(); i++){
for (NSPElement receivedNSPe : nspElements)
if (receivedNSPe.getFilename().equals(mDataset.get(i).getFilename()))
mDataset.get(i).setStatus(receivedNSPe.getStatus());
}
mAdapter.notifyDataSetChanged();
blockUI(false);
break;
}
}
@Override
public void onReceive(Context context, Intent intent)
{
Logger.debug("MediaMountedReceiver");
LocationsManagerBase lm = _lm;
if(lm == null)
return;
String mountPath = intent.getDataString();
ExternalStorageLocation loc = mountPath != null ? new ExternalStorageLocation(context, "ext storage", mountPath, null) : null;
try
{
Thread.sleep(3000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
switch (intent.getAction())
{
case UsbManager.ACTION_USB_DEVICE_ATTACHED:
Logger.debug("ACTION_USB_DEVICE_ATTACHED");
lm.updateDeviceLocations();
LocationsManager.broadcastLocationAdded(context, loc);
break;
case Intent.ACTION_MEDIA_MOUNTED:
Logger.debug("ACTION_MEDIA_MOUNTED");
lm.updateDeviceLocations();
LocationsManager.broadcastLocationAdded(context, loc);
break;
case Intent.ACTION_MEDIA_UNMOUNTED:
Logger.debug("ACTION_MEDIA_UNMOUNTED");
lm.updateDeviceLocations();
LocationsManager.broadcastLocationRemoved(context, loc);
break;
case Intent.ACTION_MEDIA_REMOVED:
Logger.debug("ACTION_MEDIA_REMOVED");
lm.updateDeviceLocations();
LocationsManager.broadcastLocationRemoved(context, loc);
break;
case UsbManager.ACTION_USB_DEVICE_DETACHED:
Logger.debug("ACTION_USB_DEVICE_DETACHED");
lm.updateDeviceLocations();
LocationsManager.broadcastLocationRemoved(context, loc);
break;
}
}
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
final UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (device == null)
return;
switch (action) {
case ACTION_USB_PERMISSION:
boolean granted = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false);
if (!granted) {
Log.e(TAG, "permission denied for device: " + device);
setState(STATE_ERROR, R.string.error_no_permission);
return;
}
performUsbPermissionCallback(device);
break;
case UsbManager.ACTION_USB_DEVICE_ATTACHED:
if (!isRoyaleDevice(device)) return;
if (mConn != null) {
Log.i(TAG, "Another picoflexx attached");
return;
}
if (mUsbManager.hasPermission(device)) {
performUsbPermissionCallback(device);
return;
}
mUsbManager.requestPermission(device, mUsbPi);
break;
case UsbManager.ACTION_USB_ACCESSORY_DETACHED:
if (!isRoyaleDevice(device))
return;
Log.i(TAG, "did the device disappear beneath us?");
break;
}
}