我有这个Android应用程序,它做某些需要在线连接的东西,但如果手机没有连接到互联网,我想发送到在线数据库的条目保存在sqlite表中。 我的问题是,一旦手机连接到互联网,我该如何重新发送这些条目,我应该搜索哪些主题?
答案 0 :(得分:1)
尝试使用BroadcastReceiver监控手机的互联网状态。
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo active = manager.getActiveNetworkInfo();
if(active == null) { //no internet
}
if(mobile.isConnected()) { //mobile connetcion
}
if(wifi.isConnected()) { //wifi connetcion
}
}
}
AndroidManifest.xml中的
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<application
...
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
</application>