简体   繁体   English

如何在我的android应用程序运行期间检查网络是否具有更改状态?

[英]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... 创建一个带有IntentFilterBroadcastReceiver ,以“监听” ...

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.

相关问题 如何在给定时间后自动运行 android 应用程序 - How can I automatically run an android application after a given time 设备处于锁定状态且屏幕处于关闭状态时,如何运行我的应用程序? - How can I run my application when device is in locked state and screen is in turned off state? 如何在运行时通过Netbeans窗口向Java应用程序提供输入? - How can I provide inputs to a java application during run-time, through netbeans window? 如何在Android应用程序中更改(线程的)堆栈大小? - How can I change the stack size (of a thread) in my Android Application? 如何获取用户在android中运行应用程序的“最后”日期或时间? - How to get the “last” date or time that the user has run the application in android? 在我的android应用程序与网络服务之间的通信过程中,如何管理连接丢失? - How can I manage connection loss during the communication between my android application and the webservice? 在游戏过程中如何更改Timer()的速度? - How can i change the speed on my Timer() during my game? 如何在Android应用程序中运行Web应用程序的网页 - How can I run the web page of my web application in an android application 如何在Android应用程序中将Bluetooth适配器状态更改为在线? - How can I change the Bluetooth Adapter state to online in my Android app? 如何检查我的Firebase数据库是否具有特定用户的数据? (Android / Java) - How can I check if my Firebase database has data for a certain user? (Android/Java)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM