I'm stuck while working with BroadCastReceiver
in which it is invoking only when device is powered on or restarted. I am connecting the USB device using OTG cable. Android system showing USB inserted icon every time but my app not receiving any event.
Let me know what wrong I'm doing.
I am having an application which only has a BroadcastReceiver
as below.
public class MountReceiver extends BroadcastReceiver {
private static final String TAG = MountReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
String actionName = intent.getAction();
Toast.makeText(context.getApplicationContext()
, "on Receive", Toast.LENGTH_LONG).show();
extractAllDataFromIntent(intent, context);
boolean isMounted;
if (actionName.equals("android.intent.action.MEDIA_MOUNTED")) {
isMounted = true;
} else {
// actionName.equals("android.intent.action.MEDIA_UNMOUNTED"
isMounted = false;
}
}
private void extractAllDataFromIntent(Intent intent, Context context) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Set<String> keys = bundle.keySet();
Iterator<String> it = keys.iterator();
Log.e(TAG, "Dumping Intent start");
StringBuilder msg = new StringBuilder();
msg.append(TAG + " Seprate app");
while (it.hasNext()) {
String key = it.next();
Log.e(TAG, "[" + key + "=" + bundle.get(key) + "]");
msg.append("[" + key + "=" + bundle.get(key) + "]");
}
Log.e(TAG, "Dumping Intent end");
Toast.makeText(context.getApplicationContext()
, msg, Toast.LENGTH_LONG).show();
//Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}
}
}
I have manifest entry for this receiver as follows.
<receiver
android:name="com.example.manmohan.mountreceiverdemo.MountReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<action android:name="android.intent.action.MEDIA_UNMOUNTED" />
<action android:name="android.intent.action.MEDIA_REMOVED" />
<action android:name="android.intent.action.MEDIA_EJECT" />
<action android:name="android.intent.action.MEDIA_BAD_REMOVAL" />
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
<action android:name="android.hardware.usb.action.USB_STATE" />
<data android:scheme="file" />
</intent-filter>
</receiver>
A weird case I see is that receiver is receiving WiFi change events when added WiFi state change callbacks, but still no USB events are getting received.
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>