2016-07-17 22 views
2

毎分1回更新する時計ウィジェットを作成しようとしています。IntentFilter(Intent.ACTION_TIME_TICK)を使用して時計ウィジェットが更新を停止する理由

私は、更新要求を送信するために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); 
     } 
    } 
} 

ですか?

答えて

3

処理が終了しています。これは完全に正常です。プロセスが終了すると、受信者は破棄され、ブロードキャストは受信されなくなります。

マニフェストにACTION_TIME_TICKを登録することはできませんが、それはこれを処理する典型的な方法です。

AlarmManagerというアプローチに戻すことをおすすめします。

+0

Hmm - AppWidgetProviderクラスのマニフェストには入れていません。 – DeNitE

+0

@DeNitE:それは分かります。あなたは 'registerReceiver()'を呼び出しています。これは、プロセスの実行中にのみ機能します。あなたのプロセスは永遠に実行されることはありません。多くのブロードキャストでは、マニフェストに登録し、実行中のプロセスがない場合でも受信できます。これは一部のブロードキャストでは不可能です。 'ACTION_TIME_TICK'はそのような放送です。 – CommonsWare

+0

興味深い - ほとんどのウィジェットはAlarmManagerを使用して更新を継続していると思いますか?システムクロックの分とアラームを同期させる方法はありますか? – DeNitE

関連する問題