使用已启用的电池保护程序在Android Nougat或Android奥利奥上管理互联网连接
问题描述:
当设备使用启用的电池保护程序解锁时,我无法在新的Android版本上正确管理互联网连接。使用已启用的电池保护程序在Android Nougat或Android奥利奥上管理互联网连接
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()方法返回连接,但事实并非如此。
我试图实现类似平安的逻辑,但解开我收到没有连接所有的时间,直到关闭电池节电模式
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();
}
}
答
注册中的onCreate(您广播)和的onDestroy注销它( )。让我,你有什么工作。
它没有帮助,因为当我注册BR下onCreate()onReceive()调用时只收到一次 – Dimaslviv
有什么建议吗? – Dimaslviv
我有相同的代码,你有。正如我所说的,我在ONCREATE注册了我的BR,但也许是您用来测试的设备。您是否尝试过使用其他设备?你必须把它放在那里的原因是因为你创造了你的活动,并且至少你会杀了它。 –