【问题标题】:App crashes without internet access应用程序在无法访问互联网的情况下崩溃
【发布时间】:2014-03-20 12:00:41
【问题描述】:

我的应用主方法如下所示:

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;
   }

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

【问题讨论】:

标签: android


【解决方案1】:

试试这个:

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 文件中

【讨论】:

  • 我需要把它放在 protected void onCreate(Bundle savedInstanceState) 的开头吗??
【解决方案2】:

这个怎么样?

@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
}

}

【讨论】:

    【解决方案3】:

    您是否在清单中授予了以下权限。

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

    【讨论】:

      【解决方案4】:
      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;
          }
      

      【讨论】:

        【解决方案5】:

        创建 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);
                    }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-03-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-15
          相关资源
          最近更新 更多