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。



更多相关文章

  1. 2011.09.01(3)——— android service startService bindService
  2. android保存图片到sdcard后,在图片浏览器不能马上显示出来
  3. Intent常用Uri,打开相应的应用
  4. Android之service探究
  5. 2011.09.01(3)——— android service startService bindService
  6. Android(安卓)用AsyncTask下载网络图片并显示百分比
  7. Android(安卓)Touch事件
  8. Android中Scroller类的分析
  9. Android(安卓)TextView文字过多时通过滚动条显示多余内容

随机推荐

  1. Android官方开发文档Training系列课程中
  2. android 安全讲座第一层 Android(安卓)AP
  3. Android上的蓝牙通信功能的开发:Bluetooth
  4. 探秘腾讯Android手机游戏平台之不安装游
  5. Android系统使用MediaFile类判断音频文件
  6. Android开发指南-用户界面-创建菜单 .
  7. Android日志工具Log
  8. 使用adb和FTP从电脑给Android设备拷贝文
  9. Android(安卓)Canvas简单使用
  10. Android设置Alpha值实现图片渐变效果