0

In Android, i am using Timer class to check wifi connection in mobile every second, because i want to Turn On GPS, when device is not using wifi. But it is showing me fatal Timer-0 RunTimeException.

Here is my timer function

public void checkWifi()
{
    Timer timer = new Timer();
    TimerTask hourlyTask = new TimerTask() {
        @Override
        public void run()
        {
            boolean isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                             .isConnectedOrConnecting();
            if (!isWifi) {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
            }
        }
    };
    timer.schedule(hourlyTask, 0l, 1000);
}

Here is my LogCat

  02-14 02:52:53.126: E/AndroidRuntime(1539): FATAL EXCEPTION: Timer-0
  02-14 02:52:53.126: E/AndroidRuntime(1539): Process: com.example.gpsco11wifialert, PID: 1539
  02-14 02:52:53.126: E/AndroidRuntime(1539): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
  02-14 02:52:53.126: E/AndroidRuntime(1539):   at android.os.Handler.<init>(Handler.java:200)
  02-14 02:52:53.126: E/AndroidRuntime(1539):   at android.os.Handler.<init>(Handler.java:114)
  02-14 02:52:53.126: E/AndroidRuntime(1539):   at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:221)
  02-14 02:52:53.126: E/AndroidRuntime(1539):   at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:221)
  02-14 02:52:53.126: E/AndroidRuntime(1539):   at android.location.LocationManager.wrapListener(LocationManager.java:844)
  02-14 02:52:53.126: E/AndroidRuntime(1539):   at android.location.LocationManager.requestLocationUpdates(LocationManager.java:857)
  02-14 02:52:53.126: E/AndroidRuntime(1539):   at android.location.LocationManager.requestLocationUpdates(LocationManager.java:454)
  02-14 02:52:53.126: E/AndroidRuntime(1539):   at com.example.gpsco11wifialert.GpsC011Wifi$MyLocationListener$4.run(GpsC011Wifi.java:213)
  02-14 02:52:53.126: E/AndroidRuntime(1539):   at java.util.Timer$TimerImpl.run(Timer.java:284)
pat
  • 12,587
  • 1
  • 23
  • 52
Anand K
  • 233
  • 1
  • 6
  • 21

2 Answers2

1

As the error says in logcat you're calling it from a worker thread, Use Handler's or AsyncTask to do it.

But by looking at your requirement i suggest you to go with BradcastReceiver that listens for wifi state.

For Broadcastreceiver: BroadcastReceiver when wifi or 3g network state changed

For handler:Android + FATAL EXCEPTION: Timer-0 (Removing ImageView using timer)

Community
  • 1
  • 1
Vishal Santharam
  • 1,963
  • 1
  • 16
  • 30
0

I know this is not feasible at all but still you can try this to avoid this error. Or if you don't want to use this then you can try Asynctask class that will solve your problem.

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

This will remove your error.

InnocentKiller
  • 5,234
  • 7
  • 36
  • 84