我怎样才能收到通知时,该设备失去网络连接? [英] How can I receive a notification when the device loses network connectivity?

查看:135
本文介绍了我怎样才能收到通知时,该设备失去网络连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可以写code,通过它,你可以决定是否将设备连接到网络。

但是,在我的应用程序是我想要做的就是一个通知,如果设备发生变化,从网络的状态为无网络。这会发生,例如,当用户行进到一个隧道和失去信号等或WiFi上时,用户外出的接入点的范围,不再能够访问因特网。

难道Android的API提供的东西,你可以注册一个监听器,让你得到通知时,都会在网络状态的变化?

我发现这个code,并试图使用它,但它不会做任何事情。我没有得到任何通知,当网络状态的变化。

 公共类ConnectivityManager扩展PhoneStateListener {

活动活性;
公共ConnectivityManager(活性){
    TelephonyManager telephonyManager =(TelephonyManager)a.getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(这一点,PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
    活性= A;
}

@覆盖
公共无效onDataConnectionStateChanged(INT状态){
    super.onDataConnectionStateChanged(州);
    开关(州){
    案例TelephonyManager.DATA_DISCONNECTED:
        新AlertDialog.Builder(活性)。
        setCancelable(假)。
        的setTitle(连接管理器)。
        setMessage(没有网络连接,请连接到互联网,并再次启动。)。
        setNeutralButton(OK,新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话,诠释whichButton){
                System.exit(0);
            }
        })。创建();
        打破;

    案例TelephonyManager.DATA_CONNECTED:
        打破;
    }
}
}
 

另外,我还添加了相应的权限在AndroidManifest.xml中:

 <使用-权限的Andr​​oid:名称=android.permission.INTERNET对>< /使用-许可>
<使用-权限的Andr​​oid:名称=android.permission.READ_PHONE_STATE>< /使用-许可>
<使用-权限的Andr​​oid:名称=android.permission.ACCESS_NETWORK_STATE>< /使用-许可>
 

解决方案

  myTelephonyManager =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

callStateListener =新PhoneStateListener(){
    公共无效onDataConnectionStateChanged(INT状态){
        开关(州){
        案例TelephonyManager.DATA_DISCONNECTED:
            Log.i(国家,离线);
            //字符串stateString =脱机;
            // Toast.makeText(getApplicationContext(),
            // stateString,Toast.LENGTH_LONG).show();
            打破;
        案例TelephonyManager.DATA_SUSPENDED:
            Log.i(国家,IDLE);
            // stateString =空闲;
            // Toast.makeText(getApplicationContext(),
            // stateString,Toast.LENGTH_LONG).show();
            打破;
        }
    }
};
myTelephonyManager.listen(callStateListener,
            PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
 

I know you can write code through which you can determine whether the device is connected to a network.

But in my app what I want to do is get a notification if the device changes its state from 'network' to 'no network'. This would happen, for example, when the user travels into a tunnel and loses signal, etc. Or when on WiFi, the user goes out of range of the access point and no longer has access to the internet.

Does the Android API provide something where you can register a listener so that you get notified every time there is a change in network state?

I found this code and tried to use it, but it does not do anything. I don't get any notifications when the network state changes.

public class ConnectivityManager extends PhoneStateListener{

Activity activity;
public ConnectivityManager(Activity a){
    TelephonyManager telephonyManager = (TelephonyManager)a.getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(this, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
    activity = a;
}

@Override
public void onDataConnectionStateChanged(int state) {
    super.onDataConnectionStateChanged(state);
    switch (state) {
    case TelephonyManager.DATA_DISCONNECTED:
        new AlertDialog.Builder(activity).
        setCancelable(false).
        setTitle("Connection Manager").
        setMessage("There is no network connection. Please connect to internet and start again.").
        setNeutralButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                System.exit(0);
            }
        }).create();
        break;

    case TelephonyManager.DATA_CONNECTED:
        break;
    }
}
}

Also, I have added the appropriate permissions in AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

解决方案

myTelephonyManager=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

callStateListener = new PhoneStateListener(){
    public void onDataConnectionStateChanged(int state){
        switch(state){
        case TelephonyManager.DATA_DISCONNECTED:
            Log.i("State: ", "Offline");
            // String stateString = "Offline";
            // Toast.makeText(getApplicationContext(),
            // stateString, Toast.LENGTH_LONG).show();
            break;
        case TelephonyManager.DATA_SUSPENDED:
            Log.i("State: ", "IDLE");
            // stateString = "Idle";
            // Toast.makeText(getApplicationContext(),
            // stateString, Toast.LENGTH_LONG).show();
            break;
        }
    }       
};                      
myTelephonyManager.listen(callStateListener,
            PhoneStateListener.LISTEN_DATA_CONNECTION_STATE); 

这篇关于我怎样才能收到通知时,该设备失去网络连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆