Android 窗口小部件--APP Widget

来源:互联网 发布:印度美食 知乎 编辑:程序博客网 时间:2024/05/20 05:04
今天就写写 Android 窗口小部件的用法,即App Widget.我们知道,App Widget是应用程序窗口小部件(Widget)是微型的应用程序视图,它可以被嵌入到其它应用程序中(比如桌面)并接收周期性的更新。你可以通过一个App Widget Provider来发布一个Widget。实现App Widget主要用到的类有AppWidgetProvider 它继承自BroadcastReceiver,它可以接受widget相关的广播,例如 widget 的更新、删除、开启和禁用等。接下来,给大家说说实现步骤:1、在资源文件夹res/layout中创建widget的布局文件,我创建的是widget.xml文件,如下:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >     <TextView         android:id="@+id/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="时间显示"        /></LinearLayout>

2、在res文件夹中创建xml文件夹,并且在xml文件夹中创建widgetconfig.xml文件,为widget的配置文件,如下:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"    android:initialLayout="@layout/widget"    android:minHeight="30dp"    android:minWidth="60dp"    android:updatePeriodMillis="864000" ></appwidget-provider>

3、创建TimerService继承自Service,其中实现onCreate(),onDestroy()方法。在onCreate方法中设置定时任务,使每隔一秒widget进行更新。
代码如下:

public class TimerService extends Service {    private Timer timer;    private SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    @Override    public IBinder onBind(Intent arg0) {        // TODO Auto-generated method stub        return null;    }    @Override    public void onCreate() {        // TODO Auto-generated method stub        super.onCreate();        timer=new Timer();        timer.schedule(new TimerTask() {            @Override            public void run() {                updateViews();              }        }, 0, 1000);//设置为每隔1秒更新    }    /**     * 更新     */    private void updateViews(){        String time=dateFormat.format(new Date());        RemoteViews rv=new RemoteViews(getPackageName(), R.layout.widget);        rv.setTextViewText(R.id.tv, time);        AppWidgetManager manager=AppWidgetManager.getInstance(getApplicationContext());        ComponentName cn=new ComponentName(getApplicationContext(), WidgetProvider.class);        manager.updateAppWidget(cn, rv);    }    @Override    public void onDestroy() {        // TODO Auto-generated method stub        super.onDestroy();        timer=null;    }

4、创建WidgetProvider继承自AppWidgetProvider,重写方法,其中onDeleted是将widget从屏幕删除是调用,onDisabled是在最后一个widget从屏幕删除是调用,onEnabled是在第一个widget添加到屏幕是调用,onUpdate是在刷新的时候调用。我们需要在执行onEnabled方法时开启服务,在onDisabled方法执行时,停止服务。代码如下:

public class WidgetProvider extends AppWidgetProvider {        @Override        public void onDeleted(Context context, int[] appWidgetIds) {            // TODO Auto-generated method stub            super.onDeleted(context, appWidgetIds);            //将widget从桌面删除        }        /**         * 最后一个widget被从桌面删除执行         */        @Override        public void onDisabled(Context context) {        // TODO Auto-generated method stub        super.onDisabled(context);        context.stopService(new Intent(context,TimerService.class));        }        /**         * 表示第一个widget添加到桌面上执行         */        @Override        public void onEnabled(Context context) {        // TODO Auto-generated method stub        super.onEnabled(context);        context.startService(new Intent(context,TimerService.class));        }        /**         *          */        @Override        public void onReceive(Context context, Intent intent) {        // TODO Auto-generated method stub        super.onReceive(context, intent);        }        /**         * 刷新的时候执行         */        @Override        public void onUpdate(Context context, AppWidgetManager appWidgetManager,            int[] appWidgetIds) {        // TODO Auto-generated method stub        super.onUpdate(context, appWidgetManager, appWidgetIds);        //remoteView和AppWidgetManager        }}   

5、我们需要在定义文件中定义我们的WidgetProvider和TimerService,代码如下:

   <receiver android:name="com.example.mywidght.WidgetProvider" >            <intent-filter>                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />            </intent-filter>            <meta-data                android:name="android.appwidget.provider"                android:resource="@xml/widghtconfig" />        </receiver>        <service android:name="com.example.mywidght.TimerService" >        </service>

当你做了以上步骤,就可以运行你的程序了,我贴的代码是我做的一个桌面的时间插件。如果觉得有用请点赞,谢谢。如果有疑问,可加群137149792讨论,共同进步。

0 0
原创粉丝点击