Android 桌面小部件

1. 添加AppWidgetProvider 实际上就是个带有界面的BroadcastReceiver

public class SimpleWidgetProvider extends AppWidgetProvider {

}

2. 在res -> xml 下添加AppWidgetProviderInfo

 <?xml version="1.0" encoding="utf-8"?>
 <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="120dp"
     android:minHeight="60dp"
 android:updatePeriodMillis="1800000"
 android:initialLayout="@layout/hoem_widget"
 android:resizeMode="horizontal|vertical"
 android:widgetCategory="home_screen|keyguard"
     android:previewImage="@drawable/ic_launcher">
 </appwidget-provider>

3. 添加桌面widget的layout

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@color/accent_normal">
    <ImageView
      android:id="@+id/doge_imageView"
      android:layout_width="110dp"
      android:layout_height="110dp"
      android:src="@drawable/ic_clear_white_24dp" />
   </LinearLayout>

4. 注册

1 <receiver android:name=".SimpleWidgetProvider" >
2 <intent-filter>
3 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
4 <action android:name="com.seewo.homewidgets.action.CLICK" />
5 </intent-filter>
6 <meta-data android:name="android.appwidget.provider"
7 android:resource="@xml/appwidget_provider_info" />
8 </receiver>


5. 运行后,长按桌面空白处,在底部的桌面小部件列表中将自定义的部件拖到桌面

 

 

posted @ 2017-11-03 12:42  戴杭林  阅读(2250)  评论(0编辑  收藏  举报