Android:AppWidget、桌面小部件

主要事件方法:

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



参考资料:http://www.cnblogs.com/qianlifeng/archive/2011/03/26/1996407.html















本文出自 “天空没有痕迹但我飞过” 博客,转载请与作者联系!

你可能感兴趣的:(appwidget,桌面小部件)