2013-04-02 23 views
0

數據方面,我想通過使用移動數據連接,從我的主服務器的數據。 當我的應用程序打開時,它應該通過數據連接自動連接到主機服務器。 當連接通過啓用移動不與藍牙或WiFi使用數據連接丟失......我的服務應該自動連接至主機服務器的情況。 請幫我從這種情況如何連接到編程中移動由Android

在我的代碼

我使用的檢查數據連接是否

ConnectivityManager cm =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 

    NetworkInfo netInfo = cm.getActiveNetworkInfo(); 

    if (netInfo != null && netInfo.isConnectedOrConnecting()) { 


    //some task 

     return true;     
    } 
    else{ 

    Toast.makeText(this, "No internet connection", Toast.LENGTH_SHORT).show(); 

    } 
    return false; 
+0

我不建議使用這種做法。用戶關閉數據連接可能有一個可行的原因。雖然我不確定,但我相信在這種情況下,嘗試啓用它只會導致錯誤。我建議你在後臺啓動一項服務,定期檢查連接是否再次可用,並讓你的應用程序同時休眠。如果用戶正在使用該應用程序,請捕獲前面提到的錯誤,並要求用戶打開連接。 – Eric

回答

2

使用這個類,因爲它是和調用它的方法時,你永遠要Enalbe /禁用DataConnection

從任何地方像這樣

InternetControl.EnableInternet(getBaseContext()); 
這個類的
package com.AZone.eabc; 

import java.lang.reflect.InvocationTargetException; 
import java.lang.reflect.Method; 
import java.lang.reflect.Field; 
import android.net.ConnectivityManager; 
import android.util.Log; 
import android.content.Context; 

public class InternetControl { 



    public static void EnableInternet(Context mycontext) 
    { 
     try { 
      Log.i("Reached Enable", "I am here"); 
      setMobileDataEnabled(mycontext,true); 
     } catch (NoSuchFieldException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ClassNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IllegalArgumentException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IllegalAccessException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (NoSuchMethodException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (InvocationTargetException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    public static void DisableInternet(Context mycontext) 
    { 
     try { 
      Log.i("Reached Disable", "I am here"); 
      setMobileDataEnabled(mycontext,false); 
     } catch (NoSuchFieldException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ClassNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IllegalArgumentException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IllegalAccessException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (NoSuchMethodException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (InvocationTargetException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    private static void setMobileDataEnabled(Context context , boolean enabled) throws NoSuchFieldException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { 
      final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 

      final Class conmanClass = Class.forName(conman.getClass().getName()); 

      final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService"); 
      iConnectivityManagerField.setAccessible(true); 
      final Object iConnectivityManager = iConnectivityManagerField.get(conman); 
      final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName()); 
      final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE); 
      setMobileDataEnabledMethod.setAccessible(true); 

      setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled); 
     } 

} 

呼叫方法在你個

ADD以下權限AndroidManifest文件

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/> 
    <uses-permission android:name="android.permission.INTERNET" /> 
+0

只是調用的方法啓用和禁用根據您的需要將編程啓用或禁用Dataconnection – DeltaCap

+0

---它亙古不變的使我dataconnection,它只是執行這些方法。手機中的SIM卡有問題嗎?我的手機中有兩個SIM卡。是neccessary提到在SIM dataconnection是它連接前使用..如果是的話,請上傳代碼 –

+0

你不能真正定位的SIM卡要直通代碼中使用。由於Android的默認做不支持雙SIM卡。公司在實際使該操作系統與雙SIM卡兼容之前對其進行了自定義。因此,沒有範圍... 此代碼不適用於FROYO ... 您默認選擇的用於數據連接的SIM實際上已啓用不指定任何代碼明確.. 我已經與Micromax的帆布2和帆布HD檢查它,而且它在那裏工作都是雙SIMS ... 但有ICS和傑利貝恩respectivly – DeltaCap

0

使用此方法來檢查,是您的設備連接到互聯網或沒有。 公共布爾checkInternetConnection(){

ConnectivityManager conMgr = (ConnectivityManager)activity.getSystemService(Context.CONNECTIVITY_SERVICE); 

    // ARE WE CONNECTED TO THE NET 

    if (conMgr.getActiveNetworkInfo() != null 

    && conMgr.getActiveNetworkInfo().isAvailable() 

    && conMgr.getActiveNetworkInfo().isConnected()) { 

     return true; 

    } else { 
     return false; 

    } 
} 

將這個點擊事件中:

boolean Connection; 
Connection = checkInternetConnection(); 
if(Connection==false){ 
      //No internet connection  

      } 
      else{ 
      //Here do what ever you do next 
      } 
1

看起來它並不在某些手機上的Android WITN工作的4.1.x 我一直在使用這種方法很長一段時間,但它崩潰三星5282的Android 4.1.2:

「NoSuchFieldException:MSERVICE」

貌似三星去除MSERVICE領域本身,因爲它仍然可以發現在GrepCode:

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.2_r1/android/net/ConnectivityManager.java/

+0

任何幫助檢查Android 4.1.2上的移動數據開/關 – User10001

相關問題