2017-05-05 73 views
-1

场景是当我的应用程序处于脱机状态时,我正在将某些信息存储在我需要发送到服务器的数据库中。所以当网络可用时,我想通过Api调用发送这些。每当网络可用时,是否有任何方法可以调用方法?如何每次在Android中使用网络时调用方法?

+0

使用广播接收器 –

+0

你必须使用广播接收器 –

+1

如果您定位的Androidñ预览。根据Google的限制,Receiver无法工作。链接:https://developer.android.com/preview/features/background-optimization.html#connectivity-action – crashOveride

回答

0

创建广播接收器:

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> 
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> 
+0

谢谢你的答案。这对我帮助很大 –

相关问题