2012-10-29 67 views
2

爲什麼我的Android時間小部件不會自行更新? 我創建了一個時間&主屏幕的日期小部件,但問題是,我的時間小部件只會更新,如果屏幕方向改變,例如從風景到人像..它應該在實時模式下自動更新..這是我的代碼:爲什麼我的Android時間小部件不會更新?

public class PointlessWidget extends AppWidgetProvider { 
    Time mCalendar; 
    float mMinutes; 
    float mHour; 

    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
      int[] appWidgetIds) { 
     // TODO Auto-generated method stub 
     super.onUpdate(context, appWidgetManager, appWidgetIds); 

     mCalendar = new Time(); 

     // Line below sets time that will be displayed - modify if needed. 
     mCalendar.setToNow(); 


     int hour = mCalendar.hour; 
     int minute = mCalendar.minute; 
     int second = mCalendar.second; 

     mMinutes = minute + second/60.0f; 
     mHour = hour + minute/60.0f; 

     final int N = appWidgetIds.length; 

     for (int i = 0; i < N; i++) { 
      int awID = appWidgetIds[i]; 
      RemoteViews v = new RemoteViews(context.getPackageName(), 
        R.layout.widget); 

      v.setTextViewText(R.id.textHour, " " + hour); 
      v.setTextViewText(R.id.textMinute, " " + minute); 

      appWidgetManager.updateAppWidget(awID, v); 
     } 
    } 

    @Override 
    public void onDeleted(Context context, int[] appWidgetIds) { 
    // TODO Auto-generated method stub 
    super.onDeleted(context, appWidgetIds); 

    Toast.makeText(context, "Widget Deleted!", Toast.LENGTH_SHORT).show(); 
    } 

} 

這裏是我的清單:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.androidwidgets" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="11" 
     android:targetSdkVersion="15" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 

     <receiver android:name=".PointlessWidget" > 
      <intent-filter> 
       <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
      </intent-filter> 

      <meta-data 
       android:name="android.appwidget.provider" 
       android:resource="@xml/widget_stuff" /> 
     </receiver> 
     <service android:name="TimeSettableAnalogClockService"/> 

     <activity android:name=".WidgetsConfig" > 
      <intent-filter> 
       <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

,這是我widget_stuff XML。我已經在轉換中將updatePeriodMillis =「1000」或等效設置爲1秒。

<?xml version="1.0" encoding="utf-8"?> 
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
    android:initialLayout="@layout/widget" 
    android:minHeight="30dp" 
    android:minWidth="274dp" 
    android:updatePeriodMillis="1000" > 

</appwidget-provider> 
+0

檢查該解決方案 http://stackoverflow.com/questions/2078122/android-widget-not-updating 我想你應該等待30分鐘 –

回答

1

我已經在我的問題想出解決方案。併爲他人着想同樣的問題作爲我的我會分享我的代碼:

import java.text.SimpleDateFormat; 
import java.util.Calendar; 

import android.os.Bundle; 
import android.os.Handler; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.app.Activity; 

public class DigitalClock extends Activity { 
    private Runnable updateTimeTask; 
    private Handler handler; 

    public static final int TIME_UPDATE_INTERVAL_MSEC = 1000; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     if (updateTimeTask == null) { 
      updateTimeTask = new Runnable() { 
       @Override 
       public void run() { 
        // if (!DigitalClock.this.isActive()) return; 

        timeAndDate(); 
        handler.postDelayed(this, TIME_UPDATE_INTERVAL_MSEC); 
       } 
      }; 
     } 
     if (handler == null) { 
      handler = new Handler(); 
     } 
     handler.removeCallbacks(updateTimeTask); 
     handler.postDelayed(updateTimeTask, TIME_UPDATE_INTERVAL_MSEC); 
    } 

    public void timeAndDate() { 
     Calendar c = Calendar.getInstance(); 

     SimpleDateFormat time = new SimpleDateFormat("hh:mm:ss"); 
     SimpleDateFormat date = new SimpleDateFormat("MMMM dd yyyy"); 
     SimpleDateFormat day = new SimpleDateFormat("EEEE"); 
     SimpleDateFormat am_pm = new SimpleDateFormat("aa"); 
     String sTime = time.format(c.getTime()); 
     String sDate = date.format(c.getTime()); 
     String sDay = day.format(c.getTime()); 
     String sAm_Pm = am_pm.format(c.getTime()); 

     // formattedDate have current date/time 
     Toast.makeText(this, sDate + " | " + sTime, Toast.LENGTH_SHORT).show(); 

     // Now we display value in TextView 
     TextView txtTime = (TextView) findViewById(R.id.txtHour); 
     TextView txtDay = (TextView) findViewById(R.id.txtDay); 
     TextView txtDate = (TextView) findViewById(R.id.txtDate); 
     TextView txtAm_Pm = (TextView) findViewById(R.id.txtAm_Pm); 

     txtTime.setText(sTime); 
     txtDate.setText(sDate); 
     txtDay.setText(sDay); 
     txtAm_Pm.setText(sAm_Pm); 
    } 
} 
1

要更新您的時間部件,請使用AlarmManager類。

你可以使用後臺服務來更新你的時間部件。

+0

感謝您的想法.. –

相關問題