如果在下载天气数据时用户超出所有连接范围,应用程序会挂起然后崩溃,我的天气应用程序会崩溃。我在MainActivity中实现了BroadcastReceiver,以检测何时发生连接更改。我在onStart中注册它并在onStop中注销它。发生这种情况时,我取消所有消息并尝试显示一个对话框,通知用户连接已丢失。似乎没有及时调用接收器来取消处理程序。有没有办法强制接收器被调用的优先级?
我不确定要包含哪些代码,但下面是我处理接收器的代码:
public BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("#########", "mReciver onReceive MA");
if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
Log.d("#####", "connectivity action MA mReceiver");
if (mHandler != null) {
mHandler.removeMessages(ACCUWX.Messages.DATA_LOADED_FROM_DATABASE);
mHandler.removeMessages(ACCUWX.Messages.DATAFEED_DOWNLOAD_COMPLETE);
mHandler.removeMessages(ACCUWX.Messages.DATAFEED_DOWNLOAD_FAILED);
mHandler.removeMessages(ACCUWX.Messages.DATAFEED_DOWNLOAD_FROM_GPS);
mHandler.removeMessages(ACCUWX.Messages.GPS_TIMEOUT_OCCURRED);
mHandler.removeMessages(ACCUWX.Messages.LOCATION_CODE_FAILED_FROM_COORDS);
mHandler.removeMessages(ACCUWX.Messages.LOCATION_CODE_FOUND_FROM_COORDS);
mHandler.removeMessages(ACCUWX.Messages.LOCATION_SEARCH_COMPLETE);
mHandler.removeMessages(ACCUWX.Messages.LOCATION_SEARCH_FAILED);
mHandler.sendEmptyMessage(ACCUWX.Messages.CONNECTIVITY_CHANGE);
}
}
}
};
从这里开始消息处理程序:
case ACCUWX.Messages.CONNECTIVITY_CHANGE:
Log.d("!#!#@!#$!@$!@#$@!#$@!", "CONNECTIVITY CHANGE MSG");
if (!isNetworkReachable()){
Log.d("@!$#@$#!@$@!$#", "network not reachable in CONNECTIVITY CHANGE MA MSG");
if (mLocationSearchDialog != null) mLocationSearchDialog.dismiss();
if (mProgressDialog != null) mProgressDialog.dismiss();
showConnectivityAlertDialog();
}
break;
在此范围内,调用此方法来检测连接状态:
private boolean isNetworkReachable() {
Log.d("#########", "isNetworkReachable MA");
boolean airplaneMode = false;
boolean mobileConnected;
boolean wifiConnected;
NetworkInfo wifi, mobile;
boolean result = false;
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
try {
int airplaneValue = Settings.System.getInt(this.getContentResolver(), Settings.System.AIRPLANE_MODE_ON);
airplaneMode = (airplaneValue != 0);
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
mobileConnected = mobile.isConnected();
wifiConnected = wifi.isConnected();
Log.d("##########", "airplaneMode in MA " + airplaneMode);
Log.d("##########", "mobileConnected in MA " + mobileConnected);
Log.d("##########", "wifiConnected in MA " + wifiConnected);
if (airplaneMode)
result = false;
else {
if (mobileConnected || wifiConnected)
result = true;
}
return result;
}
知道如何阻止我的应用程序崩溃并让用户知道他们丢失了连接吗?
我的错误日志不一致。以下是两份单独的报告:
02-04 08:01:02.889: ERROR/AndroidRuntime(6565): FATAL EXCEPTION: main
02-04 08:01:02.889: ERROR/AndroidRuntime(6565): java.lang.RuntimeException: Unable to start receiver com.accuweather.android.simpleweatherlite.dellstreak.widget.WidgetProvider: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
02-04 08:01:02.889: ERROR/AndroidRuntime(6565): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2821)
02-04 08:01:02.889: ERROR/AndroidRuntime(6565): at android.app.ActivityThread.access$3200(ActivityThread.java:125)
02-04 08:01:02.889: ERROR/AndroidRuntime(6565): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
02-04 08:01:02.889: ERROR/AndroidRuntime(6565): at android.os.Handler.dispatchMessage(Handler.java:99)
02-04 08:01:02.889: ERROR/AndroidRuntime(6565): at android.os.Looper.loop(Looper.java:123)
02-04 08:01:02.889: ERROR/AndroidRuntime(6565): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-04 08:01:02.889: ERROR/AndroidRuntime(6565): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 08:01:02.889: ERROR/AndroidRuntime(6565): at java.lang.reflect.Method.invoke(Method.java:521)
02-04 08:01:02.889: ERROR/AndroidRuntime(6565): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-04 08:01:02.889: ERROR/AndroidRuntime(6565): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-04 08:01:02.889: ERROR/AndroidRuntime(6565): at dalvik.system.NativeStart.main(Native Method)
02-04 08:01:02.889: ERROR/AndroidRuntime(6565): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
02-04 08:01:02.889: ERROR/AndroidRuntime(6565): at android.view.ViewRoot.setView(ViewRoot.java:509)
02-04 08:01:02.889: ERROR/AndroidRuntime(6565): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
02-04 08:01:02.889: ERROR/AndroidRuntime(6565): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
02-04 08:01:02.889: ERROR/AndroidRuntime(6565): at android.app.Dialog.show(Dialog.java:241)
02-04 08:01:02.889: ERROR/AndroidRuntime(6565): at com.accuweather.android.simpleweatherlite.dellstreak.widget.WidgetProvider.showConnectivityAlertDialog(WidgetProvider.java:127)
02-04 08:01:02.889: ERROR/AndroidRuntime(6565): at com.accuweather.android.simpleweatherlite.dellstreak.widget.WidgetProvider.onReceive(WidgetProvider.java:174)
02-04 08:01:02.889: ERROR/AndroidRuntime(6565): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)
和另一个:
02-04 07:40:07.330: ERROR/AndroidRuntime(6316): FATAL EXCEPTION: main
02-04 07:40:07.330: ERROR/AndroidRuntime(6316): java.lang.ArrayIndexOutOfBoundsException
02-04 07:40:07.330: ERROR/AndroidRuntime(6316): at com.accuweather.android.simpleweatherlite.dellstreak.MainActivity$10.handleMessage(MainActivity.java:2067)
02-04 07:40:07.330: ERROR/AndroidRuntime(6316): at android.os.Handler.dispatchMessage(Handler.java:99)
02-04 07:40:07.330: ERROR/AndroidRuntime(6316): at android.os.Looper.loop(Looper.java:123)
02-04 07:40:07.330: ERROR/AndroidRuntime(6316): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-04 07:40:07.330: ERROR/AndroidRuntime(6316): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 07:40:07.330: ERROR/AndroidRuntime(6316): at java.lang.reflect.Method.invoke(Method.java:521)
02-04 07:40:07.330: ERROR/AndroidRuntime(6316): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-04 07:40:07.330: ERROR/AndroidRuntime(6316): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-04 07:40:07.330: ERROR/AndroidRuntime(6316): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您的第一个堆栈跟踪是因为您试图在BroadcastReceiver
附近显示一个对话框,我可以告诉您。
您的第二个堆栈跟踪来自您自己的代码,您正在运行数组的末尾。