搜尋此網誌

2012年3月19日

GPS on / off toggleing sample code

public void gpsOnOff(Boolean action, int retryFreq, int waitTime) {
                        boolean lastStateIsOn = false;
                        boolean curStateIsOn = false;
                        Thread thread = null;     
                       
                        gpsl = new LocationListener() {
                                @Override
                                public void onLocationChanged(Location arg0) {
                                        // TODO Auto-generated method stub                                 
                                }
                                @Override
                                public void onStatusChanged(String provider, int status,
                                                Bundle extras) {
                                        // TODO Auto-generated method stub                                 
                                }
                                @Override
                                public void onProviderDisabled(String provider) {
                                        // TODO Auto-generated method stub                                 
                                }
                                @Override
                                public void onProviderEnabled(String provider) {
                                        // TODO Auto-generated method stub                                 
                                }
                        };
                       
                        if (action == true)
                                sendUIMsg("Turn On GPS setting ... ");
                        else
                                sendUIMsg("Turn Off GPS setting ... ");

                        try {
                                Log.w(name, "GPSOnOff toggling");
                                locationManager = (LocationManager) context
                                                .getSystemService(Context.LOCATION_SERVICE);
                               
                                /*ClassLoader cl = null;
                                Class<?> secureClass = cl.loadClass("android.provider.Settings$Secure");
                                Method isMethod = secureClass.getMethod("isLocationProviderEnabled",
                                                ContentResolver.class, String.class);
                                Boolean ret = (Boolean) isMethod.invoke(secureClass, context.getContentResolver(), "gps");
                                Log.w(name, "GPSOnOff status: " + ret);*/
                               
                                /*String str = Settings.Secure.getString(context.getContentResolver(),
                                                Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
                                Log.v("GPS", str);
                                if (str != null) {
                                        Log.w(name, "GPSOnOff status: " + str.contains("gps"));
                                }
                                else{
                                        Log.w(name, "GPSOnOff status off");
                                }*/
                               
                                try {
                                        lastStateIsOn = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
                                        Log.w(name, "GPSOnOff lastStateIsOn: " + lastStateIsOn);
                                } catch (SecurityException e) {
                                        e.printStackTrace();
                                        Log.w(name, "GPS SecurityException: " + e.toString());
                                } catch (IllegalArgumentException e) {
                                        e.printStackTrace();
                                        Log.w(name, "GPS IllegalArgumentException: " + e.toString());
                                }                                              

                                if ((lastStateIsOn && action) || (!lastStateIsOn && !action)) {
                                        Log.w(name, "GPSOnOff same setting");
                                        if (curStateIsOn == true) {
                                                Looper.prepare();
                                                locationManager.requestLocationUpdates(
                                                                LocationManager.GPS_PROVIDER, 0, 0, gpsl);
                                                // Looper.loop();
                                                SystemClock.sleep(3000);
                                                locationManager.removeUpdates(gpsl);
                                        }
                                        return;
                                } else {
                                        Settings.Secure.setLocationProviderEnabled(
                                                        context.getContentResolver(),
                                                        LocationManager.GPS_PROVIDER, action);
                                        Log.w(name, "GPSOnOff setting changed");

                                        int retry = 0;
                                        do {
                                                curStateIsOn = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
                                                Log.w(name, "GPSOnOff curStateIsOn: " + curStateIsOn);

                                                if (lastStateIsOn != curStateIsOn)
                                                        break;

                                                SystemClock.sleep(waitTime);
                                                retry++;
                                        } while (retry < retryFreq);
                                       
                                        // curStateIsOn = true;
                                       
                                        if (curStateIsOn == true) {
                                                Looper.prepare();
                                                locationManager.requestLocationUpdates(
                                                                LocationManager.GPS_PROVIDER, 0, 0, gpsl);
                                                // Looper.loop();
                                                SystemClock.sleep(3000);
                                                locationManager.removeUpdates(gpsl);
                                        }
                                }
                        } catch (Exception e) {
                                e.printStackTrace();
                                Log.w(name, "Handler exception: " + e.toString());
                        }
                }      

沒有留言:

張貼留言