2011年8月26日 星期五

想榨乾XOOM的電力嗎?試試看這個會隨時更新Sensor值的Widget

寄件者 scratchlab

Jimmy's papa想試試看在XOOM上,是否能隨時按Home鍵後回到主畫面後,就有個氣壓計小工具(Widget)告訴我目前的氣壓值,這工具可以教Jimmy看著氣壓值並且對照戶外的天氣,去了解兩者之間的關係,而且順便讓Jimmy回想一下上個月到科工館玩氣壓體驗艙當時的感覺

不過Jimmy's papa寫這個小工具時,竟然發覺氣壓值無法更新,一查之下android:updatePeriodMillis這個屬性竟然Android 1.5版後就沒效了(這是網友說的,有待查證),官方文件也提到"Updates requested with updatePeriodMillis will not be delivered more than once every 30 minutes."

解決之道有AlarmManager或TimerTask兩種方式解決,Jimmy's papa覺得TimerTask比較簡單,以下是程式碼

package com.jimmyscratchlab.androidbarometerwidget;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.text.format.DateFormat;
import android.util.Log;
import android.widget.RemoteViews;

public class AndroidBarometerWidget extends AppWidgetProvider {
    private static final String TAG = "sensor";
    private  SensorManager sm;
    private String value = "";
    
    @Override
    public void onUpdate(Context context,AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // TODO Auto-generated method stub
        sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        int sensorType = Sensor.TYPE_PRESSURE;
        sm.registerListener(myPressureListener,sm.getDefaultSensor(sensorType),SensorManager.SENSOR_DELAY_NORMAL);
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 500);
    }

    
    
     private class MyTime extends TimerTask
     {
         RemoteViews remoteViews;
         AppWidgetManager appWidgetManager;
         ComponentName thisWidget;
         
         public MyTime(Context context, AppWidgetManager appWidgetManager)
          {
             this.appWidgetManager = appWidgetManager;
             remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
             thisWidget = new ComponentName(context, AndroidBarometerWidget.class);
          }
          
         @Override
         public void run()
          {
             remoteViews.setTextViewText(R.id.now,  value);
             appWidgetManager.updateAppWidget(thisWidget, remoteViews);
          }
     }
    
    
    
    
    final SensorEventListener myPressureListener = new SensorEventListener(){

        public void onSensorChanged(SensorEvent sensorEvent){
            
            if(sensorEvent.sensor.getType() == Sensor.TYPE_PRESSURE){
                Log.i(TAG,"onSensorChanged");
                
                java.text.DecimalFormat df = new java.text.DecimalFormat("#.#");

                value = "目前氣壓(PRESSURE): "+ df.format( sensorEvent.values[0] )+"millibars"; 
            }
            
        }

        public void onAccuracyChanged(Sensor sensor , int accuracy){
            Log.i(TAG, "onAccuracyChanged");
        }
    };
     
}

沒有留言:

張貼留言