gpt4 book ai didi

android - 在启用省电模式的 Android Nougat 或 Android Oreo 上管理互联网连接

转载 作者:行者123 更新时间:2023-11-29 02:37:22 25 4
gpt4 key购买 nike

当设备在启用节电模式的情况下解锁时,我无法在新的 Android 版本上正确管理互联网连接。

android.permission.INTERNET 和 android.permission.ACCESS_NETWORK_STATE 权限已添加到 list 文件。

我在 Activity 启动时注册接收到的广播以监听 Co​​nnectivityManager.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 上的连接的好的解决方案吗?

先谢谢

源代码 Activity :

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

最佳答案

在 onCreate() 中注册您的广播并在 onDestroy() 中注销它。让我,你所拥有的必须发挥作用。

关于android - 在启用省电模式的 Android Nougat 或 Android Oreo 上管理互联网连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46287015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com