Android的桌面小图标Widget的学习

来源:互联网 发布:高校数据采集平台 编辑:程序博客网 时间:2024/05/20 19:48

android提供向桌面上放置一个远程布局的功能。最近学了一下,在这里记录一下学习笔记。

public class MyWidget extends AppWidgetProvider{    public static String TAG = "MyWidget";    @Override    public void onReceive(Context context, Intent intent) {        Log.i(TAG, "onReceive: ");        super.onReceive(context, intent);    }    @Override    public void onEnabled(Context context) {        Log.i(TAG, "onEnabled: ");        super.onEnabled(context);    }    @Override    public void onDeleted(Context context, int[] appWidgetIds) {        Log.i(TAG, "onDeleted: ");        super.onDeleted(context, appWidgetIds);    }    @Override    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {        Log.i(TAG, "onUpdate: ");        super.onUpdate(context, appWidgetManager, appWidgetIds);    }    @Override    public void onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds) {        Log.i(TAG, "onRestored: ");        super.onRestored(context, oldWidgetIds, newWidgetIds);    }    @Override    public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {        Log.i(TAG, "onAppWidgetOptionsChanged: ");        super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);    }    @Override    public void onDisabled(Context context) {        Log.i(TAG, "onDisabled: ");        super.onDisabled(context);    }}

1,首先要建一个类MyWidget继承AppWidgetProvider,通过查看他的源码可以知道它是一个BroadcastReceiver。所以要在清单文件当中注册。

它的action为:android.appwidget.action.APPWIDGET_UPDATE。 

<receiver android:name=".MyWidget"    android:icon="@mipmap/desk_play"    android:label="音乐">    <intent-filter>        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />    </intent-filter>    <meta-data android:name="android.appwidget.provider"        android:resource="@xml/appwidget_info" /></receiver>

<meta-data>元素中定义了Widget的AppWidgetInfo的信息。appwidget_info.xml文件在res/xml文件夹下。内容是:

<?xml version="1.0" encoding="utf-8"?><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"    android:minWidth="40dp"    android:minHeight="40dp"    android:updatePeriodMillis="86400000"    android:previewImage="@drawable/play_ic"    android:initialLayout="@layout/my_widget"    android:resizeMode="horizontal|vertical"    android:widgetCategory="home_screen|keyguard"></appwidget-provider><!--<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"    android:minWidth="40dp"    android:minHeight="40dp"    android:updatePeriodMillis="86400000"    android:previewImage="@drawable/preview"    android:initialLayout="@layout/example_appwidget"    android:configure="com.example.android.ExampleAppWidgetConfigure"    android:resizeMode="horizontal|vertical"    android:widgetCategory="home_screen|keyguard"    android:initialKeyguardLayout="@layout/example_keyguard"></appwidget-provider>minWidth和minHeight是widget在桌面上的的最小的宽高updatePeriodMillis是Widget的刷新时间,单位是:ms。如果要是需要频繁的更新。可以用AlarmManager,并且设置Type为ELAPSED_REALTIME或者RTC。initialLayout是一个布局就是Widget的布局。configure的是一个Activity。当向桌面上添加Widget的时候就会启动,用来提示这个Widget的作用previewImage是当选择该Widget添加到桌面的时候的的显示图片。拖动widget向桌面的图片resizeMode是widget在桌面上调整大小的时候的模式,水平方向,垂直方向。widgetCategory是widget显示的位置,桌面或锁屏的界面initialKeyguardLayout是锁屏的时候的显示布局-->
然后就可以在布局文件当中设置widget的显示布局。在Widget的布局要依赖RemoteViews ,RemoteViews对指定的控件进行支持

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/play_ic"></LinearLayout><!--Widget的布局中只支持一下的布局和控件。不支持这些控件的子类是不支持的:FrameLayoutLinearLayoutRelativeLayoutGridLayoutAnalogClockButtonChronometerImageButtonImageViewProgressBarTextViewViewFlipperListViewGridViewStackViewAdapterViewFlipper-->

做完这些就可以在桌面上添加Widget了。


2,Widget的生命周期。

第一次创建一个Widget:04-03 10:26:13.416 1300-1300/com.example.yyw.appwidgetdemo I/MyWidget: onReceive: 04-03 10:26:13.416 1300-1300/com.example.yyw.appwidgetdemo I/MyWidget: onEnabled: 04-03 10:26:13.416 1300-1300/com.example.yyw.appwidgetdemo I/MyWidget: onReceive: 04-03 10:26:13.420 1300-1300/com.example.yyw.appwidgetdemo I/MyWidget: onUpdate: 第二次创建一个Widget:04-03 10:26:39.515 1300-1300/com.example.yyw.appwidgetdemo I/MyWidget: onReceive: 04-03 10:26:39.515 1300-1300/com.example.yyw.appwidgetdemo I/MyWidget: onUpdate: 第三次创建一个Widget04-03 10:29:07.485 1300-1300/com.example.yyw.appwidgetdemo I/MyWidget: onReceive: 04-03 10:29:07.485 1300-1300/com.example.yyw.appwidgetdemo I/MyWidget: onUpdate:修改Widget在桌面上的大小的时候04-03 10:30:09.057 1300-1300/com.example.yyw.appwidgetdemo I/MyWidget: onReceive: 04-03 10:30:09.057 1300-1300/com.example.yyw.appwidgetdemo I/MyWidget: onAppWidgetOptionsChanged: 移除一个Widget04-03 10:30:34.692 1300-1300/com.example.yyw.appwidgetdemo I/MyWidget: onReceive: 04-03 10:30:34.692 1300-1300/com.example.yyw.appwidgetdemo I/MyWidget: onDeleted: 再移除一个:04-03 10:30:34.692 1300-1300/com.example.yyw.appwidgetdemo I/MyWidget: onReceive: 04-03 10:30:34.692 1300-1300/com.example.yyw.appwidgetdemo I/MyWidget: onDeleted: 当最后一个被移除的时候:04-03 10:31:44.211 1300-1300/com.example.yyw.appwidgetdemo I/MyWidget: onReceive: 04-03 10:31:44.211 1300-1300/com.example.yyw.appwidgetdemo I/MyWidget: onDeleted: 04-03 10:31:44.219 1300-1300/com.example.yyw.appwidgetdemo I/MyWidget: onReceive: 04-03 10:31:44.219 1300-1300/com.example.yyw.appwidgetdemo I/MyWidget: onDisabled:
从上面的log可以看出:

onReceive在每一次的创建和移除都会调用。

onEnabled只有在第一的时候才会调用。

onUpdate在每次创建都会调用的。而且在android:updatePeriodMillis的时间到的时候也会调用。

onDeleted在每次移除一个Widget的时候会被调用。

onDisabled在全部移除Widget的时候会调用到。

onAppWidgetOptionsChanged会在改变Widget大小的时候调用。

3,添加桌面view的点击事件。

 @Override    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {        for (int id : appWidgetIds ) {            Log.i(TAG, "onUpdate: " + id);            AppWidgetManager am = AppWidgetManager.getInstance(context);            //Widget是在其他应用上面显示的所以要用RemoteViews来管理            RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.my_widget);            Intent intent = new Intent(context,MainActivity.class);            PendingIntent pendingIntent = PendingIntent.getActivity(context,2016,intent,0);            //设置点击图片的点击事件,打开一个Activity            remoteViews.setOnClickPendingIntent(R.id.iv,pendingIntent);            //把Widget更新            am.updateAppWidget(id,remoteViews);        }//        super.onUpdate(context, appWidgetManager, appWidgetIds);    }

用PendingIntent做为管理点击事件的的处理事件,可以打开Activity,service,broadcast。



0 0
原创粉丝点击