2011-11-27 48 views
2

我剛開始學習Android,我正在嘗試編寫一個小部件,它每隔幾秒更新一次計數器,但是某處丟失了某些東西。我在屏幕上獲得1。我正在使用Android 4模擬器。x時間後的Android小部件更新

package com.ibluekey; 

import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.Locale; 
import com.ibluekey.R; 

import android.app.PendingIntent; 
import android.app.Service; 
import android.appwidget.AppWidgetManager; 
import android.appwidget.AppWidgetProvider; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Handler; 
import android.os.IBinder; 
import android.os.SystemClock; 
import android.util.Log; 
import android.widget.RemoteViews; 
import android.widget.Toast; 

public class WidgetActivity extends AppWidgetProvider { 
    public static WidgetActivity Widgetke = null; 
    public static Context context; 
    public static AppWidgetManager appWidgetManager; 
    public static int appWidgetIds[]; 
    private Handler mHandler = new Handler(); 
    private static int count=0; 
    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {  
     if (null == context) context = WidgetActivity.context; 
     if (null == appWidgetManager) appWidgetManager = WidgetActivity.appWidgetManager; 
     if (null == appWidgetIds) appWidgetIds = WidgetActivity.appWidgetIds; 

     WidgetActivity.Widgetke = this; 
     WidgetActivity.context = context; 
     WidgetActivity.appWidgetManager = appWidgetManager; 
     WidgetActivity.appWidgetIds = appWidgetIds; 

     Log.d("PXR", "onUpdate"); 

     final int N = appWidgetIds.length; 
     for (int i=0; i<N; i++) { 
      int appWidgetId = appWidgetIds[i]; 

      updateAppWidget(context,appWidgetManager, appWidgetId);    
     } 

    } 


    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, 
      int appWidgetId) { 

     count++; 

     /*Intent intent = new Intent(context, UpdateService.class); 
     PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0); 

     RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget); 
     remoteViews.setOnClickPendingIntent(R.id.mainlayout, pendingIntent); 

     remoteViews.setTextViewText(R.id.test_out, ""+count); 

     // Tell the widget manager 
     appWidgetManager.updateAppWidget(appWidgetId, remoteViews);*/ 

     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget); 
     views.setTextViewText(R.id.test_out, count+""); 
     appWidgetManager.updateAppWidget(appWidgetId, views); 
    } 



    public static class UpdateService extends Service { 
     @Override 
     public void onStart(Intent intent, int startId) { 
      //WidgetActivity.Widgetke.onUpdate(context, appWidgetManager, appWidgetIds); 
      //Toast.makeText(context, "Update Widget", Toast.LENGTH_SHORT).show(); 

      WidgetActivity.Widgetke.mHandler.removeCallbacks(WidgetActivity.Widgetke.mUpdateTimeTask); 
      WidgetActivity.Widgetke.mHandler.postDelayed(WidgetActivity.Widgetke.mUpdateTimeTask, 100); 
     } 

     @Override 
     public IBinder onBind(Intent arg0) { 
      return null; 
     } 
    } 

    private Runnable mUpdateTimeTask = new Runnable() { 
      public void run() {   

       WidgetActivity.Widgetke.onUpdate(context, appWidgetManager, appWidgetIds);    
       WidgetActivity.Widgetke.mHandler.postAtTime(this, 1000); 

      } 
     }; 
} 

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.ibluekey" 
     android:versionCode="1" 
     android:versionName="1.0"> 


    <uses-sdk android:minSdkVersion="4" /> 

    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"> 
     <receiver android:name=".Reciever" android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="MyTestIntent" /> 
      </intent-filter> 
     </receiver> 
     <receiver android:name="com.ibluekey.WidgetActivity" android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
      </intent-filter> 
      <meta-data     
       android:name="android.appwidget.provider" 
       android:resource="@xml/widgetprovider" /> 
     </receiver> 

     <service android:name="com.ibluekey.WidgetActivity$UpdateService" /> 
    </application> 

</manifest> 

回答

2

這是一個很棒的教程系列,用於在Android中創建小部件並定期更新它們。它從這裏開始: http://android-er.blogspot.com/2010/10/simple-home-screen-app-widget-with.html

而且具有周期性定時器/報警到此爲止: http://android-er.blogspot.com/2010/10/app-widget-using-alarm-manager.html

基本上你需要設置在清單中有些東西聽的廣播意圖,其更新微件。它還顯示瞭如何爲小部件創建配置活動。

要創建警報或週期性事件,您還可以使用AlarmManager類。

1

appWidgetManager.updateAppWidget(appWidgetId, views); 

添加此

super.onUpdate(context, appWidgetManager, appWidgetId); 

我相信,這將解決您的問題