2011-08-02 154 views
1

在我的應用程序中,我正在使用互聯網連接。我想通過我的應用程序檢查互聯網連接,所以我正在使用廣播接收器。 我已經註冊接收機的清單,但不知道爲什麼它不工作,可以幫助....是否有任何錯誤註冊接收器...我已經把接收方法登錄,以檢查它是否獲得註冊,但該日誌永遠不會print.and當我通過代碼註冊廣播接收機那麼它的做工精細...廣播接收器未註冊

這裏是我的代碼... `

<receiver android:name="com.android.fishdemo.CheckInternetConnectionChangeReceiver"> 
<intent-filter> 
    <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
    <action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION" /> 
    </intent-filter> 
</receiver>` 
+0

你在哪裏註冊?我的意思是在哪個元素之間?應用程序元素內部還是外部? – Shah

+0

CheckInternetConnectionChangeReceiver是BroadcastReceiver的子類,你有onReceive覆蓋正確嗎? – huntsfromshadow

+0

@sHaH ...在應用程序元素下....我必須寫入activity元素嗎? – android

回答

0

那麼根據我們的討論..

您需要在您的活動中註冊Receiver並將其設置爲全球,以便您可以訪問它any where ..

由於您在關閉應用程序時還需要取消註冊接收器。

我建議您在主要活動oncreate方法中註冊接收器,並在onKeyDown/onBackKeyPressed方法中取消註冊。當你的應用程序將被關閉..

OOK

希望這有助於

感謝 沙阿...

0

經過檢查網絡(包括移動數據和WIFI)這個工作代碼:

@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 
    this.context = context; 
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 
    NetworkInfo WIFINetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 

    settings = context.getApplicationContext().getSharedPreferences(PREFS_NAME, 0); 
    email = settings.getString("email", null); 

    if(mobNetInfo.isConnected() || WIFINetInfo.isConnected()){ // write your code here to handle something} 
相關問題