2017-05-05 81 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

謝謝你的答案。這對我幫助很大 –

相關問題