2017-06-13 20 views
-2

私はデジタル時計ウィジェットを実装しようとしています。私はその時間に自分のホーンの画面に時計のウィジェットをドラッグしているときにホーム画面上のスペースを取るが、システムクロックが時間を自分の時間を更新するとき、私のウィジェットの時計はウィジェットとして表示されます。モバイル時計の時間は2:10で、ウィジェットは画面上にドラッグされ、システム時計がホーム画面に表示されたときに私のウィジェットが2時11分に表示されます。ここに私の時計ウィジェットのコードです。Android Widgetデジタル時計

Clockwidget.java

public class ClockWidget extends AppWidgetProvider { 

@Override 
public void onDisabled(Context context) { 
    super.onDisabled(context); 
    context.stopService(new Intent(context, UpdateService.class)); 
} 


@Override 
public void onEnabled(Context context) { 
    super.onEnabled(context); 
    // context.startService(new Intent(UpdateService.ACTION_UPDATE)); 
    context.startService(new Intent(context, UpdateService.class)); 


} 


@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
    super.onUpdate(context, appWidgetManager, appWidgetIds); 
    // context.startService(new Intent(UpdateService.ACTION_UPDATE)); 
    context.startService(new Intent(context, UpdateService.class)); 

} 


public static final class UpdateService extends Service { 

    static final String ACTION_UPDATE = "com.example.kevan.mywidget.action.UPDATE"; 

    private final static IntentFilter sIntentFilter; 

    private final static String FORMAT_12_HOURS = "h:mm"; 
    private final static String FORMAT_24_HOURS = "kk:mm"; 

    private String mTimeFormat; 
    private String mDateFormat; 
    private Calendar mCalendar; 
    private String mAM, mPM; 

    static { 
     sIntentFilter = new IntentFilter(); 
     sIntentFilter.addAction(Intent.ACTION_TIME_TICK); 
     sIntentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 
     sIntentFilter.addAction(Intent.ACTION_TIME_CHANGED); 
    } 


    @Override 
    public void onCreate() { 
     super.onCreate(); 
     reinit(); 
     registerReceiver(mTimeChangedReceiver, sIntentFilter); 

    } 


    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     unregisterReceiver(mTimeChangedReceiver); 
    } 


    @Override 
    public void onStart(Intent intent, int startId) { 
     super.onStart(intent, startId); 

     if (ACTION_UPDATE.equals(intent.getAction())) { 
      update(); 
     } 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 


    private void update() { 
     mCalendar.setTimeInMillis(System.currentTimeMillis()); 
     final CharSequence time = DateFormat.format(mTimeFormat, mCalendar); 
     final CharSequence date = DateFormat.format(mDateFormat, mCalendar); 

     RemoteViews views = new RemoteViews(getPackageName(), R.layout.main); 
     views.setTextViewText(R.id.Time, time); 
     views.setTextViewText(R.id.Date, date); 

     if (!is24HourMode(this)) { 
      final boolean isMorning = (mCalendar.get(Calendar.AM_PM) == 0); 
      views.setTextViewText(R.id.AM_PM, (isMorning ? mAM : mPM)); 
     } else { 
      views.setTextViewText(R.id.AM_PM, ""); 
     } 

     ComponentName widget = new ComponentName(this, ClockWidget.class); 
     AppWidgetManager manager = AppWidgetManager.getInstance(this); 
     manager.updateAppWidget(widget, views); 
    } 

    private void reinit() { 
     final String[] ampm = new DateFormatSymbols().getAmPmStrings(); 
     mDateFormat = getString(R.string.date_format); 
     mTimeFormat = is24HourMode(this) ? FORMAT_24_HOURS : FORMAT_12_HOURS; 
     mCalendar = Calendar.getInstance(); 
     mAM = ampm[0].toLowerCase(); 
     mPM = ampm[1].toLowerCase(); 
    } 

    private static boolean is24HourMode(final Context context) { 
     return DateFormat.is24HourFormat(context); 
    } 


    private final BroadcastReceiver mTimeChangedReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      final String action = intent.getAction(); 

      if (action.equals(Intent.ACTION_TIME_CHANGED) || 
        action.equals(Intent.ACTION_TIMEZONE_CHANGED)) { 

       reinit(); 
      } 

      update(); 
     } 
    }; 
} 

}

答えて

0

大丈夫。つまり、ウィジェットが更新されているときにシステム時間が変更されたときに、時間が変更されたときに初めて更新されることを意味します。 あなたの提案は、あなたのサービスを最初に更新する必要があります。それは時間が変更されたときに更新されます。 このようにして問題を解決することができます。それを試してみてください。

+0

私のコードに従って私にコードを与えてください。 –

+0

でコードを作成してくださいこのコードを追加してくださいupdate(); –