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);中出现此错误,如何解决呢?
解决方法:
删除行super.onReceive(context,intent).基本实现始终不执行任何操作(这是一种抽象方法).
标签:android-broadcast,android-networking,android 来源: https://codeday.me/bug/20191031/1972619.html