Register to BroadcastReceiver

231 views
Skip to first unread message

Gorka

unread,
May 27, 2010, 7:06:38 AM5/27/10
to android-platform
Hi,

I am trying to register to a BroadcastReceiver in order to obtain the
results of a Bluetooth Inquiry. The problem I have is, at least as I
know, is not possible to use the registerService function if my class
does not extend of Activity or Context.

The definition of my class is like this:

public class BluetoothDiscovery extends Discovery implements Runnable

And I create my broadcast receiver like this:

mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {

}
}
};

So, when I try to register the service I do:

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, new
IntentFilter(BluetoothDevice.ACTION_FOUND));

I have an error in this last sentence because the class does not know
what registerReceiver means.

What can I do¿¿¿

rk

unread,
May 27, 2010, 8:21:04 AM5/27/10
to android-platform
have your tried the other way of registering through XML?

Thanks
rk

Gorka

unread,
May 27, 2010, 8:22:25 AM5/27/10
to android-platform
No, I don't. I didn't even know that was possible, I am new to Android
development

Rahony Goulart Ricardo

unread,
May 27, 2010, 11:05:18 AM5/27/10
to android-platform
Try to keep the instance of his activity (at boot) into a static
variable.
And use this reference to call the method registerReceiver

Dianne Hackborn

unread,
May 31, 2010, 2:54:25 PM5/31/10
to android-...@googlegroups.com
On Thu, May 27, 2010 at 8:05 AM, Rahony Goulart Ricardo <ron...@gmail.com> wrote:
Try to keep the instance of his activity (at boot) into a static
variable.

Don't do this, you will leak the activity.  If you need a context not associated with a particular activity or service (that is global to the process), use Context.getApplicationContext().

--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Gorka

unread,
Jun 2, 2010, 12:03:42 PM6/2/10
to android-platform
Hi,

Sorry, I have not been able to connect for some days. I tried to add
your suggestion but an error appears:
"cannot make a static reference to the non-static method
getApplicationContext() from the type Context"

This is the code I am using:

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
Context.getApplicationContext().registerReceiver(mReceiver, new
IntentFilter(BluetoothDevice.ACTION_FOUND));

Bye, and thank you

On 31 mayo, 20:54, Dianne Hackborn <hack...@android.com> wrote:
> On Thu, May 27, 2010 at 8:05 AM, Rahony Goulart Ricardo
> <rony...@gmail.com>wrote:
>
> > Try to keep the instance of his activity (at boot) into a static
> > variable.
>
> Don't do this, you will leak the activity.  If you need a context not
> associated with a particular activity or service (that is global to the
> process), use Context.getApplicationContext().
>
> --
> Dianne Hackborn
> Android framework engineer
> hack...@android.com

Dianne Hackborn

unread,
Jun 2, 2010, 12:41:39 PM6/2/10
to android-...@googlegroups.com
You need to call this on an existing Context object.

--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To post to this group, send email to android-...@googlegroups.com.
To unsubscribe from this group, send email to android-platfo...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-platform?hl=en.




--
Dianne Hackborn
Android framework engineer
hac...@android.com

Gorka

unread,
Jun 3, 2010, 4:14:18 AM6/3/10
to android-platform
Hi,

Thank you all for your help. My code works now.

Bye.
> > android-platfo...@googlegroups.com<android-platform%2Bunsu...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/android-platform?hl=en.
>
> --
> Dianne Hackborn
> Android framework engineer
Reply all
Reply to author
Forward
0 new messages