x小时后更新Android小部件

时间:2011-11-27 18:15:18

标签: android android-layout android-widget

我刚刚开始学习Android,我正在尝试编写一个小部件,它会在每一秒钟内更新计数器,但在某个地方它缺少一些东西。我在屏幕上得到1。我正在使用Android 4模拟器。

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 个答案:

答案 0 :(得分: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 :(得分:1)

appWidgetManager.updateAppWidget(appWidgetId, views);

添加此

super.onUpdate(context, appWidgetManager, appWidgetId);

我相信这会解决你的问题