您好,登錄后才能下訂單哦!
如何在Android 7.0中監聽網絡的變化?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
public class NetChangeReceiver extends BroadcastReceiver { private static final String ANDROID_NET_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equalsIgnoreCase(ANDROID_NET_CHANGE_ACTION)){ Toast.makeText(context, "Net Changed", Toast.LENGTH_SHORT).show(); } } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.outman.example.androidtest"> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".NetChangeReceiver"> <intent-filter > <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/> </intent-filter> </receiver> </application> </manifest>
上面代碼,在Android7.0設備前,當網絡切換時,能收到消息。而在Android7.0及以上設備,則無法收到。
Android7.0 行為變更上明確說明
Android 7.0 移除了三項隱式廣播,因為隱式廣播會在后臺頻繁啟動已注冊偵聽這些廣播的應用。刪除這些廣播可以顯著提升設備性能和用戶體驗。
為緩解這些問題,Android 7.0 應用了以下優化措施:
面向 Android 7.0 開發的應用不會收到 CONNECTIVITY_ACTION 廣播,即使它們已有清單條目來請求接受這些事件的通知。在前臺運行的應用如果使用 BroadcastReceiver 請求接收通知,則仍可以在主線程中偵聽 CONNECTIVITY_CHANGE。
應用無法發送或接收 ACTION_NEW_PICTURE 或 ACTION_NEW_VIDEO 廣播。此項優化會影響所有應用,而不僅僅是面向 Android 7.0 的應用。
解決辦法
1. 大家都知道,注冊廣播有兩種方式,一種是在AndroidManifest.xml中,另一種通過register方法。
Android文檔中描述,通過在AndroidManifest.xml中注冊方式,App在前后臺都無法接收到廣播。而通過register的方式,當App在運行時,是可以接收到廣播的。
Note: A BroadcastReceiver registered with Context.registerReceiver() continues to receive these broadcasts while the app is running.
public class MainActivity extends Activity { private NetChangeReceiver netChangeReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); netChangeReceiver = new NetChangeReceiver(); registerReceiver(netChangeReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(netChangeReceiver); } }
2. 提供了一種更穩定的方式
Monitoring Network Connectivity While the App is Running
Apps that are running can still listen for CONNECTIVITY_CHANGE with a registered BroadcastReceiver. However, the ConnectivityManager API provides a more robust method to request a callback only when specified network conditions are met.NetworkRequest objects define the parameters of the network callback in terms of NetworkCapabilities. You create NetworkRequest objects with the NetworkRequest.Builder class. registerNetworkCallback() then passes the NetworkRequest object to the system. When the network conditions are met, the app receives a callback to execute the onAvailable() method defined in its ConnectivityManager.NetworkCallback class.
The app continues to receive callbacks until either the app exits or it calls unregisterNetworkCallback().
public class MainActivity extends Activity { private ConnectivityManager.NetworkCallback networkCallback; private ConnectivityManager connectivityManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); networkCallback = new NetworkCallbackImpl(); NetworkRequest.Builder builder = new NetworkRequest.Builder(); NetworkRequest request = builder.build(); connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); connectivityManager.registerNetworkCallback(request, networkCallback); } @Override protected void onDestroy() { super.onDestroy(); connectivityManager.unregisterNetworkCallback(networkCallback); } private class NetworkCallbackImpl extends ConnectivityManager.NetworkCallback { @Override public void onAvailable(Network network) { super.onAvailable(network); Toast.makeText(getBaseContext(), "onAvailable", Toast.LENGTH_SHORT).show(); } @Override public void onLosing(Network network, int maxMsToLive) { super.onLosing(network, maxMsToLive); Toast.makeText(getBaseContext(), "onLosing", Toast.LENGTH_SHORT).show(); } @Override public void onLost(Network network) { super.onLost(network); Toast.makeText(getBaseContext(), "onLost", Toast.LENGTH_SHORT).show(); } @Override public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) { super.onCapabilitiesChanged(network, networkCapabilities); Toast.makeText(getBaseContext(), "onCapabilitiesChanged", Toast.LENGTH_SHORT).show(); } @Override public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) { super.onLinkPropertiesChanged(network, linkProperties); Toast.makeText(getBaseContext(), "onLinkPropertiesChanged", Toast.LENGTH_SHORT).show(); } } }
看完上述內容,你們掌握如何在Android 7.0中監聽網絡的變化的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。