2012-11-01 43 views
4

我想爲我的互聯網連接檢查實現廣播接收器。如果連接不存在,只需完成();它。但我仍在搞亂背景。請檢查我的下面的代碼。Android使用廣播檢查互聯網連接

/** 
* This broadcast receiver is awoken after boot and registers the service that 
* checks for new photos from all the known contacts. 
*/ 


public class ConnectionDetector extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) { 



    boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,false); 

    if(noConnectivity){ 

     ((Activity)context).finish(); 
     //Show Warning Message 
     //Close Application the way i suggested 
    } 

    } 


} 

AndroidManifest

<receiver  android:name=".ConnectionDetector" 
         android:label="NetworkConnection"> 
     <intent-filter> 
      <action  android:name="android.net.conn.CONNECTIVITY_CHANGE"/> 
     </intent-filter> 
     </receiver> 

的logcat:

11-01 22:40:29.179: E/AndroidRuntime(29531): FATAL EXCEPTION: main 
11-01 22:40:29.179: E/AndroidRuntime(29531): java.lang.RuntimeException: Unable to start receiver in.wptrafficanalyzer.actionbarsherlocknavtab.ConnectionDetector: java.lang.ClassCastException: android.app.ReceiverRestrictedContext 
11-01 22:40:29.179: E/AndroidRuntime(29531): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1809) 
11-01 22:40:29.179: E/AndroidRuntime(29531): at android.app.ActivityThread.access$2400(ActivityThread.java:117) 
11-01 22:40:29.179: E/AndroidRuntime(29531): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985) 
11-01 22:40:29.179: E/AndroidRuntime(29531): at android.os.Handler.dispatchMessage(Handler.java:99) 
11-01 22:40:29.179: E/AndroidRuntime(29531): at android.os.Looper.loop(Looper.java:130) 
11-01 22:40:29.179: E/AndroidRuntime(29531): at android.app.ActivityThread.main(ActivityThread.java:3691) 
11-01 22:40:29.179: E/AndroidRuntime(29531): at java.lang.reflect.Method.invokeNative(Native Method) 
11-01 22:40:29.179: E/AndroidRuntime(29531): at java.lang.reflect.Method.invoke(Method.java:507) 
11-01 22:40:29.179: E/AndroidRuntime(29531): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907) 
11-01 22:40:29.179: E/AndroidRuntime(29531): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665) 
11-01 22:40:29.179: E/AndroidRuntime(29531): at dalvik.system.NativeStart.main(Native Method) 
11-01 22:40:29.179: E/AndroidRuntime(29531): Caused by: java.lang.ClassCastException: android.app.ReceiverRestrictedContext 
11-01 22:40:29.179: E/AndroidRuntime(29531): at in.wptrafficanalyzer.actionbarsherlocknavtab.ConnectionDetector.onReceive(ConnectionDetector.java:29) 
11-01 22:40:29.179: E/AndroidRuntime(29531): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1798) 
11-01 22:40:29.179: E/AndroidRuntime(29531): ... 10 more 

回答

7

你BroadcastReciever得到的上下文是不是一個活動。 BroadcastReciever可以在活動之外工作,並且不受限制,除非您專門製作。

在互聯網上的問題把剛剛完成的活動留出不好的做法不通知用戶就可以做到以下幾點:

public abstract class ConnectionAwareActivity extends Activity { 

protected final IntentFilter mIntentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); // A filter for a BR. We want to listen to internet changes 
protected final ConnectionDetector mConnectionDetector = new ConnectionDetector(); // Creating an instance of our BR for activity to use 

@Override 
protected void onResume() { 
    super.onResume(); 
    try { 
     registerReceiver(mConnectionDetector, mIntentFilter); // Activity gets shown, we register a BR and it starts to receive notifications about internet changes 
    } catch (Exception exc) { 
     // whoops 
    } 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    try { 
     unregisterReceiver(mConnectionDetector); // Try to unregister BR, since when activity is not visible to user, we don't want to perform any operations on internet change 
    } catch (Exception exc) { 
     // whoops 
    } 
} 

// Your BR that is encapsulated in Activity and therefore has access to it's methods, since it has access to Activity instance 
protected class ConnectionDetector extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); 
     if (noConnectivity) { 
      finish(); 
     } 

    } 
} 

}

使用它作爲一個父類的其他活動(這個從延伸活動,將其改爲任何),必須在連接錯誤時終止。

這是非常基本的變體,有點不對,因爲你更喜歡組合超過超類

+0

謝謝亞歷克斯。您提供的代碼對我來說太高級了。你有關於這個的教程嗎?或者一個例子? –

+0

我們只是在活動中封裝廣播接收器。當向用戶顯示活動時,它註冊broadcastreceiver,並在其暫停時取消註冊。在你的例子中,BR被註冊了所有的應用程序,在我的情況下,我們只在活動期間註冊它 –

+0

我會給代碼添加一些註釋 –