如何在Android系统中检查网络连接?
本例演示如何通过广播接收器检查网络连接状态。
步骤1 − 在Android Studio中创建一个新项目,转到文件 ⇒ 新建项目,并填写所有必需的详细信息以创建新项目。
步骤2 − 要查找互联网状态,我们必须将网络状态权限添加到AndroidManifest.xml文件,如下所示。
<?xml version="1.0" encoding = "utf-8"?> <manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "com.example.andy.myapplication"> <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"> <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>
步骤3 − 以下是修改后的主活动文件MainActivity.java的内容。此文件可以包含每个基本生命周期方法。我们创建了一个文本视图,当用户点击文本视图时,它将调用broadcastIntent()方法来广播CONNECTIVITY_ACTION意图。
import android.content.BroadcastReceiver; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private BroadcastReceiver MyReceiver = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MyReceiver = new MyReceiver(); TextView click=findViewById(R.id.click); click.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { broadcastIntent(); } }); } public void broadcastIntent() { registerReceiver(MyReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); } @Override protected void onPause() { super.onPause(); unregisterReceiver(MyReceiver); } }
步骤4 − 创建一个NetworkUtil类来查找网络状态,如下所示。
import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; class NetworkUtil { public static String getConnectivityStatusString(Context context) { String status = null; ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); if (activeNetwork != null) { if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) { status = "Wifi enabled"; return status; } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) { status = "Mobile data enabled"; return status; } } else { status = "No internet is available"; return status; } return status; } }
步骤5 − 创建一个广播接收器类,并命名为MyReceiver.java。此广播接收器将从NetworkUtil类更新UI。
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String status = NetworkUtil.getConnectivityStatusString(context); if(status.isEmpty()) { status="No Internet Connection"; } Toast.makeText(context, status, Toast.LENGTH_LONG).show(); } }
步骤6 − 在清单文件中更新您的广播接收器,如下所示。
<?xml version = "1.0" encoding = "utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package = "com.example.andy.myapplication"> <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"> <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 = "MyReceiver"> <intent-filter> <action android:name = "android.net.conn.CONNECTIVITY_CHANGE" /> <action android:name = "android.net.wifi.WIFI_STATE_CHANGED" /> </intent-filter> </receiver> </application> </manifest>
步骤7 − 以下是res/layout/activity_main.xml文件的内容,其中包含一个文本视图以广播连接状态意图。
<?xml version = "1.0" encoding = "utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity"> <TextView android:id="@+id/click" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click here" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
让我们尝试运行您的应用程序。我假设您已将实际的Android移动设备连接到计算机。要在Android Studio中运行应用程序,请打开项目中的一个活动文件,然后单击运行 工具栏中的图标。选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕。
在上面的屏幕中,我们选择了wifi连接,输出应如下所示:
在上面的屏幕中,我们选择了wifi连接,输出应如下所示:
点击 此处 下载项目代码
广告