2017-06-13 72 views
-2

我嘗試實現數字時鐘部件。當我在屏幕上拖動時鐘小部件時,它在主屏幕上佔用一個空間,但是當系統時鐘在時間上更新時,我的小部件時鐘顯示爲小部件前部。手機時鐘的時間是2:10,我在屏幕上拖動我的小部件,當系統時鐘爲2:11時,我的小部件顯示在主屏幕上,它不顯示。這是我的clockwidget代碼。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

沒關係。這意味着系統時間在那個時候改變了,你的小部件正在更新,所以當時間改變時它可能會首次更新。 所以我的建議是,你應該在onCreate時間第一次更新你的服務,然後它會在時間改變時更新。 這樣你的問題就可以解決。嘗試一下。

+0

根據我的代碼給我代碼。 –

+0

at on在您的代碼中創建請添加此代碼update(); –