2012-11-27 4 views
1

위젯에 CountDownTimer를 구현하고 싶습니다.AppWidgetProvider에서 CountDownTimer 구현

import android.content.Intent; 
import android.os.CountDownTimer; 
import android.widget.RemoteViews; 
import android.widget.TextView; 

public class CountDown extends CountDownTimer { 
    private RemoteViews views; 

    public CountDown(long millisInFuture, int countDownInterval, RemoteViews views) { 
     super(millisInFuture, countDownInterval); 

     this.views = views; 
    } 

    @Override 
    public void onFinish() { 
    } 

    @Override 
    public void onTick(long millisUntilFinished) { 
     int days = (int) (millisUntilFinished/(1000 * 60 * 60 * 24)); 

      views.setTextViewText(R.id.countWidget, days); 
    } 
} 

그리고의 AppWidgetProvider 클래스

import java.util.Calendar; 
import java.util.Date; 
import java.util.TimeZone; 

import android.app.PendingIntent; 
import android.appwidget.AppWidgetManager; 
import android.appwidget.AppWidgetProvider; 
import android.content.Context; 
import android.content.Intent; 
import android.widget.RemoteViews; 
import android.widget.TextView; 

public class CountDownWidgetProvider extends AppWidgetProvider { 
    private Calendar currentDate = Calendar.getInstance(); 
    private Calendar firstDecember = Calendar.getInstance(); 

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     Date date = new Date(); 

     this.firstDecember.setTimeZone(TimeZone.getTimeZone("Europe/Berlin")); 
     this.currentDate.setTimeZone(TimeZone.getTimeZone("Europe/Berlin")); 
     this.firstDecember.set(2012, 11, 1, 0, 0, 0); // TODO: Set to December 
     this.currentDate.setTime(date); 

     final int N = appWidgetIds.length; 

     // Perform this loop procedure for each App Widget that belongs to this provider 
     for (int i=0; i<N; i++) { 
      int appWidgetId = appWidgetIds[i]; 

      // Create an Intent to launch MainActivity 
      Intent intent = new Intent(context, MainActivity.class); 
      PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 

      // Get the layout for the App Widget and attach an on-click listener 
      // to the button 
      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.count_down_app_widget); 
      views.setOnClickPendingIntent(R.id.countWidget, pendingIntent); 

      long millis = this.firstDecember.getTimeInMillis() - this.currentDate.getTimeInMillis(); 

      CountDown countDown = new CountDown(millis, 1000, views); 
      countDown.start(); 

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

그러나 그것은 그런 식으로 작동하지 않습니다 : 내가 생각 해낸 것은. 위젯 TextView는 새로 고치거나 변경하지 않습니다. 카운트 다운이 시작되지 않는 것 같습니다.

아마 어디 선가 실수를하지만 안드로이드 개발에 새로운, 그래서 나는 당신이 나를 도울 수 있다고 생각.

답변

1
you have to update widget after setting the text 
use 
appWidgetManager.updateAppWidget(appWidgetId, views); 


after 
    views.setTextViewText(R.id.countWidget, days); 
관련 문제