我在服务中编写了一个内部类 BroadcastReceiver。我想在 BroadcastReceiver 的 onReceive() 中向 Activity 发送另一个广播。
我的代码是:
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);
}
}
但 Eclipse 提示“无法对非静态方法 sendBoradcast() 进行静态引用”
我认为的解决方案是动态注册广播接收器并在类之前删除“静态”。
这是最好的方法吗?
最佳答案
您必须更改您的代码,如:
public static class ServiceBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent intent1 = new Intent("com.test.test");
context.sendBroadcast(intent1);
}
}
现在,您只需调用
sendBrodcast()
.这样,您正在使用父类(Service
)中的方法。这是错误的,因为您正在从静态类内部访问非静态成员。所以,使用
context.sendBroadcast(intent1);
那个错误就会消失。
关于static - 如何在广播接收器 Android 中发送广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18840339/