4

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

这是我的代码片段://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);
    }
}
4

1 回答 1

3

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

或者,用于AlarmManager触发PendingIntent将调用您的某些代码的代码,这些代码将以其他频率更新您的应用小部件。

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

于 2013-06-03T19:47:01.593 回答