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);
}
}