一、创建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:dimen尺寸资源文件的使用
  2. Android(安卓)修改WiFi热点的默认SSID和密码
  3. android下usb框架系列文章---(3)Storage框架整理
  4. 第15天android:使用sqlite
  5. android中The connection to adb is down .. You must restart a
  6. android TextUtils的使用
  7. android 音频总结
  8. FregClient进程,创建一个BpFregService类型的代理对象
  9. Android(安卓)Studio下项目编译出错could not find com.android.

随机推荐

  1. Android开发——消息处理传递机制
  2. WebRTC android h264 编解码适配(二)
  3. 【Android】Handler 中 Looper 死循环为
  4. Android中WebView中的JS访问
  5. Android(安卓)Binder机制浅析
  6. Android(安卓)SDK工具(谷歌提供的16个工具
  7. Android系统的启动时间
  8. 卷二 Dalvik与Android源码分析 第五章 In
  9. Android(安卓)兼容性介绍之setBackground
  10. 【iOS-cocos2d-X 游戏开发之十五】Cocos2