如何在设备丢失networking连接时收到通知?

我知道你可以编写代码,通过它你可以确定设备是否连接到networking。

但在我的应用程序,我想要做的是获得通知,如果设备从“networking”更改为“无networking”的状态。 例如,当用户进入隧道并丢失信号等时,会发生这种情况。或者当在WiFi上时,用户超出接入点的范围并且不能再访问互联网。

Android API是否提供了可以注册侦听器的内容,以便在每次networking状态发生变化时都能得到通知?

我发现这个代码,并试图使用它,但它没有做任何事情。 networking状态改变时,我没有收到任何通知。

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; } } } 

另外,我在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> 

Solutions Collecting From Web of "如何在设备丢失networking连接时收到通知?"

 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); 

您可能需要考虑使用BroadcastReceiver来代替ConnectivityManager.CONNECTIVITY_ACTION 。 从文档:

networking连接发生了变化。 连接已build立或丢失。 受影响的networking的NetworkInfo作为额外的发送; 应该查看发生了什么样的连接事件。

这款接收器适用于WiFi和蜂窝数据连接,不像PhoneStateListener.LISTEN_DATA_CONNECTION_STATE ,它只会通知您蜂窝networking的变化。

接受的答案不适合我(我的平板电脑连接到WiFi – 没有3G)。 下面的代码为我工作:

 public class ConnectivityChangeReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { System.out.println("*** Action: " + intent.getAction()); if(intent.getAction().equalsIgnoreCase("android.net.conn.CONNECTIVITY_CHANGE")) { Toast.makeText(context, "Connection changed", Toast.LENGTH_SHORT).show(); } } } 

并更改为AndroidManifest.xml

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <receiver android:name=".custom.ConnectivityChangeReceiver"> <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver> 

不知道这是否会帮助,但这看起来类似 …

我通过轮询ConnectivityManager来实现此function。

 _connectivityManager = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE); 

一旦我们有一个连接pipe理器,只是每隔一段时间轮询它,看看你是否仍然连接

 private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); final Runnable networkStatusChecker = new Runnable() { public void run() { NetworkInfo activeNetwork = _connectivityManager.getActiveNetworkInfo(); boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting(); if(!isConnected) { System.out.println("disconnected from network"); networkCheckerHandle.cancel(true); } } }; final ScheduledFuture networkCheckerHandle = scheduler.scheduleAtFixedRate(networkStatusChecker, 10, 10, SECONDS);