当设备使用启用的节电模式解锁时,我无法在新的 Android 版本上正确管理 Internet 连接。
android.permission.INTERNET 和 android.permission.ACCESS_NETWORK_STATE 权限被添加到清单文件中。
我注册收到的广播以在 Activity 启动时收听 ConnectivityManager.CONNECTIVITY_ACTION 并在停止时取消注册。当手动禁用\启用 Wi-FI 或蜂窝连接时,它可以完美运行。
我也使用方法来检查连接
private boolean isNetworkAvailable() {
ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = conn.getActiveNetworkInfo();
return null != networkInfo && networkInfo.isConnected();
}
每次手机解锁并且我的应用程序在前台时 isNetworkAvailable() 方法返回已连接但未连接。
我试图实现类似 ping 逻辑的东西,但是在解锁手机后,我收到的电话一直没有连接,直到禁用节电模式
try {
InetAddress inetAddress = InetAddress.getByName("www.google.com");
if (inetAddress.isReachable(1000)) {
// IS CONNECTED
}
} catch (IOException e) {
e.printStackTrace();
}
// IS NOT CONNECTED
有人知道如何处理 Android 7 和 Android 8 上的连接的好解决方案吗?
预先感谢
源代码 活动:
public class MainActivity extends AppCompatActivity implements ConnectionManager.ConnectionStatusListener {
private TextView textView;
private ConnectionManager cm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
cm = new ConnectionManager(getBaseContext());
//Show by default is disconnected
disconnected();
}
@Override
protected void onStart() {
super.onStart();
cm.register(this);
}
@Override
protected void onStop() {
super.onStop();
cm.unregister(this);
}
// ConnectionManager.ConnectionStatusListener implementation
@Override
public void connected() {
textView.setText("Connected");
textView.setTextColor(Color.GREEN);
}
@Override
public void disconnected() {
textView.setText("Disconnected");
textView.setTextColor(Color.RED);
}
}
我的连接管理器实现:
class ConnectionManager {
private final Context context;
private final Object syncObj = new Object();
private final LinkedList<ConnectionStatusListener> listeners = new LinkedList<>();
private final Handler uiHandler;
private final Handler ioHandler;
private final BroadcastReceiver connectivityActionBR = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
updateConnStatus();
}
};
private final Runnable pingRunnable = new Runnable() {
@Override
public void run() {
try {
InetAddress inetAddress = InetAddress.getByName("www.google.com");
if (!inetAddress.isReachable(1000)) {
notifyListeners(false);
startPingServerDelayed(500);
} else {
notifyListeners(true);
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
public ConnectionManager(Context context) {
this.context = context;
uiHandler = new Handler();
HandlerThread handlerThread = new HandlerThread("checkInternetConnectionThread");
handlerThread.start();
ioHandler = new Handler(handlerThread.getLooper());
// TODO: 9/18/17 add destroy to stop threadHandler
}
public void register(ConnectionStatusListener listener) {
synchronized (syncObj) {
if (!listeners.contains(listener)) {
listeners.add(listener);
}
registerBR();
}
}
public void unregister(ConnectionStatusListener listener) {
synchronized (syncObj) {
listeners.remove(listener);
}
unregisterBR();
stopPingServer();
}
private void registerBR() {
context.registerReceiver(connectivityActionBR, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
private void unregisterBR() {
context.unregisterReceiver(connectivityActionBR);
}
private void notifyListeners(final boolean isConnected) {
Log.e("---", "isConnected=" + isConnected);
uiHandler.post(new Runnable() {
@Override
public void run() {
synchronized (syncObj) {
for(ConnectionStatusListener listener : listeners) {
if (isConnected) {
listener.connected();
} else {
listener.disconnected();
}
}
}
}
});
}
private void updateConnStatus() {
if (!isNetworkAvailable()) {
stopPingServer();
notifyListeners(false);
}
startPingServerNow();
}
private void startPingServerDelayed(long millis) {
ioHandler.postDelayed(pingRunnable, millis);
}
private void startPingServerNow() {
ioHandler.post(pingRunnable);
}
private void stopPingServer() {
ioHandler.removeCallbacks(pingRunnable);
}
private boolean isNetworkAvailable() {
ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = conn.getActiveNetworkInfo();
return null != networkInfo && networkInfo.isConnectedOrConnecting();
}
public interface ConnectionStatusListener {
void connected();
void disconnected();
}
}