我正在研究Android应用程序,它主要在互联网上运行。现在问题是当我使用我的应用程序时,由于互联网应用程序崩溃了。事情是在打开妻子之后,我编写了使用连接管理器类来获取网络状态的代码,它给网络带来了好处并且返回了真值,但实际上通过网络的数据没有处理(这意味着没有互联网访问)。那么,当网络状态为真且网络访问为假(无法访问Internet)时,如何防止应用程序崩溃消息。
答案 0 :(得分:2)
在从您的应用使用互联网之前,尝试检查设备是否已连接
ConnectivityManager con=(ConnectivityManager)getSystemService(Activity.CONNECTIVITY_SERVICE);
boolean wifi=con.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
boolean internet=con.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
//check Internet connection
if(internet||wifi)
{
//your code
}else{
new AlertDialog.Builder(this)
.setIcon(R.drawable.title)
.setTitle("No internet connection")
.setMessage("Please turn on mobile data")
.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
//code for exit
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
})
.show();
}
不要忘记添加所需的权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
答案 1 :(得分:0)
试试这段代码:
//Check if Internet Network is active
private boolean checkNetwork() {
boolean wifiDataAvailable = false;
boolean mobileDataAvailable = false;
ConnectivityManager conManager = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] networkInfo = conManager.getAllNetworkInfo();
for (NetworkInfo netInfo : networkInfo) {
if (netInfo.getTypeName().equalsIgnoreCase("WIFI"))
if (netInfo.isConnected())
wifiDataAvailable = true;
if (netInfo.getTypeName().equalsIgnoreCase("MOBILE"))
if (netInfo.isConnected())
mobileDataAvailable = true;
}
return wifiDataAvailable || mobileDataAvailable;
}
将代码放在try catch块中以查看异常。
答案 2 :(得分: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;
}
}
现在在您的Activity中将onCreate()方法称为
ConnectionDetector cd = new ConnectionDetector(this);
if(cd.isConnectingToInternet) {
// do your stuff here
} else {
// No Internet Connection
}
答案 3 :(得分:0)
//define global variable
public static boolean isHaveInternet = true;
//add permission in androidmainfest file
<receiver android:name="NetworkReciver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
//add class for networkcheck
package com.example;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class NetworkReciver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
boolean status = GeneralClass.getConnectivityStatusString(context);
GeneralClass.isHaveInternet = status ? true : false;
if (status)
Log.e("NET-------------->", "connecte" + status);
else
Log.e("NET------------->", "not connecte" + status);
}
}
//check network connection where you want
if (GeneralClass.isHaveInternet)
{
//your code
}
else
{
Toast.makeText(getApplicationContext(),
"Internet connection not avilable",
Toast.LENGTH_SHORT).show();
}