看板 AndroidDev 關於我們 聯絡資訊
大家好,最近再做一個 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
gpc:xml寫的時間週期不可用 他會被framework覆蓋掉 05/24 02:34
gpc:有可能會被調整到30分鐘一次 要週期更新 請愛用 alarm 即可 05/24 02:35
darkk6:alarm 好用!! 感謝~ 那麼有時按鈕會失效的問題,我是不是 05/24 03:33
darkk6:只要定時去 update widget 重新 bind PendingIntent 就 05/24 03:33
darkk6:可以解決了呢~ ? 05/24 03:33
asilzheng:你的 widget 怎麼更新的 每次都有 bind PendingIntent? 05/24 09:03
asilzheng:不然應該只是沒按到? 05/24 09:04