gpt4 book ai didi

互联网连接状态更改的 Android 事件

转载 作者:IT王子 更新时间:2023-10-28 23:40:51 29 4
gpt4 key购买 nike

我正在制作一个应用程序,其中用户会频繁地将信息和文件上传到我的服务器。这是通过专门的上传服务在新线程中完成的。

我从这个帖子知道

Detect whether there is an Internet connection available on Android

您可以相对轻松地检查是否有互联网连接。您还可以获取 socketTimeoutExceptions 来检测互联网连接问题。这一切都很好,并且让我可以在连接由于某种原因无法正常工作时轻松缓存我的上传。

但我的问题是我如何知道何时重新尝试上传?连接恢复时是否触发事件?或者我是否坚持创建一个新线程,然后每 30 秒检查一次互联网连接或其他什么?

任何想法都将不胜感激!

最佳答案

很老的帖子但我想分享我的接收器

无需将您的手放在 list 或其他无聊的资源上 :)

用法

您的 Activity :

/*
* You need to implement NetworkStateReceiverListener.
* This interface is described inside the NewtworkStateReceiver class
*/
public class MyActivity implements NetworkStateReceiverListener {
/* ... */
private NetworkStateReceiver networkStateReceiver;
}

在您的 Activity 中:实例化接收器

public void onCreate(Bundle savedInstanceState) {
/* ... */
networkStateReceiver = new NetworkStateReceiver();
networkStateReceiver.addListener(this);
this.registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
}

public void onDestroy() {
super.onDestroy();
networkStateReceiver.removeListener(this);
this.unregisterReceiver(networkStateReceiver);
}

在您的 Activity 中:实现所需的方法

@Override
public void networkAvailable() {
Log.d("tommydevall", "I'm in, baby!");
/* TODO: Your connection-oriented stuff here */
}

@Override
public void networkUnavailable() {
Log.d("tommydevall", "I'm dancing with myself");
/* TODO: Your disconnection-oriented stuff here */
}

接收者

只需将 NetworkStateReceiver.java 复制并粘贴到您的项目中

public class NetworkStateReceiver extends BroadcastReceiver {

protected Set<NetworkStateReceiverListener> listeners;
protected Boolean connected;

public NetworkStateReceiver() {
listeners = new HashSet<NetworkStateReceiverListener>();
connected = null;
}

public void onReceive(Context context, Intent intent) {
if(intent == null || intent.getExtras() == null)
return;

ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = manager.getActiveNetworkInfo();

if(ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
connected = true;
} else if(intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) {
connected = false;
}

notifyStateToAll();
}

private void notifyStateToAll() {
for(NetworkStateReceiverListener listener : listeners)
notifyState(listener);
}

private void notifyState(NetworkStateReceiverListener listener) {
if(connected == null || listener == null)
return;

if(connected == true)
listener.networkAvailable();
else
listener.networkUnavailable();
}

public void addListener(NetworkStateReceiverListener l) {
listeners.add(l);
notifyState(l);
}

public void removeListener(NetworkStateReceiverListener l) {
listeners.remove(l);
}

public interface NetworkStateReceiverListener {
public void networkAvailable();
public void networkUnavailable();
}
}

享受 ;)

关于互联网连接状态更改的 Android 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6169059/

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