2
매분마다 한 번 업데이트되는 시계 위젯을 만들려고합니다.내 시계 위젯이 IntentFilter (Intent.ACTION_TIME_TICK)를 사용하여 업데이트되지 않는 이유
나는 업데이트 요청을 보내도록 알람 관리기를 사용했지만, 나는 약 10 분 동안 IntentFilter(Intent.ACTION_TIME_TICK)
완벽하게 위젯로드 및 업데이트를 사용하거나 그렇게 ACTION_TIME_TICK로 IntentFilter를 사용하는 것만 큼 정확하지 않을 것이라고 생각 ... 그런 다음 업데이트가 중지됩니다.
여기 내의 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);
}
}
}
어떤 제안이있어?
흠 - 나는 AppWidgetProvider 클래스의 manifest에 전혀 넣지 않았습니다. – DeNitE
@DeNitE : 이해합니다. 당신은'registerReceiver()'를 호출하고있다. 이는 프로세스가 실행 중일 때만 작동합니다. 귀하의 프로세스는 영원히 실행되지 않을 것이며 그렇게해서도 안됩니다. 많은 브로드 캐스트의 경우 매니페스트에 등록 할 수 있으며 실행중인 프로세스가없는 경우에도 수신 할 수 있습니다. 일부 방송에서는 불가능합니다. 'ACTION_TIME_TICK'은 그러한 방송 중 하나입니다. – CommonsWare
흥미 롭다 - 그래서 대부분의 위젯은 AlarmManager를 사용하여 업데이트를 계속 진행할 것이라고 생각한다. 알람을 시스템 클럭 시간과 동기화하는 방법이 있습니까? – DeNitE