使用android服务更新小部件

时间:2013-06-03 18:25:37

标签: android android-widget android-service

我有一个简单的按钮小部件翻转一组图像。现在我想要而不是按钮点击,图像在一定时间间隔后翻转。为此,我需要服务类。任何人都可以帮助我如何在我的代码中包含它。我对服务不太了解。

这是我的代码段: //widgetprovider.java

public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds){

        //Log.i("Tag","In onUpdate method");
        System.out.println("Tag In onUpdate method");
        RemoteViews views=new RemoteViews(context.getPackageName(),R.layout.widget_layout);
        views.setOnClickPendingIntent(R.id.widget_button, buildButtonPendingIntent(context));
        Log.i("Tag","Context value after setOnClickPI before pushWIDGET:"+context);

        pushWidgetUpdate(context,views);
        Log.i("Tag","after pushwidget update:"+context);
    }

    public static PendingIntent buildButtonPendingIntent(Context context){

        Intent intent=new Intent();
        intent.setAction("com.example.android.intent.action.CHANGE_PICTURE");

        Log.i("Tag","intent's action:"+intent);
        return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

    public static void pushWidgetUpdate(Context context,RemoteViews views){

        System.out.println("Inside pushwidget");
        ComponentName myWidget=new ComponentName(context, MyWidgetProvider.class);
        AppWidgetManager manager=AppWidgetManager.getInstance(context);
        manager.updateAppWidget(myWidget, views);
    }

// IntentReceiver.java

public void onReceive(Context context, Intent intent) {

        // Log.i("Tag1","In onReceive:"+intent);
        System.out.println("In onReceive()");

        if (intent.getAction().equals(
                "com.example.android.intent.action.CHANGE_PICTURE")) {
            updateWidgetPictureAndButtonListener(context);
        }
    }

    private void updateWidgetPictureAndButtonListener(Context context) {

        RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.widget_layout);
        views.setImageViewResource(R.id.widget_image, getImageToSet());

        System.out.println("in updateWidgetPicture method");
        // Log.i("Tag","in updaeWIdgetPicture method");
        // remember to set ur button click listeners
        views.setOnClickPendingIntent(R.id.widget_button,
                MyWidgetProvider.buildButtonPendingIntent(context));

        MyWidgetProvider.pushWidgetUpdate(context.getApplicationContext(),
                views);
    }

//编辑的java文件

package com.example.pictureappservice;

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


// this works without service
public class MyWidgetProvider extends AppWidgetProvider {

    private static int clickCount = 0;
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds){

        Log.i("onUpdate method call","Called");
        ComponentName thisWidget=new ComponentName(context, MyWidgetProvider.class);
        int []allWidgetIds=appWidgetManager.getAppWidgetIds(thisWidget);

        //built intent to call service
        Intent intent=new Intent(context.getApplicationContext(),UpdateService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,allWidgetIds);

        Log.i("LOG","before service");
        //update widget via service
        context.startService(new Intent(context,UpdateService.class));
    }

    public static class UpdateService extends Service{

        public void onStart(Intent intent, int startId){

            AppWidgetManager appWidgetManager=AppWidgetManager.getInstance(this.getApplicationContext());
            int []allWidgetIds=intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);

            ComponentName thisWidget=new ComponentName(getApplicationContext(),MyWidgetProvider.class);
            int []allWidgetIds2=appWidgetManager.getAppWidgetIds(thisWidget);

            Log.i("LOG","From Intent" + String.valueOf(allWidgetIds.length));
            Log.i("LOG","Direct" + String.valueOf(allWidgetIds2.length));

            for(int widgetId:allWidgetIds){

                //  logic 
                Log.i("LOG","intent's action"+intent.getAction());
                //if (intent.getAction().equals("com.example.android.intent.action.CHANGE_PICTURE")) {

                    RemoteViews views=new RemoteViews(this.getApplicationContext().getPackageName(),R.layout.widget_layout);    
                    views.setImageViewResource(R.id.widget_image, getImageToSet());

                    //intent
                    Intent clickIntent=new Intent(this.getApplicationContext(),MyWidgetProvider.class);
                    clickIntent.setAction("AppWidgetManager.ACTION_APPWIDGET_UPDATE");
                    //clickIntent.setAction("com.example.android.intent.action.CHANGE_PICTURE");
                    clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

                    Log.i("Tag","intent's action:"+intent);

                    PendingIntent pendingIntent=PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                    views.setOnClickPendingIntent(R.id.widget_image, pendingIntent);
                    appWidgetManager.updateAppWidget(widgetId,views);
                //}
            }
            stopSelf();
            super.onStart(intent, startId);
        }

        private int getImageToSet() {
            clickCount++;
            Log.i("Tag", "in getImageToSet()" + clickCount);

            return clickCount % 2 == 0 ? R.drawable.paypal_logo : R.drawable.paypaldonation;
        }
        @Override
        public IBinder onBind(Intent intent) {
            // We don't need to bind to this service
            return null;
        }
    }

    public static void pushWidgetUpdate(Context context,RemoteViews views){

        System.out.println("Inside pushwidget");
        ComponentName myWidget=new ComponentName(context, MyWidgetProvider.class);
        AppWidgetManager manager=AppWidgetManager.getInstance(context);
        manager.updateAppWidget(myWidget, views);
    }
}

1 个答案:

答案 0 :(得分:3)

如果您的“特定时间间隔”为15分钟或更长时间,请在您的应用小部件元数据中使用android:updatePeriodMillis,这会触发onUpdate()AppWidgetProvider方法的调用。

或者,使用AlarmManager触发PendingIntent,该{{1}}将调用您的某些代码,这些代码将在其他频率上更新您的应用小部件。

但是,我建议不要每分钟更新一次app小部件(设备处于睡眠状态时更长时间),以降低功耗。