简体   繁体   English

广播接收器未注册

[英]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. 在我的应用程序中,我正在使用Internet连接。我想通过我的应用程序检查Internet连接,因此我正在使用广播接收器。 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... 我在清单中注册了接收方,但不知道为什么它不起作用。有人可以帮忙....在注册接收方时有任何错误...我已经登录了receive方法来检查其是否正在注册,但是日志永远不会被打印。当我通过代码注册广播接收器时,它的工作正常...

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): 通过以下工作代码检查网络(移动数据和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. 我建议您在主要活动oncreate方法中注册接收器,然后在onKeyDown / onBackKeyPressed方法中注销它。 when your application will be closed.. 什么时候关闭您的应用程序。

ook ook

Hope this helped 希望这有所帮助

Thanks sHaH... 谢谢...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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