AppWidget的基礎(chǔ)知識(shí)可以參考網(wǎng)上也有的資料强重,相對(duì)比較實(shí)用的可以看看這篇:
app widget簡(jiǎn)單用法之音樂播放器桌面控件
但在實(shí)際調(diào)測(cè)過(guò)程中赃阀,8.0版本以上的手機(jī)死活收不到service發(fā)過(guò)來(lái)的ACTION消息根暑,試了6.0和7.0時(shí)沒有問題的。查了android官方的一些說(shuō)法椎椰,在8.0以上廣播機(jī)制有所變化宦棺,sendbroadcast前要指定下receiver的類(AppwidgetProvider本質(zhì)上還是一個(gè)receiver),比如:在MyAppwidgetProvider里onUpdate時(shí)指定了一個(gè)按鍵的事件:
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.myappwidget);
Intent intent_count = new Intent();
intent_count.setAction(Constant.ACTION_COUNT);?
PendingIntent pendingIntent_count = PendingIntent.getBroadcast(context, 0, intent_count, 0);?
remoteViews.setOnClickPendingIntent(R.id.wvbtn_t1, pendingIntent_count);
如果此時(shí)的廣播是發(fā)給MyAppwidgetProvider自己的箕戳,則需要添加:
intent_count.setComponent(new ComponentName(context,MyAppWidgetProvider.class));//必須寫
如果此時(shí)的廣播是發(fā)給其他的service或者activity某残,則不要添加setComponent,調(diào)試發(fā)現(xiàn)加了廣播收不到陵吸。
在其他的service或者activity里自己寫個(gè)receiver玻墅,在onReceive里添加:其他代碼可以參考上面的播放器文章
new_intent.setComponent(new ComponentName(context,MyAppWidgetProvider.class));//8.0以上版本必須寫
其他的如靜態(tài)注冊(cè)按照常規(guī)實(shí)現(xiàn)即可。目前僅發(fā)現(xiàn)這一個(gè)問題壮虫,后續(xù)再補(bǔ)充吧澳厢。