开发者

Lose All Connections and My Application Crashes Android

I have a weather application that is crashing if, while downloading weather data, the user goes out of range of all connectivity, the application hangs and then crashes. I have implemented a BroadcastReceiver within the MainActivity to detect when there is a connectivity change. I register it in onStart and unregister it in onStop. When this occurs I cancel all messages and try to display a dialog that informs the user that the connection has been lost. It appears that the receiver is not being called in time to cancel the handlers. Is there any way to force priority that the receiver be called upon?

I am not sure what code to include, but below is my code for handling the receiver:

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

    }

 };    

From here the message handler:

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;

And from within this, this method is called to detect connectivity state:

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

Any idea how I can stop my application from crashing and let the user know that they lost their connection?

My error logs are not consistent. Here are two separate reports:

  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)
开发者_Python百科

and another:

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)


Your first stack trace is because you are attempting to show a dialog from a BroadcastReceiver, near as I can tell.

Your second stack trace comes from your own code, where you are running past the end of your array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜