场景是当我的应用程序离线时,我在数据库中存储了一些我需要发送到服务器的信息。因此,当网络可用时,我想通过Api呼叫发送它们。每当网络可用时,有没有办法调用方法?
答案 0 :(得分:0)
/// BROADCAST RECEIVER CLASS
public class UpdateReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent arg1)
{
boolean isConnected = arg1.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
if (isConnected) {
} else {
//FETCH YOUR DATA FROM SQLITE
//CALL API
}
}
}
/// ANDROID MANIFEST CODE
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<receiver android:name="com.example.welcome.nkew.UiUtils.UpdateReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
答案 1 :(得分:0)
创建broadcastReceiver:
public class InternetBroadcastReceiver extends BroadcastReceiver {
static final String TAG = "InternetReceiver";
private static int networkType = -1;
private static boolean connectedWithNetwork = false;
public static void initNetworkStatus(Context context) {
ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
networkType = -1;
if (networkInfo != null) {
Log.d(TAG, "Init: ACTIVE NetworkInfo: " + networkInfo.toString());
if (networkInfo.isConnected()) {
networkType = networkInfo.getType();
}
}
Log.d(TAG, "initNetworkStatus -> " + networkType);
}
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive " + intent.getAction());
try {
if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {
Log.d(TAG, "System shutdown, stopping yaxim.");
} else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
boolean isConnected = (networkInfo != null) && (networkInfo.isConnected() == true);
boolean wasConnected = (networkType != -1);
if (connectedWithNetwork && isConnected)
return;
connectedWithNetwork = isConnected;
if (wasConnected && !isConnected) {
Log.d(TAG, "we got disconnected");
networkType = -1;
} else if (isConnected && (networkInfo.getType() != networkType)) {
Log.d(TAG, "we got (re)connected: " + networkInfo.toString());
networkType = networkInfo.getType();
try {
Intent bIntent = new Intent("custom-event-name");
bIntent.putExtra("subject", Utils.SUBJECT_INTERNET_CONNECTED);
LocalBroadcastManager.getInstance(context).sendBroadcast(bIntent);
} catch (Exception e) {
e.printStackTrace();
}
} else if (isConnected && (networkInfo.getType() == networkType)) {
Log.d(TAG, "we stay connected, sending a ping");
try {
Intent bIntent = new Intent("custom-event-name");
bIntent.putExtra("subject", Utils.SUBJECT_INTERNET_CONNECTED);
LocalBroadcastManager.getInstance(context).sendBroadcast(bIntent);
} catch (Exception e) {
e.printStackTrace();
}
} else
return;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
并在清单文件中注册
<receiver android:name=".packagename.InternetBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>