android 公开静态内部类BroadcastReceiver
第一次写博客,写得不好盼大家指正.
当想实现广播接收功能时,因java 是单继承,所以一般一个类都会extends Activity,所以会想到内部类来extends BroadcastReceiver,这中间可能会出现两种异常
1:java.lang.RuntimeException: Unable to instantiate
receiver com.example.progressbar.ProgressBarActivity$mBroadcastReceiver:
java.lang.InstantiationException: com.example.progressbar.ProgressBarActivity$mBroadcastReceiver
这是因为内部类需要写成静态的,即static
static class mBroadcastReceiver extends BroadcastReceiver {
//必须为静态公开的(public static)
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
}
}
2: java.lang.RuntimeException: Unable to instantiate
receiver com.example

这篇博客探讨了在Android中如何使用公开静态内部类BroadcastReceiver来实现广播接收功能。由于Java的单继承特性,作者建议通过静态内部类方式避免冲突。文章提到了两个可能出现的问题:内部类需要声明为static,以及需要将其设置为public以避免访问权限问题。正确的做法是在内部类前添加$符号。此外,还讨论了广播接收器的注册和注销操作,指出这些步骤通常在onCreate()或onResume()中进行。
最低0.47元/天 解锁文章
1800

被折叠的 条评论
为什么被折叠?



