復雜的widget布局

packagecom.example.tlinux.widgetprovider;

importjava.util.ArrayList;

importjava.util.HashMap;

importjava.util.List;

importjava.util.Map;

importandroid.content.Context;

importandroid.content.Intent;

importandroid.database.Cursor;

importandroid.widget.RemoteViews;

importandroid.widget.RemoteViewsService.RemoteViewsFactory;

public classWidgetFactoryimplementsRemoteViewsFactory {

privateContextcontext;

privateListdata;

private intversion=0;

publicWidgetFactory(Context context,Intent intent) {

//TODO Auto-generated constructor stub

this.context= context;

data=newArrayList<>();

}

@Override

public voidonCreate() {

//TODO Auto-generated method stub

InitListViewData();

}

//初始化DATA

private voidInitListViewData() {

for(inti=version;i

data.add("position:"+i);

}

version++;

}

@Override

public voidonDataSetChanged() {

//TODO Auto-generated method stub

}

@Override

public voidonDestroy() {

//TODO Auto-generated method stub

data.clear();

}

@Override

public intgetCount() {

//TODO Auto-generated method stub

returndata.size();

}

@Override

publicRemoteViewsgetViewAt(intposition) {

//TODO Auto-generated method stub

HashMap map;

RemoteViews remoteViews =newRemoteViews(context.getPackageName(),

R.layout.item);//獲取item的視圖

remoteViews.setTextViewText(R.id.tv_name,data.get(position));

returnremoteViews;

}

@Override

publicRemoteViewsgetLoadingView() {

//TODO Auto-generated method stub

return null;

}

@Override

public intgetViewTypeCount() {

//TODO Auto-generated method stub

return1;

}

@Override

public longgetItemId(intposition) {

//TODO Auto-generated method stub

returnposition;

}

@Override

public booleanhasStableIds() {

//TODO Auto-generated method stub

return true;

}

}





packagecom.example.tlinux.widgetprovider;
packagecom.example.tlinux.widgetprovider;




package="com.example.tlinux.widgetprovider">

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme">

android:name=".MainActivity"

android:label="@string/app_name">

android:permission="android.permission.BIND_REMOTEVIEWS"

android:exported="false">

importandroid.app.PendingIntent;

importandroid.appwidget.AppWidgetManager;

importandroid.appwidget.AppWidgetProvider;

importandroid.content.ComponentName;

importandroid.content.Context;

importandroid.content.Intent;

importandroid.net.Uri;

importandroid.text.SpannableStringBuilder;

importandroid.text.format.Time;

importandroid.text.style.AbsoluteSizeSpan;

importandroid.text.style.TypefaceSpan;

importandroid.widget.RemoteViews;

importjava.util.Timer;

importjava.util.TimerTask;

/**

* Created by tlinux on 15-8-10.

*/

public classMyWidgetProviderextendsAppWidgetProvider {

privateAppWidgetManagerappWidgetManager;

private int[]appWidgetIds;

privateContextcontext;

@Override

public voidonReceive(Context context,Intent intent) {

super.onReceive(context,intent);

String action=intent.getAction();

if((context.getPackageName() +".APPWIDGET_UPDATE").equals(action)) {

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

performUpdate(context,appWidgetManager,

appWidgetManager.getAppWidgetIds(getComponentName(context)),

null/* no eventIds */);

}

}

staticComponentNamegetComponentName(Context context) {

return newComponentName(context,MyWidgetProvider.class);

}

@Override

public voidonUpdate(Context context,AppWidgetManager appWidgetManager, int[] appWidgetIds) {

super.onUpdate(context,appWidgetManager,appWidgetIds);

performUpdate(context,appWidgetManager,appWidgetIds, null);

}

private voidperformUpdate(Context context,AppWidgetManager appWidgetManager, int[] appWidgetIds, long[] changedEventIds) {

//? ? ? ? for (int appWidgetId: appWidgetIds){

//? ? ? ? ? ? Intent updateIntent=new Intent(context,MyWidgetService.class);

//? ? ? ? ? ? updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

//? ? ? ? ? ? if (changedEventIds!=null){

//? ? ? ? ? ? ? ? updateIntent.putExtra("com.android.xxxx", changedEventIds);

//? ? ? ? ? ? }

//

//

//

//? ? ? ? ? ? updateIntent.setData(Uri.parse(updateIntent.toUri(Intent.URI_INTENT_SCHEME)));

//

//

//? ? ? ? ? ? RemoteViews views=new RemoteViews(context.getPackageName(), R.layout.appwidget);

//

//? ? ? ? ? ? Time time = new Time();

//? ? ? ? ? ? time.setToNow();

//? ? ? ? ? ? long millis = time.toMillis(true);

//

//? ? ? ? ? ? String monthDayStr = (time.month + 1) + "." + time.monthDay;

//? ? ? ? ? ? SpannableStringBuilder bob = new SpannableStringBuilder();

//? ? ? ? ? ? bob.append(monthDayStr);

//? ? ? ? ? ? bob.setSpan(new AbsoluteSizeSpan(22), 0, bob.length(), 0);

//? ? ? ? ? ? bob.setSpan(new TypefaceSpan("sans-serif-light"), 0, bob.length(), 0);

//? ? ? ? ? ? bob.append("? ");

//? ? ? ? ? ? int length = bob.length();

//? ? ? ? ? ? bob.append(time.year + "");

//? ? ? ? ? ? bob.setSpan(new AbsoluteSizeSpan(12), length, bob.length(), 0);

//? ? ? ? ? ? bob.setSpan(new TypefaceSpan("sans-serif-thin"), length, bob.length(), 0);

//

//? ? ? ? ? ? views.setTextViewText(R.id.tv_title, bob);

//

//? ? ? ? ? ? views.setRemoteAdapter(appWidgetId, R.id.lv_content, updateIntent);

//? ? ? ? ? ? appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.lv_content);

//

//? ? ? ? ? ? appWidgetManager.updateAppWidget(appWidgetId, views);

//? ? ? ? }

this.appWidgetIds= appWidgetIds;

this.appWidgetManager= appWidgetManager;

this.context= context;

Timer timer =newTimer();//時時更新

timer.schedule(newTimerTask() {

@Override

public voidrun() {

//TODO Auto-generated method stub

for(inti =0;i < MyWidgetProvider.this.appWidgetIds.length;i++)// appWidgetIds.length,表示創(chuàng)建了多shao個widget

{

Intent ServiceIntent =newIntent(MyWidgetProvider.this.context,MyWidgetService.class);

//創(chuàng)建一個intent,啟動WidgetService,WidgetService事繼承的RemoteViewsService,返回一個RemoteViewsFactory

ServiceIntent.putExtra(MyWidgetProvider.this.appWidgetManager.EXTRA_APPWIDGET_ID,

MyWidgetProvider.this.appWidgetIds[i]);

ServiceIntent.setData(Uri.parse(ServiceIntent.toUri(ServiceIntent.URI_INTENT_SCHEME)));

//設置uri隆夯,我不太清楚的地方是這邊的uri是怎么獲取到的蕉鸳,是根據(jù)剛開始new的時候得出的URI嗎希俩?

RemoteViews widgetRemoteView =newRemoteViews(MyWidgetProvider.this.context

.getPackageName(),R.layout.appwidget);

widgetRemoteView.setRemoteAdapter(MyWidgetProvider.this.appWidgetIds[i],

R.id.lv_content,ServiceIntent);

//走到這里大家應該可以看出chǔ理Listview需要用SetRemoteAdapter理澎,和ServiceIntent返回的值

MyWidgetProvider.this.appWidgetManager.updateAppWidget(MyWidgetProvider.this.appWidgetIds[i],

widgetRemoteView);

}

}

},0,1000);

}

}

importandroid.appwidget.AppWidgetManager;

importandroid.content.BroadcastReceiver;

importandroid.content.Context;

importandroid.content.Intent;

importandroid.graphics.Color;

importandroid.widget.RemoteViews;

importandroid.widget.RemoteViewsService;

importjava.util.ArrayList;

importjava.util.List;

/**

* Created by tlinux on 15-8-10.

*/

public classMyWidgetServiceextendsRemoteViewsService {

@Override

publicRemoteViewsFactoryonGetViewFactory(Intent intent) {

return newWidgetFactory(this,intent);

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市锅论,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌楣号,老刑警劉巖最易,帶你破解...
    沈念sama閱讀 222,946評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異炫狱,居然都是意外死亡耘纱,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,336評論 3 399
  • 文/潘曉璐 我一進店門毕荐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來束析,“玉大人,你說我怎么就攤上這事憎亚≡笨埽” “怎么了弄慰?”我有些...
    開封第一講書人閱讀 169,716評論 0 364
  • 文/不壞的土叔 我叫張陵,是天一觀的道長蝶锋。 經(jīng)常有香客問我陆爽,道長,這世上最難降的妖魔是什么扳缕? 我笑而不...
    開封第一講書人閱讀 60,222評論 1 300
  • 正文 為了忘掉前任慌闭,我火速辦了婚禮,結(jié)果婚禮上躯舔,老公的妹妹穿的比我還像新娘驴剔。我一直安慰自己,他們只是感情好粥庄,可當我...
    茶點故事閱讀 69,223評論 6 398
  • 文/花漫 我一把揭開白布丧失。 她就那樣靜靜地躺著,像睡著了一般惜互。 火紅的嫁衣襯著肌膚如雪布讹。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,807評論 1 314
  • 那天训堆,我揣著相機與錄音描验,去河邊找鬼。 笑死坑鱼,一個胖子當著我的面吹牛挠乳,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播姑躲,決...
    沈念sama閱讀 41,235評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼睡扬,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了黍析?” 一聲冷哼從身側(cè)響起卖怜,我...
    開封第一講書人閱讀 40,189評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎阐枣,沒想到半個月后马靠,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,712評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡蔼两,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,775評論 3 343
  • 正文 我和宋清朗相戀三年甩鳄,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片额划。...
    茶點故事閱讀 40,926評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡妙啃,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情揖赴,我是刑警寧澤馆匿,帶...
    沈念sama閱讀 36,580評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站燥滑,受9級特大地震影響渐北,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜铭拧,卻給世界環(huán)境...
    茶點故事閱讀 42,259評論 3 336
  • 文/蒙蒙 一赃蛛、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧搀菩,春花似錦呕臂、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,750評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽炼团。三九已至澎嚣,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間瘟芝,已是汗流浹背易桃。 一陣腳步聲響...
    開封第一講書人閱讀 33,867評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留锌俱,地道東北人晤郑。 一個月前我還...
    沈念sama閱讀 49,368評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像贸宏,于是被迫代替她去往敵國和親造寝。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,930評論 2 361

推薦閱讀更多精彩內(nèi)容