简体   繁体   中英

broadcast receiver not register

In my app I'm using internet connection.I want to check internet connection through my application so I'm using broadcast receiver for that. i have register receiver in manifest but don't know why its not working.Can somebody help....is there any mistake in registering the receiver...i have put log in on receive method to check whether its getting register but that log never get print.and when i register broadcast receiver via code then its working fine...

Here is my code... `

<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>`

Go through this working code for checking networks (both Mobile data and 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}

Well According to Our Discussion ..

You need to Register your Receiver in your activity and make it global so that you can access it any where..

Since you also need to unregister the receiver when ever you are closing the application.

I recommend that you register the receiver in you main activity oncreate method and unregister it in the onKeyDown / onBackKeyPressed Method. when your application will be closed..

ook

Hope this helped

Thanks sHaH...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM