3

My code misses something, need your eyes to locate.

I created a USBOnReciever broadcastreceiver.

public class USBOnReceiver extends BroadcastReceiver
{ 
@Override
public void onReceive(Context context, Intent intent) 
{
    Toast.makeText(context,  "Phone was connected to power" ,Toast.LENGTH_LONG).show();
    Log.d("tag", "Phone was connected to power");
} 
} 

My manifest is:

<application>
    <receiver android:name=".USBOnReceiver" android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.UMS_CONNECTED" />
        </intent-filter>
    </receiver>

</application>

and here is when I stuck. nothing appears to happen when connecting the USB ( I also tried with power_connected).

I understand I can register the BR either through the manifest or programmaticaly. But not sure how to implement.

In my activity I added

        USBOnReceiver myReceiver = new USBOnReceiver();

But it looks so unconnected and useless :-/

Will appreciate your eyes here.

0

1 Answer 1

2

Your code is seems complete. If im not mistaken, the only issue you have is: You wrote reciever inside your manifest instead of receiver. So the BroadcastReceiver never got registered correctly.

To the topic of registering in code:

Yeah thats possible. Just create an instance of your receiver and use Context.registerReceiver(mReceiver, new IntentFilter(...)).

If you have registered in your manifest, there is no need to do that. BroadcastReceivers only live for the execution of onReceive(). Therefore the system creates the instances and kills them afterwards.

3
  • Thanks, the typo was fixed. However, still nothing happens. When I connect the USB, I get the regular Notification: USB Connected, but I don't get my toast, or log. What's the better way to test? Am I missing something in my activity? I have a feeling I do. Shouldn't I add something in my Activity? I mean, just the manifest registration, it seems so fragile :-/ Reading your comment again, I don't even need to instantiate it in my activity. ok, will keep trying
    – Saariko
    Commented Jun 26, 2011 at 20:18
  • Are you using the USB mass storage function ("USB-Stick mode", if you will, where you access your sdcard from a computer)? According to the doc this only gets fired when you use that, not when you connect to usb just to charge.
    – user658042
    Commented Jun 26, 2011 at 20:24
  • Thanks alex. I am not sure what I did wrong, but first thing, due to my apps needs, I removed the registration from the manifest, and put it in runtime. I will need to update my service when the BR is getting executed, but I will tacklel that later. As for this issue, I want to thank you.
    – Saariko
    Commented Jun 26, 2011 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.