2014-03-31 87 views
0

我在嘗試動態更新我的窗口小部件的TextClock的format24Hour無法在窗口小部件中調用一個TextClock的setFormat24Hour

要做到這一點我使用RemoteViews像這樣:

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.clockwidget); 
views.setString(R.id.textclock, "setFormat24Hour", formatString24.toString()); 
appWidgetManager.updateAppWidget(appWidgetId, views); 

然而,這導致我的窗口小部件,以顯示「問題加載控件」和logcat的顯示

03-31 17:37:19.872: W/AppWidgetHostView(1408): updateAppWidget couldn't find any view, using error view 
03-31 17:37:19.872: W/AppWidgetHostView(1408): android.widget.RemoteViews$ActionException: view: android.widget.TextClock doesn't have method: setFormat24Hour(class java.lang.String) 

但是我敢肯定那android.widget.TextClock有一個方法setFormat24Hour(CharSequence format)它被標記爲@RemotableViewMethod

有什麼我失蹤?爲什麼這不起作用?

+0

可能你需要顯式轉換字符串的CharSequence:您可以按以下格式設置這個時間呢? (雖然會很奇怪,因爲String是一個CharSequence,但我不知道如何完成方法匹配) – njzk2

回答

4

感謝njzk2我找到了解決方案。這是一個相當愚蠢的問題。 我需要撥打RemoteViews#setCharSequence而不是setString,然後一切正常!

0

我知道這個問題已經問了幾個月前,但你自己的答案其實是錯誤的我認爲。

正如API指南規定,允許在一個appwidget唯一的類是:

AnalogClock 
Button 
Chronometer 
ImageButton 
ImageView 
ProgressBar 
TextView 
ViewFlipper 
ListView 
GridView 
StackView 
AdapterViewFlipper 

因此,你需要在屏幕上更新微件每一秒,分鐘或小時的alarmManager,根據你想要什麼。

更新窗口小部件時,您必須將文本視圖的文本更改爲實際時間。

RemoteViews views = new RemoteViews(context.getPackageName(), viewID); 
Calendar c = Calendar.getInstance(); 
SimpleDateFormat timeFormat24 = new SimpleDateFormat("kk:mm"); 
String time24 = timeFormat24.format(c.getTime()); 
views.setTextViewText(R.id.textClock, time24); 

See this link for more information on the formatting.

+1

雖然你正確的看到文檔沒有列出TextClock(http://developer.android.com/guide /topics/appwidgets/index.html)作爲應用程序小部件中的可能小部件,這確實是可能的。 這可以通過嘗試驗證或驗證@RemoteView註釋存在於http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.2_r1中進行驗證/android/widget/TextClock.java?av=f。 –

+0

@PatrickHuy哇......真是太棒了!你有關於TextClock電池使用情況的任何信息嗎?在AppWidget中是否會喚醒手機? – Barthy

+0

我沒有硬數字,但電池使用情況比手動更新文本字段更好,因爲您不需要運行服務。 TextClock偵聽ACTION_TIME_TICK意圖。一個正常的應用程序小部件不能做到這一點。 ACTION_TIME_TICK不會喚醒手機(請參閱http://stackoverflow.com/questions/3274583/will-time-tick-broadcast-in-deep-sleep)我使用TextClock創建了此應用程序:https:// play。 google.com/store/apps/details?id=cc.frz.circleclock但是它也使用一項服務來更新對電池不太好的圖形,但可能沒問題。 –

相關問題