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

Wubi.exe cannot install Ubuntu on Win7 32bit laptop

1.      Get Ubuntu 32bit ISO, extract wubi.exe from the ISO file.
2.      Put wubi.exe and ISO at the same folder.
3.      Run wubi.exe, and it will not download amd64 source file and install on 32bit from the ISO file.
4.      If Win7 use dynamic storage setting, then you cannot install Ubuntu by wubi.exe

2012年3月7日

關於/proc/stat與CPU使用率的計算

http://colby.id.au/node/39
http://hi.baidu.com/%B5%DA%C6%DF%B9%FA%B6%C8/blog/item/8849e14b6a82e4f682025cc6.html
http://www.linuxhowtos.org/System/procstat.htm
http://blog.csdn.net/pppjob/article/details/4060336