当 Activity/fragment 位于前台时如何连续检查互联网连接并解析数据

我不知道这是否是一个基本问题。

假设有一个活动/片段位于前台。调用 onCreate() / onViewCreate() 时没有互联网连接。

那么,当Activity/fragment处于前台状态时,如何在有网络的情况下不断检查网络连接后台并解析数据呢?

如有任何不正确之处,敬请原谅。提前致谢


婷婷同学_
浏览 133回答 3
3回答

慕妹3146593

首先,创建一个名为 NetworkChangeReceiver 的类,如下所示:public class NetworkChangeReceiver extends BroadcastReceiver {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onReceive(final Context context, final Intent intent) {&nbsp; &nbsp; &nbsp; &nbsp; ConnectivityManager connectivityManager&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);&nbsp; &nbsp; &nbsp; &nbsp; NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();&nbsp; &nbsp; &nbsp; &nbsp; if ( activeNetworkInfo != null && activeNetworkInfo.isConnected()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(context, "Network is back", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}每次网络发生变化时,此类都会收到通知,并且函数中的代码onReceive会检查我们是否在线。下一个,在您的清单中添加<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 并告诉 Android 您想要过滤此类事件。<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; package="com.example.test">&nbsp; &nbsp; <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>&nbsp; &nbsp; <application&nbsp; &nbsp; &nbsp; &nbsp; android:allowBackup="true"&nbsp; &nbsp; &nbsp; &nbsp; android:icon="@mipmap/ic_launcher"&nbsp; &nbsp; &nbsp; &nbsp; android:label="@string/app_name"&nbsp; &nbsp; &nbsp; &nbsp; android:roundIcon="@mipmap/ic_launcher_round"&nbsp; &nbsp; &nbsp; &nbsp; android:supportsRtl="true"&nbsp; &nbsp; &nbsp; &nbsp; android:theme="@style/AppTheme">&nbsp; &nbsp; &nbsp; &nbsp; <receiver android:name=".NetworkChangeReceiver" >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <intent-filter>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </intent-filter>&nbsp; &nbsp; &nbsp; &nbsp; </receiver>&nbsp; &nbsp; &nbsp; &nbsp; <activity android:name=".MainActivity">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <intent-filter>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <action android:name="android.intent.action.MAIN" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <category android:name="android.intent.category.LAUNCHER" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </intent-filter>&nbsp; &nbsp; &nbsp; &nbsp; </activity>&nbsp; &nbsp; </application></manifest>接下来在您的主要活动中注册这些事件&nbsp;private BroadcastReceiver mNetworkReceiver;&nbsp;protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp; &nbsp; &nbsp; mNetworkReceiver = new NetworkChangeReceiver();&nbsp; &nbsp; &nbsp; &nbsp; registerReceiver(mNetworkReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onDestroy() {&nbsp; &nbsp; &nbsp; &nbsp; super.onDestroy();&nbsp; &nbsp; &nbsp; &nbsp; unregisterReceiver(mNetworkReceiver);&nbsp; &nbsp; }

温温酱

用于处理后台的东西您可以使用WorkManager(部分Android Jetpack)或Android-Job在后台检查互联网连接,这些是在后台处理作业的最佳库。工作经理is a new architecture component from Google and tries to solve a very similar problem this library tries to solve: implementing background jobs only once for all Android versions.Android-作业 is a utility library for Android to run jobs delayed in the background.用于处理前台的东西

一只萌萌小番薯

聆听 Android 的连接变化networkCallBack:https://developer.android.com/reference/android/net/ConnectivityManager#registerNetworkCallback(android.net.NetworkRequest,%20android.net.ConnectivityManager.NetworkCallback)ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);connectivityManager.registerNetworkCallback(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new NetworkRequest.Builder().build(),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ConnectivityManager.NetworkCallback() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onAvailable(Network network) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.onAvailable(network);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onLost(Network network) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.onLost(network);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; &nbsp; &nbsp; }});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java