[英]Android: Internet BroadcastReceiver for the application
In my application I need not to allow the user to use the app if there is no internet connection. 在我的应用程序中,如果没有互联网连接,则无需允许用户使用该应用程序。 I know there are a bunch of good tutorials out there to do this, but non has actually fulfilled the requirements I need.
我知道有很多很好的教程可以做到这一点,但实际上并没有满足我的要求。
The internet connection should be checked all time in the app. 应始终在应用程序中检查互联网连接。 That's why, and based on this part of the documentation, I registered my receiver in the
Application
class. 因此,基于文档的这一部分,我在
Application
类中注册了接收器。 However, the problem was where to unregister the receiver , since there are no onDestort(), onStop()
methods in the class. 但是,问题在于在哪里注销接收者 ,因为该类中没有
onDestort(), onStop()
方法。
The other option I tried is to (un)register the receiver in my BaseActivity
class which extends AppCompatActivity
and all other activities in the app extends the BaseActivity
, but I keep getting java.lang.IllegalArgumentException: Receiver not registered
我尝试的另一个选项是在扩展了
AppCompatActivity
BaseActivity
类中(取消注册)接收器,而应用程序中的所有其他活动都扩展了BaseActivity
,但是我一直在获取java.lang.IllegalArgumentException: Receiver not registered
Lots of questions were solved by unregistering the receiver in onStop()
if it was registered in onStart()
, or to unregister it in onDestroy()
if it was registered in onCreate()
, but that didn't work for me as well. 通过在
onStop()
注销接收器onStop()
如果已在onStart()
中注册onStart()
或在onDestroy()
注销onDestroy()
如果已在onCreate()
注册onDestroy()
来解决很多问题,但这对我也不起作用。
public class ConnectivityReceiver extends BroadcastReceiver {
public interface ConnectivityChangedListener {
void onConnectivityChanged(boolean isConnected);
}
public static ConnectivityChangedListener listener;
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null
&& activeNetwork.isConnectedOrConnecting();
if (listener != null) {
Log.i("Connectivity", "" + isConnected);
listener.onConnectivityChanged(isConnected);
}
}
}
My BaseActivity.java (Other activities extend this one) 我的BaseActivity.java(其他活动对此进行了扩展)
public class BaseActivity extends AppCompatActivity implements ConnectivityReceiver.ConnectivityChangedListener {
private static BroadcastReceiver br;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
br = new ConnectivityReceiver();
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
ConnectivityReceiver.listener = this;
registerReceiver(br, filter);
}
@Override
public void onConnectivityChanged(boolean isConnected) {
Log.i("Connectivity", "Activity " + isConnected);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(br);
}
}
However, the problem was where to unregister the receiver, since there are no onDestort(), onStop() methods in the class.
但是,问题在于在哪里注销接收者,因为该类中没有onDestort()和onStop()方法。
onDestroy()
method of the service you can unregister the receiver. onDestroy()
方法中,您可以注销接收者。 Register in the onCreate()
method of the service. onCreate()
方法中注册。 The advantage of this is that you can perform some long operation because a broadcast receiver will just execute the code inside onReceive()
and exit away, say if you want to start any service or do other work that takes time then putting inside service is the best. 这样做的好处是您可以执行一些长时间的操作,因为广播接收器将只执行
onReceive()
的代码并退出,例如,如果您要启动任何服务或执行其他需要花费时间的工作,则将服务放入内部最好。
I guess other than that there is no place you could put it elsewhere. 我想除了那儿没有其他地方可以放它了。
Create a static method in broadcast receiver in a Application class like this, 在Application类的广播接收器中创建一个静态方法,如下所示:
static ConnectionReceiver mConnectionReceiver; 静态ConnectionReceiver mConnectionReceiver; static Context appContext;
静态上下文appContext; static void registerConReceiver() { mConnectionReceiver = new ConnectionReceiver();
静态无效registerConReceiver(){mConnectionReceiver = new ConnectionReceiver(); IntentFilter filter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
IntentFilter filter = new IntentFilter(“ android.net.conn.CONNECTIVITY_CHANGE”); appContext.registerReceiver(mConnectionReceiver,filter);
appContext.registerReceiver(mConnectionReceiver,过滤器); }
}
Then unregister whenever you want 然后随时注销
static void unregisterConReceiver()
{
try{
appContext.unregisterReceiver(mConnectionReceiver);
mConnectionReceiver=null;
}catch (IllegalArgumentException e) {
mConnectionReceiver = null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.