3 回答

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個(gè)贊
首先,創(chuàng)建一個(gè)名為 NetworkChangeReceiver 的類(lèi),如下所示:
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
ConnectivityManager connectivityManager
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if ( activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
Toast.makeText(context, "Network is back", Toast.LENGTH_SHORT).show();
}
}
}
每次網(wǎng)絡(luò)發(fā)生變化時(shí),此類(lèi)都會(huì)收到通知,并且函數(shù)中的代碼onReceive會(huì)檢查我們是否在線。
下一個(gè),
在您的清單中添加<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 并告訴 Android 您想要過(guò)濾此類(lèi)事件。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver android:name=".NetworkChangeReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
接下來(lái)在您的主要活動(dòng)中注冊(cè)這些事件
private BroadcastReceiver mNetworkReceiver;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNetworkReceiver = new NetworkChangeReceiver();
registerReceiver(mNetworkReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(mNetworkReceiver);
}

TA貢獻(xiàn)1752條經(jīng)驗(yàn) 獲得超4個(gè)贊
用于處理后臺(tái)的東西
您可以使用WorkManager(部分Android Jetpack
)或Android-Job在后臺(tái)檢查互聯(lián)網(wǎng)連接,這些是在后臺(tái)處理作業(yè)的最佳庫(kù)。
工作經(jīng)理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-作業(yè)?is a utility library for Android to run jobs delayed in the background.
用于處理前臺(tái)的東西

TA貢獻(xiàn)1795條經(jīng)驗(yàn) 獲得超7個(gè)贊
聆聽(tīng) 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(
new NetworkRequest.Builder().build(),
new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
super.onAvailable(network);
// ...
}
@Override
public void onLost(Network network) {
super.onLost(network);
// ...
}
});
添加回答
舉報(bào)