为什么我的Clock小部件停止更新 - 使用IntentFilter(Intent.ACTION_TIME_TICK)

时间:2016-07-17 19:52:03

标签: android android-widget clock intentfilter

我正在尝试创建一个每分钟更新一次的时钟小部件。

我尝试使用AlarmManager发送更新请求,但我认为这不如使用IntentFilter(Intent.ACTION_TIME_TICK)

那么准确

小部件加载完美,并使用ACTION_TIME_TICK IntentFilter更新约10分钟左右......但随后只是停止更新。

这是我的AppWidgetProvider类

public class ClockWidget extends AppWidgetProvider
{
    private final String TAG = "ClockWidget";

    private static SharedPreferences sharedPrefs;
    private static SharedPreferences.Editor prefEditor;
    private final String SHARED_PREF_NAME = "ClassicBlack_Prefs";

    private BroadcastReceiver receiver;

    @Override
    public void onEnabled(Context context)
    {
        Log.d(TAG, "onEnabled()");

        super.onEnabled(context);
        sharedPrefs = context.getSharedPreferences(SHARED_PREF_NAME, 0);
        prefEditor = sharedPrefs.edit();
        prefEditor.putBoolean("isWatchfaceActive", true).commit();

        drawWatch(context);

        receiver = new BroadcastReceiver()
        {
            @Override
            public void onReceive(Context ctx, Intent intent)
            {
                if (intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0)
                {
                    drawWatch(ctx);
                }
            }
        };

        context.getApplicationContext().registerReceiver(receiver, new IntentFilter(Intent.ACTION_TIME_TICK));
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
    {
        Log.d(TAG, "onUpdate()");
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

    @Override
    public void onDisabled(Context context)
    {
        Log.d(TAG, "onDisabled()");

        super.onDisabled(context);
        sharedPrefs = context.getSharedPreferences(SHARED_PREF_NAME, 0);
        prefEditor = sharedPrefs.edit();
        prefEditor.putBoolean("isWatchfaceActive", false).commit();

        if (receiver != null)
        {
            context.getApplicationContext().unregisterReceiver(receiver);
        }
    }

    private void drawWatch(Context context)
    {
        Log.d(TAG, "drawWatch()");

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());
        ComponentName thisWidget = new ComponentName(context, ClockWidget.class);

        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

        Log.i("ExampleWidget", "Updating widgets " + Arrays.asList(allWidgetIds));

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i = 0; i < allWidgetIds.length; i++)
        {
            DrawWatchFace drawWatchFace = new DrawWatchFace(context, null, true, 500);
            drawWatchFace.createWatchWidget();

            // Get the layout for the App Widget and attach an on-click listener to the button
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.clock_widget_layout);
            views.setImageViewBitmap(R.id.widget_canvas_imageView_small, drawWatchFace.getPalletBitmap());

            // Tell the AppWidgetManager to perform an update on the current app widget
            appWidgetManager.updateAppWidget(allWidgetIds[i], views);
        }
    }
}

有什么建议吗?

1 个答案:

答案 0 :(得分:3)

您的流程正在终止。这是完全正常的。当您的进程终止时,您的接收器将被销毁,您将不再接收广播。

您无法在清单中注册ACTION_TIME_TICK,这是处理此问题的典型方式。

我建议您回到AlarmManager方法。