一、创建AppWidget的布局文件,名称为appwidget_layout.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><Button android:id="@+id/button1"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="测试"    /></LinearLayout>

二、创建appwidgetprovider,先在res文件夹下创建xml文件夹,同时在xml文件夹下创建appwidget_info.xml

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"    android:minWidth="294dp"    android:minHeight="72dp"    android:updatePeriodMillis="600000"    android:initialLayout="@layout/appwidget_layout"   ></appwidget-provider>

三、在AndroidManifest.xml中添加如下信息

<receiver android:name="cn.edu.appwidget.MyAppWidgetProvider"><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>

四、继承AppWidgetProvider

package cn.edu.appwidget;import android.app.PendingIntent;import android.appwidget.AppWidgetManager;import android.appwidget.AppWidgetProvider;import android.content.Context;import android.content.Intent;import android.widget.RemoteViews;import cn.edu.design.AppWidget_01Activity;import cn.edu.design.R;public class MyAppWidgetProvider extends AppWidgetProvider{@Overridepublic void onDeleted(Context context, int[] appWidgetIds) {// TODO Auto-generated method stubsuper.onDeleted(context, appWidgetIds);}@Overridepublic void onDisabled(Context context) {// TODO Auto-generated method stubsuper.onDisabled(context);}@Overridepublic void onEnabled(Context context) {// TODO Auto-generated method stubsuper.onEnabled(context);}@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubsuper.onReceive(context, intent);}@Overridepublic void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {System.out.println("update");for(int i=0;i<appWidgetIds.length;i++){Intent intent=new Intent(context,AppWidget_01Activity.class);PendingIntent pendingIntent=PendingIntent.getActivity(context, 0, intent, 0);RemoteViews remoteViews=new RemoteViews(context.getPackageName(),R.layout.appwidget_layout);remoteViews.setOnClickPendingIntent(R.id.button1, pendingIntent);appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);}// TODO Auto-generated method stubsuper.onUpdate(context, appWidgetManager, appWidgetIds);}}

五、处理广播信息

1、其他步骤和上面的方法相同,将AndroidManifest.xml修改为

<receiver android:name="cn.edu.appwidget.MyAppWidgetProvider_01"><intent-filter><action android:name="android.appwidget.action.APPWIDGET_UPDATE" /></intent-filter><intent-filter ><action android:name="cn.edu.myaction"/> <!--cn.edu.myaction是自己定义的字符串--></intent-filter><meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget_info"/></receiver>

2、在继承AppWidgetProvider的类中

public class MyAppWidgetProvider_01 extends AppWidgetProvider{  //自己定义字符串,在Intent的Action中使用  private final static String MY_ACTION="cn.edu.myaction";@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubsuper.onReceive(context, intent);if(intent.getAction().equals(MY_ACTION)){System.out.println("execute broadcast");}}@Overridepublic void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {// TODO Auto-generated method stubSystem.out.println("update");for(int i=0;i<appWidgetIds.length;i++){Intent intent=new Intent();intent.setAction(MY_ACTION);//当执行pendingIntent的时候会产生一个广播消息PendingIntent pendingIntent=PendingIntent.getBroadcast(context, 0, intent, 0);RemoteViews remoteViews=new RemoteViews(context.getPackageName(),R.layout.appwidget_layout);//为aawidget的布局文件中的按钮增加事件remoteViews.setOnClickPendingIntent(R.id.button1, pendingIntent);appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);}super.onUpdate(context, appWidgetManager, appWidgetIds);}}

更多相关文章

  1. Android中五中布局文件的使用和介绍
  2. Android软键盘弹出时不把布局顶上去的解决方法
  3. 输入法弹出后,不影响activity原有布局(android:windowSoftInputMo
  4. Android中的表格布局TableLayout
  5. Android 学习笔记6 —— Activity 布局学习1
  6. Android布局背景颜色设置
  7. android:layout_gravity 居中布局
  8. 转:善用Android预定义样式来为我们的布局设置效果,大大节约代码量
  9. Android创建隐藏文件或者文件夹

随机推荐

  1. JDK ANT ANDROID Configure
  2. Android+JNI调用–文件操作
  3. GestureDetector.OnGestureListener 触摸
  4. Android(安卓)视图绑定,找不到类 'ResultP
  5. Android菜单应用(Menu)
  6. Android(安卓)如何使一个service 开机启
  7. android延迟进入主界面和点击按钮进入主
  8. Android应用程序使用Localsocket与Linux
  9. Android国际化
  10. 动态设置android:drawableLeft|Right|Top