4

I have written a inner class BroadcastReceiver in a Service. And I want to send another broadcast in onReceive() of BroadcastReceiver to an Activity.

My code is:

public static class ServiceBroadcastReceiver extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent)
    {
        // TODO Auto-generated method stub
        Intent intent1 = new Intent("com.test.test");
        sendBroadcast(intent1); 
    }
}

But Eclipse hints "Can't make a static reference to the non-static method sendBoradcast()"

The solution I thought is to dynamically register the broadcastreceiver and remove "static" before the class.

Is this the best way?

sgyaqing
  • 51
  • 1
  • 4

1 Answers1

2

You must change your code like:

public static class ServiceBroadcastReceiver extends BroadcastReceiver  {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent intent1 = new Intent("com.test.test");
        context.sendBroadcast(intent1); 
    }
}

Now, you are just calling sendBrodcast(). This way, you are using a method from your parent Class (Service).. This is wrong because your are accessing a non-static member from inside a Static Class.

So, use context.sendBroadcast(intent1); and that error would gone.

guipivoto
  • 18,327
  • 9
  • 60
  • 75