我正在開發Android應用程序,主要在互聯網上運行。現在的問題是當我使用我的應用程序,由於互聯網應用程序崩潰。事情是在妻子接通之後,我編寫了使用連接管理器類來獲取網絡狀態的代碼,它賦予網絡良好並返回一個真正的價值,但實際上通過網絡的數據沒有處理(這意味着沒有互聯網訪問)。所以,當網絡狀態爲true並且網絡訪問爲假(無法訪問Internet)時,如何防止應用程序崩潰消息。檢查如何停止與網絡連接時崩潰的android應用程序?
回答
嘗試,如果設備連接或不
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" />
這檢查移動互聯網或wifi是否打開,但沒有提供有關真實連接的信息...例如,wifi可能是但供應商禁用互聯網訪問 –
爲此,您需要創建自定義功能 –
您會指導編寫自定義功能嗎? – ponduri
嘗試此代碼之前使用互聯網從您的應用程序:
//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塊中以查看異常。
請告訴解決方案上網不能用於網絡連接狀態 – ponduri
讓一個類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;
}
}
在活動
現在調用它的onCreate()方法
ConnectionDetector cd = new ConnectionDetector(this);
if(cd.isConnectingToInternet) {
// do your stuff here
} else {
// No Internet Connection
}
我的問題是沒有檢測到連接狀態。問題在於網絡訪問。感謝您的評論 – ponduri
//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();
}
請告訴解決方案的互聯網訪問不是爲了網絡連接狀態。感謝您的回覆 – ponduri
- 1. Android:當網絡連接不工作時,應用程序崩潰
- 2. 當沒有網絡連接時停止WP7應用程序崩潰
- 3. Android網絡連接崩潰
- 4. 連接停止時android藍牙崩潰
- 5. 連接到WLAN網絡時Android崩潰
- 6. 如何防止WiFi連接丟失時應用程序崩潰
- 7. 當互聯網連接關閉時,Android應用程序崩潰
- 8. 應用程序崩潰與Facebook連接
- 9. 當網絡不可用時,防止應用程序崩潰
- 10. aac播放器應用程序崩潰,如果沒有網絡連接android
- 11. 無線網絡啓用/禁用時,Android應用程序崩潰
- 12. NSZombieEnabled停止應用程序崩潰
- 13. Android - 應用程序在停止MediaPlayer onPause時崩潰
- 14. iPhone - 應用程序崩潰與可憐的互聯網連接
- 15. 應用程序崩潰時的AsyncTask沒有互聯網連接
- 16. Android的線程應用程序崩潰時,有沒有互聯網連接的
- 17. Android應用程序崩潰沒有互聯網連接?
- 18. C應用程序使用GetOpenFileName崩潰。 DEP停止崩潰
- 19. 當互聯網連接不可用時,應用程序崩潰
- 20. Facebook的連接崩潰應用程序
- 21. 當沒有互聯網連接時應用程序崩潰
- 22. 當沒有互聯網連接時,應用程序崩潰
- 23. 如何禁用/啓用android應用程序的網絡連接?
- 24. 網絡程序崩潰
- 25. Android:連接未建立時應用程序崩潰
- 26. Android應用程序崩潰時,谷歌地圖連接火力
- 27. 嘗試連接到藍牙時,應用程序崩潰android
- 28. 如果沒有網絡可用,如何阻止應用程序崩潰?
- 29. Android應用程序崩潰與Android 4.4.4
- 30. 網絡應用程序停止工作
沒有嘗試添加'嘗試...趕上'塊? –
打開妻子並沒有幫助..嘗試打開wifi ... – Vikram
yup.not resloved – ponduri