2013-04-07 51 views
2

在我的应用程序中,我不断检查互联网连接。如果在任何时间点没有互联网,用户会收到一条敬酒信息。Android:BroadcastListner获取错误

为此,我制作了一个NetworkStateReceiver类,用于扩展BroadcastReceiver类。

的Java文件:

public class NetworkStateReceiver extends BroadcastReceiver { 

    public void onReceive(Context context, Intent intent) { 
     super.onReceive(context, intent); // Here I'm getting the error mentioned below 
     Log.d("app", "Network connectivity change"); 
     if (intent.getExtras() != null) { 
      NetworkInfo ni = (NetworkInfo) intent.getExtras().get(
        ConnectivityManager.EXTRA_NETWORK_INFO); 
      if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) { 
       Log.i("app", "Network " + ni.getTypeName() + " connected"); 
      } 
     } 
     if (intent.getExtras().getBoolean(
       ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) { 
      Log.d("app", "There's no network connectivity"); 
     } 
    } 
} 

错误:

Cannot directly invoke the abstract method onReceive(Context, Intent) for the type BroadcastReceiver

这是我已经把我的Android清单文件中的应用:

<application>  
    .... 
    <receiver android:name=".NetworkStateReceiver" > 
     <intent-filter> 
      <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
     </intent-filter> 
    </receiver> 
    .... 
</application> 

这是权限我用过:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" 

为什么我在super.onReceive(context, intent);得到这个错误,我该如何摆脱它?

+0

你还没有告诉我们你得到的错误。如果程序崩溃,请从Logcat发布堆栈跟踪,或告诉我们您正在观察的行为与您期望的行为。 – Karakuri 2013-04-07 05:41:21

+0

@Karakuri它不是一个运行时错误,它是我在super.onReceive(context,intent)所在行发生的错误;是用java文件编写的。 – 2013-04-07 05:44:00

+0

@Karakuri不,它不工作。连接状态改变时,我无法看到日志文件输出。 – 2013-04-07 06:02:28

回答

3

删除行super.onReceive(context, intent)。基本实现无论如何不做任何事情(这是一种抽象方法)。