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);
得到這個錯誤,我該如何擺脫它?
你還沒有告訴我們你得到的錯誤。如果程序崩潰,請從Logcat發佈堆棧跟蹤,或告訴我們您正在觀察的行爲與您期望的行爲。 – Karakuri 2013-04-07 05:41:21
@Karakuri它不是一個運行時錯誤,它是我在super.onReceive(context,intent)所在行發生的錯誤;是用java文件編寫的。 – 2013-04-07 05:44:00
@Karakuri不,它不工作。連接狀態改變時,我無法看到日誌文件輸出。 – 2013-04-07 06:02:28