[英]How can I check if the network has change state during run time of my android application?
I'am developing an application for android, which needs network in order to retrieve some information from an xml file. 我正在为android开发一个应用程序,该应用程序需要网络才能从xml文件中检索一些信息。 I've got this code, that looking for if the users has enabled the network:
我有以下代码,用于查找用户是否启用了网络:
public boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
return true;
}
return false;
}
I use this function this way: 我通过这种方式使用此功能:
boolean connected = isOnline();
if(connected)
getLocation();
else
showDialogNetwork();
The function showDialogNetwork() : 函数showDialogNetwork() :
public void showDialogNetwork() {
new AlertDialog.Builder(this)
.setMessage("No network")
.setTitle("Error")
.setCancelable(false)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// launch settings
Intent settings = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
startActivity(settings);
}
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
// finish activity
finish();
}
})
.show();
}
If the network on the device is disabled the showDialogNetwork() will open the network tab in the settings menu. 如果禁用了设备上的网络,则showDialogNetwork()将在设置菜单中打开“网络”选项卡。 But when the users turn on the WiFi, and the application is running in the background, it doesn't "understand" that the network is turned on.
但是,当用户打开WiFi且应用程序在后台运行时,并不能“理解”网络已打开。 I need the "kill" the app after the network is turned on, to get the information from the internet.
打开网络后,我需要“杀死”该应用程序,以便从互联网上获取信息。 How can I constantly check if the network has change state during run time of my application?
如何在应用程序运行期间不断检查网络是否已更改状态? Thanks in advance, guys :)
预先感谢,伙计们:)
Create a BroadcastReceiver
with an IntentFilter
which 'listens' for... 创建一个带有
IntentFilter
的BroadcastReceiver
,以“监听” ...
WifiManager.NETWORK_STATE_CHANGED_ACTION
You can either register for that in code or if you register the BroadcastReceiver
in your manifest then use the following for the <intent-filter>
您可以在代码中注册,也可以在清单中注册
BroadcastReceiver
,然后将以下内容用于<intent-filter>
android.net.wifi.STATE_CHANGE
See WifiManager.WIFI_STATE_CHANGED_ACTION 请参阅WifiManager。WIFI_STATE_CHANGED_ACTION
To also monitor mobile Internet connectivity change you would 'listen' for... 要同时监视移动Internet连接的更改,您将“监听” ...
ConnectivityManager.CONNECTIVITY_ACTION ConnectivityManager.CONNECTIVITY_ACTION
In this case use the following in the manifest... 在这种情况下,请在清单中使用以下内容...
android.net.conn.CONNECTIVITY_CHANGE
You can also listen to changes to your Data Connection using the TelephonyManager - 您还可以使用TelephonyManager监听对数据连接的更改-
http://developer.android.com/reference/android/telephony/PhoneStateListener.html#onDataConnectionStateChanged(int ) http://developer.android.com/reference/android/telephony/PhoneStateListener.html#onDataConnectionStateChanged(int )
Even better, register for android.net.conn.CONNECTIVITY_CHANGE broadcast action which includes wifi, data etc - 更好的是,注册android.net.conn.CONNECTIVITY_CHANGE广播操作,其中包括wifi,数据等-
http://developer.android.com/reference/android/net/ConnectivityManager.html#CONNECTIVITY_ACTION http://developer.android.com/reference/android/net/ConnectivityManager.html#CONNECTIVITY_ACTION
<receiver android:name="com.yourapp.receivers.ConnectionChangeReceiver"
android:label="Connection">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.