Catch SecurityException when getting network type.

This commit is contained in:
Alex Ning 2021-08-19 20:59:24 +08:00
parent e2cca45f55
commit cc1e4794c9
2 changed files with 13 additions and 11 deletions

View File

@ -220,6 +220,7 @@ public class Utils {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Network nw = connMgr.getActiveNetwork(); Network nw = connMgr.getActiveNetwork();
if (nw == null) return NETWORK_TYPE_OTHER; if (nw == null) return NETWORK_TYPE_OTHER;
try {
NetworkCapabilities actNw = connMgr.getNetworkCapabilities(nw); NetworkCapabilities actNw = connMgr.getNetworkCapabilities(nw);
if (actNw != null) { if (actNw != null) {
if (actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { if (actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
@ -229,6 +230,7 @@ public class Utils {
return NETWORK_TYPE_CELLULAR; return NETWORK_TYPE_CELLULAR;
} }
} }
} catch (SecurityException ignore) {}
return NETWORK_TYPE_OTHER; return NETWORK_TYPE_OTHER;
} else { } else {
boolean isWifi = false; boolean isWifi = false;

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
@ -22,7 +22,7 @@
android:id="@+id/fetch_messages_info_linear_layout_inbox_fragment" android:id="@+id/fetch_messages_info_linear_layout_inbox_fragment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerInParent="true" android:layout_gravity="center"
android:gravity="center" android:gravity="center"
android:orientation="vertical" android:orientation="vertical"
android:visibility="gone"> android:visibility="gone">
@ -43,4 +43,4 @@
</LinearLayout> </LinearLayout>
</RelativeLayout> </FrameLayout>