应用程序崩溃没有互联网访问

时间:2014-03-20 12:00:42

标签: android

我的应用主要方法如下:

public class MainActivity extends Activity{

    ImageButton btnRegister, btnConfig, btnSurvey, btnUpload;
    SQLiteDatabase profile;
    TextView txtv;
    TextView tv;
    EditText et;
    TextView txtLat;
    TextView txtLong;
    Spinner spinner;
    List<Integer> editTextIdList = new ArrayList<Integer>();
     int id = 0;
     Button btnLatLng;
     List<String> assetArray = new ArrayList<String>();
     //Upload server 

        private Socket socket;
        private static final int SERVERPORT = 6000;
        private static final String SERVER_IP = "115.254.100.2";


     //GPSTracker class
     GPSTracker gps;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
        isNetworkConnected();
        initializer();

         //Thread initialize
         new Thread(new ClientThread()).start();
}

public boolean isNetworkConnected() {
        final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
        return activeNetwork != null && activeNetwork.getState() == NetworkInfo.State.CONNECTED;
   }

它需要互联网访问。但没有互联网接入它就崩溃了。我怎样才能避免崩溃的应用程序,Instaed我可以给一个Toast meassge并退出应用程序而不会崩溃吗?我怎么能这样做?

5 个答案:

答案 0 :(得分:3)

试试这个:

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

if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED 
||  conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING  ) {
//notify  you are online
Toast.makeText(getApplicationContext(), "No Internet access available", 
Toast.LENGTH_LONG).show();
}
else if ( conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED 
||  conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {
//notify  you are not online
 Toast.makeText(getApplicationContext(), "No Internet access available", 
Toast.LENGTH_LONG).show();
}

您必须包含:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

在您的Manifest.xml文件中

答案 1 :(得分:1)

这个怎么样?:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 
    if (isNetworkConnected())
{
  initializer();
  //Thread initialize
   new Thread(new ClientThread()).start();
}
else
{
//not connected
}

}

答案 2 :(得分:0)

您是否已在清单中获得以下许可。

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

答案 3 :(得分:0)

public boolean isConnectingToInternet()
    {
        ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) 
        {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }

        }
        return false;
    }

答案 4 :(得分:0)

创建ConnectionDetector类

public class ConnectionDetector {

    private Context _context;

    public ConnectionDetector(Context context){
        this._context = context;
    }

    public boolean isConnectingToInternet(){
        ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
          if (connectivity != null) 
          {
              NetworkInfo[] info = connectivity.getAllNetworkInfo();
              if (info != null) 
                  for (int i = 0; i < info.length; i++) 
                      if (info[i].getState() == NetworkInfo.State.CONNECTED)
                      {
                          return true;
                      }

          }
          return false;
    }
}

并检查设备是否已连接到互联网。

在使用前初始化cd。     isInternetPresent = cd.isConnectingToInternet();

            // check for Internet status
            if (isInternetPresent) {
                // Internet Connection is Present
                // make HTTP requests
                showAlertDialog(AndroidDetectInternetConnectionActivity.this, "Internet Connection",
                        "You have internet connection", true);
            } else {
                // Internet connection is not present
                // Ask user to connect to Internet
                showAlertDialog(AndroidDetectInternetConnectionActivity.this, "No Internet Connection",
                        "You don't have internet connection.", false);
            }