gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 08:33:41 33 4
gpt4 key购买 nike

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

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

有什么建议吗?

最佳答案

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

您不能在 list 中注册 ACTION_TIME_TICK,这是典型的处理方式。

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

关于android - 为什么我的时钟小部件停止更新 - 使用 IntentFilter(Intent.ACTION_TIME_TICK),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38425348/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com