AppWidgetProvider的生命周期和使用

/*****************************
 * 
 * @author util_c
 *
 *起动时AppWidgetProvider的执行流程:
 *第一步:onReceive()
 * 接到广播事件:android.appwidget.action.APPWIDGET_ENABLED
 *第二步:onEnabled()
 *第三步:onReceive()
 * 接到广播事件:android.appwidget.action.APPWIDGET_UPDATE
 *第四步:onUpdate()
 *
 *被删除时AppWidgetProvider
 *第一步:onReceive()
 *  接到广播事件:android.appwidget.action.APPWIDGET_DELETED
 *第二步:onDelete();
 *第三步:onReceive()
 *  接到广播事件:android.appwidget.action.APPWIDGET_DISABLED
 *第四步:onDisabled()
 **********************/
public class MyAppWidgetProvide extends AppWidgetProvider {
private int [] icons={
R.drawable.map25,
R.drawable.map26,
R.drawable.map27,
R.drawable.map28,
R.drawable.map29,
R.drawable.map30,
R.drawable.map31,
R.drawable.map32,
R.drawable.map33,
R.drawable.map35,
R.drawable.map36,
};
//必须时静态的,因为AppWidgetProvider继承自:BroadcastReceive
//执行完onReceive()方法就会,销除该对象
private static Set<Integer> set = new HashSet<Integer>();

//更新appwidget的广播事件和启动更新service的事件
//这个ACTION是自己增加的,记得要在配置清单增加否则不能开启服务器和正常发送更新广播事件
public static final String ACTION="org.gjt.chang.widget";

@SuppressLint("NewApi")
//经过测试这个方法,无论怎么去改变大小都不会执行,如果有知道的麻烦告诉一声,谢谢
@Override
public void onAppWidgetOptionsChanged(Context context,
AppWidgetManager appWidgetManager, int appWidgetId,
Bundle newOptions) {
Log.i("TEST", "onAppWidgetOptionsChanged");
super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId,
newOptions);
}
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
Log.i("TEST", "onDeleted");
//appwidget被删除时,就从集合中移除其ID
for (int i : appWidgetIds) {
Log.i("TEST", "onDeleted:id="+i);
set.remove(Integer.valueOf(i));
}
super.onDeleted(context, appWidgetIds);

}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
Log.i("TEST", "onUpdate");
//增加一个appwidget到桌面上时,就把这个appwidget的ID增加到更新集合中
for (int i : appWidgetIds) {
Log.i("TEST", "onUpdate:id="+i);
set.add(Integer.valueOf(i));
}
}

@Override
public void onReceive(Context context, Intent intent) {
Log.i("TEST", "onReceive:action="+intent.getAction());
//接收并更新从更新service发送过来的广播事件
if(intent.getAction()!=null&&intent.getAction().equals(ACTION)){
Log.i("TEST", "---------onReceive:size="+set.size());
updateWidgets(context,AppWidgetManager.getInstance(context));
}
//当点appwidget时,执行相应的事件
else if(intent.hasCategory(Intent.CATEGORY_ALTERNATIVE)){
Uri data = intent.getData();
int id = Integer.parseInt(data.getSchemeSpecificPart());
Log.i("TEST", "data="+data.getPath()+",id="+id);
Toast.makeText(context, "你点击了我..."+id, Toast.LENGTH_SHORT).show();
}
super.onReceive(context, intent);
}

/************************
* 根据appwidget的ID去更新appwidget
* @param context 上下文
* @param manager  用来更appwidget的状态
********************/
private void updateWidgets(Context context, AppWidgetManager manager) {
Iterator<Integer> it = set.iterator();
while(it.hasNext()){
int id = it.next().intValue();
int index = new Random().nextInt(icons.length);//随机获取icons数组大小范围之内的下标索引
Log.i("TEST", "updateWidgets:id="+id+",index="+index);
RemoteViews view = new RemoteViews(context.getPackageName(),R.layout.my_widget);
view.setImageViewResource(R.id.icon, icons[index]);
Intent intent = new Intent();
intent.setClass(context, MyAppWidgetProvide.class);
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
intent.setData(Uri.parse("custom:"+id));
//直接发送广播到intent接点的类
PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent, 0);
view.setOnClickPendingIntent(R.id.icon, pending);//增加点击事件
manager.updateAppWidget(id, view);//更新appwidget
}
}
@Override
public void onDisabled(Context context) {
Log.i("TEST", "onDisabled");
//开闭开启的更新service
context.stopService(new Intent(ACTION));
super.onDisabled(context);

}
@Override
public void onEnabled(Context context) {
Log.i("TEST", "onEnabled");
super.onEnabled(context);
//开启更新service
//ACTION是在配置清单注册过的action是一致的
context.startService(new Intent(ACTION));
}

}


http://download.csdn.net/detail/util_c/6778619

展开阅读全文

没有更多推荐了,返回首页