在我的應用程序中,我調用WebServices並獲取GPRS值。問題是,當互聯網沒有連接時,我的應用程序會生成「強制關閉」消息。請引導我,它是如何產生消息,互聯網不可用或請連接互聯網,它不會產生強制關閉的消息。感謝您的幫助生成互聯網未連接或啓用的消息
請引導我移動網絡。如果它不在我的應用程序生成消息,請在移動網絡上。怎麼可能?
感謝Kittu & RAM基蘭幫助:)
在我的應用程序中,我調用WebServices並獲取GPRS值。問題是,當互聯網沒有連接時,我的應用程序會生成「強制關閉」消息。請引導我,它是如何產生消息,互聯網不可用或請連接互聯網,它不會產生強制關閉的消息。感謝您的幫助生成互聯網未連接或啓用的消息
請引導我移動網絡。如果它不在我的應用程序生成消息,請在移動網絡上。怎麼可能?
感謝Kittu & RAM基蘭幫助:)
private boolean haveNetworkConnection()
{
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo)
{
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}
調用此方法時,你正在試圖調用web服務
if(haveNetworkConnection())
{
// call the webservice
}
清單中
添加<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
許可,您可以這樣做:
//check connection
public boolean isOnline() {
ConnectivityManager cm =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
//alert box to show internet connection error
AlertDialog.Builder Internet_Alert = new AlertDialog.Builder(DinnerDenActivity.this);
// set title
Internet_Alert.setTitle("Attention!");
Internet_Alert.setMessage("This application requires internet connectivity, no internet connection detected");
Internet_Alert.setPositiveButton("Quit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1)
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
onQuitPressed();
}
});
Internet_Alert.create().show();
return false;
}
個
isOnline檢查互聯網連接
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
此權限應在清單文件中添加。
非常感謝你kittu :) – bushra
您還可以使用的廣播接收機這樣
public class NetworkBroadcastReceiver extends BroadcastReceiver{
/*
* (non-Javadoc)
*
* @see android.content.BroadcastReceiver#onReceive(android.content.Context,
* android.content.Intent)
*/
@Override
public void onReceive(Context context, Intent intent) {
boolean isNetworkDown= intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
if(isNetworkDown){
ApplicationInfo.networkAvailable = false;
}else{
ApplicationInfo.networkAvailable = true;
}
}
}
,並調用Web服務之前,您可以檢查ApplicationInfo.networkAvailable
這樣
if(!ApplicationInfo.networkAvailable){
//show msg -> no network connectivity
}else{
//proceed with WS call
}
註冊您的接收器CONNECTIVITY_ACTION
。這可以在代碼完成和AndroidManifest文件
在你的應用
在上面的鏈接,我們檢查所有類型使用警報Dailog互聯網連接的按照此 Link for check Internet Connection。 你可以在你的應用程序中使用它來避免force close
你的應用程序。
非常感謝Arvind。 – bushra
非常感謝你Ram基因:) – bushra