如何处理Internet连接在Java类中的Android

如何处理Internet连接在Java类中的Android

问题描述:

如何处理,如果我有互联网连接状况不佳或如果我失去了我的连接,我也不会在Java类处理它, 在活动我可以检查互联网连接,但这里没有, 所以我绑来处理了互联网连接,同时跟踪如何处理Internet连接在Java类中的Android

@Override 
public void onLocationChanged(Location location) { 
    sharedPreferences = mContext.getSharedPreferences(PREFS_NAME, 0); 
    editor = sharedPreferences.edit(); 
    emailSharedPref = sharedPreferences.getString("email", ""); 
    Log.e("emailLocation", emailSharedPref); 
    Log.i("long", "" + location.getLongitude() + " TIME: " + t.time()); 
    getAdress(location.getLatitude(),location.getLongitude()); 
     JSONObject jsonObject = new JSONObject(); 
     URLPath urlPath = new URLPath(); 
     String serverURL = urlPath.trackEmployee; 
     WebServiceRequest request = new WebServiceRequest(); 
     request.setUrl(serverURL); 
     try { 
      jsonObject.put("latitude", location.getLatitude()); 
      jsonObject.put("longitude", location.getLongitude()); 
      jsonObject.put("street", street); 
      jsonObject.put("district", district); 
      jsonObject.put("city", city); 
      jsonObject.put("time", t.time()); 
      jsonObject.put("date", t.date()); 
      jsonObject.put("email", sharedPreferences.getString("email", "")); 

      Log.e("jsonLocation", jsonObject.toString()); 
     } catch (JSONException e) 
     { 
      e.printStackTrace(); 

     } 

     request.setRequestBody(jsonObject); 
     WebServiceAsyncTask webService = new WebServiceAsyncTask(); 
     WebServiceRequest[] requestArr = {request}; 
     webService.execute(requestArr); 


} 


    public void getAdress(double longt, double lat) { 
    try { 
     addresses = geocoder.getFromLocation(longt, lat, 1); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    if (addresses != null) { 
     street = addresses.get(0).getAddressLine(0); 
     district = addresses.get(0).getAddressLine(1); 
     city = addresses.get(0).getAddressLine(2); 
     connection="on"; 
     Log.d("connection",connection+".."+addresses.toString()); 
    }else{ 
     connection="off"; 
     Log.d("connection",connection); 
    } 
} 
+0

你应该写一个可运行的挤兑或检查每30秒连接 – Eenvincible

+0

http://*.com/questions/25678216/android-internet-connectivity-change-listener –

可以使用ConnectivityManager注册一个BroadcastReceiver与行动ConnectivityManager.CONNECTIVITY_ACTION。如果连接改变,广播事件将被发送。在onReceive方法中,您可以更新标志并在建立方法中的任何连接之前使用这些标志进行检查。

例如。

private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     checkStatus(); 
    } 
}; 

private void checkStatus(){ 
    ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); 
    //for airplane mode, networkinfo is null 
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); 
    //connection details 
    String status = networkInfo.getState().toString(); 
    String type = networkInfo.getTypeName(); 
    boolean isConnected = networkInfo.isConnected(); 
    //update flags here and check them before establishing connection 
} 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    IntentFilter intentFilter =new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); 
    registerReceiver(mBroadcastReceiver,intentFilter); 
} 
+0

请举例:) –

+0

请检查更新的答案。 – void

一个简单的方法来检查是否连接到网络存在

ü可以使用它,其中u想强行通过一个有效的应用程序上下文

/** 
* check for network connection 
*/ 
public static boolean isOnline(Context context) { 
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); 
    return activeNetworkInfo != null && activeNetworkInfo.isConnected(); 
} 

注:这不做出决议网络资源的可访问性,你查询

我建议我使用HttpURLConnection的中的AsyncTask使用,并试图处理SocketTimeoutException,以避免为@泰勒 - 塞巴斯蒂安差数据连接

问题提出:

Handler mHandler; 
    public void useHandler() { 
    mHandler = new Handler(); 
    mHandler.postDelayed(mRunnable, 1000); 
    } 

    private Runnable mRunnable = new Runnable() { 

     @Override 
     public void run() { 
      Log.e("Handlers", "Calls"); 
      /** Do something **/ 
      mHandler.postDelayed(mRunnable, 1000); 
     } 
    }; 

如何从处理器

    删除等待执行
  • mHandler.removeCallbacks(mRunnable);

如何安排再次

  • mHandler.postDelayed(mRunnable,1000);

UI线程下的Runnable的作品,所以你可以在处理程序更新的UserInterface各自的Runnable

然后u可以使用View.sendPostD Here u got more example

+0

我应该在这个函数的参数中传递什么?这class.java不活动#帮助:d –

+0

@AyaRadwan - 任何上下文(在活动ActivityName.this,在片段getActivity()等) – ceph3us