Android:AppWidget、桌面小部件
主要事件方法: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 /** * Implementation of App Widget functionality. */ public class NewAppWidget extends AppWidgetProvider { /** * 到达指定的更新时间或者当用户向桌面添加AppWidget时被调用 * */ @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int [] appWidgetIds) { // There may be multiple widgets active, so update all of them final int N = appWidgetIds.length; for ( int i = 0 ; i < N; i++) { updateAppWidget(context, appWidgetManager, appWidgetIds[i]); } } /** * AppWidget的实例第一次被创建时调用 * */ @Override public void onEnabled(Context context) { // Enter relevant functionality for when the first widget is created } /** * 最后一个appWidget被删除时调用 * */ @Override public void onDisabled(Context context) { // Enter relevant functionality for when the last widget is disabled } /** * 接受广播事件 * */ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub super .onReceive(context, intent); } /** * 删除一个AppWidget时调用 * */ @Override public void onDeleted(Context context, int [] appWidgetIds) { // TODO Auto-generated method stub super .onDeleted(context, appWidgetIds); } static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { CharSequence widgetText = context.getString(R.string.appwidget_text); // Construct the RemoteViews object RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget); views.setTextViewText(R.id.appwidget_text, widgetText); // Instruct the widget manager to update the widget appWidgetManager.updateAppWidget(appWidgetId, views); } } 本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1228549,如需转载请自行联系原作者