[問題] Widget 的 onUpdate
大家好,最近再做一個 widget , 但對於 widget 有一些小問題想請問
想先請大家幫我看一下這樣寫對不對:
在 Widget 上要綁定按鈕的 Click 事件,採用 remoteViews 的
setOnClickPendingIntent , 設定 PendingIntent 的 action String
並在 AndroidManifest.xml 定義 intent-filter 去接收,然後
在 onReceive 的地方做判斷,如:
// rv 是 RemoteViews 這段 Code 我是寫在 onUpdate() 裡面
Intent intent=new Intent("BTN_ACTION_STRING");
PendingIntent pendInt;
pendInt=PendingIntent.getBroadcast(context, 0, intent, 0);
rv.setOnClickPendingIntent(R.id.btn,pendInt);
//接著再 updateAppWidget 就不寫了
[AndroidManifest.xml]
<receiver android:name=".MyWidget" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="BTN_ACTION_STRING"></action>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_provider"/>
</receiver>
不過有時候會發生,過太久沒動還是什麼的,造成 Widget 按鈕都沒反應
個人覺得可能是 AppWidgetProvider 被系統 GC 掉了?
想請問大家要如何避免這個問題?
還有, onUpdate 到底觸發的時機是甚麼時候?
我在 widget_provider.xml 設定updateperiodmillis = 1000 (這樣是一秒吧?)
onUpdate 做 Log.d , 但似乎沒有每秒鐘都出現
最後是~ 我需不需要在 onReceive 的地方判斷
android.appwidget.action.APPWIDGET_UPDATE
的 action 去做 update 之類的呢?
主要是要解決有時候 widget 按鈕會失效的問題
感謝大家~
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.171.115.187
→
05/24 02:34, , 1F
05/24 02:34, 1F
→
05/24 02:35, , 2F
05/24 02:35, 2F
→
05/24 03:33, , 3F
05/24 03:33, 3F
→
05/24 03:33, , 4F
05/24 03:33, 4F
→
05/24 03:33, , 5F
05/24 03:33, 5F
→
05/24 09:03, , 6F
05/24 09:03, 6F
→
05/24 09:04, , 7F
05/24 09:04, 7F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 4 篇):