【问题标题】:Why does my Clock widget stop updating - using IntentFilter(Intent.ACTION_TIME_TICK)为什么我的时钟小部件停止更新 - 使用 IntentFilter(Intent.ACTION_TIME_TICK)
【发布时间】:2016-11-20 09:21:23
【问题描述】:

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

我尝试使用 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);
        }
    }
}

有什么建议吗?

【问题讨论】:

    标签: android android-widget clock intentfilter


    【解决方案1】:

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

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

    我建议您回到您的 AlarmManager 方法。

    【讨论】:

    • 嗯 - 我根本没有把它放在清单中......只是在 AppWidgetProvider 类中。
    • @DeNitE:我明白这一点。你打电话给registerReceiver()。这仅在您的进程运行时有效。您的进程不会永远运行,也不应该永远运行。对于许多广播,您可以在清单中注册它们,即使您没有正在运行的进程也可以接收它们。这对于某些广播是不可能的。 ACTION_TIME_TICK 就是这样一种广播。
    • 有趣 - 所以我猜大多数小部件只是使用 AlarmManager 来确保它继续更新?有没有办法每分钟将闹钟与系统时钟同步?
    • @DeNitE:“所以我猜大多数小部件只是使用 AlarmManager 来确保它继续更新?” - 我不知道。 “有没有办法让闹钟每分钟与系统时钟同步?” -- 欢迎您要求AlarmManager 在每分钟开始时让您控制,基于您用于构建事件时间的Calendar 对象以与set()setExact() 一起使用。或者,使用AlarmManager 只是为了确保您的进程正在运行,并使用ScheduledExecutorService 作为您每分钟一次的逻辑。也可能有其他模式;这些都是我想到的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-13
    相关资源
    最近更新 更多