Chinaunix首页 | 论坛 | 博客

分类: Android平台

2017-09-04 17:03:58

原文地址:android学习之AppWidget 作者:一生有你llx

一、 什么是AppWidget
      桌面上见到的那种一个个的小窗口,利用这个小窗口可以给用户提供一些方便快捷的操作,有点像快捷方式。。。
      
二、 与AppWidget相关的数据
    1、AppWidgetProviderInfo对象,为appWidget提供元数据,包括布局、更新频率等等,这个对象被定义在xml文件中
    2、appWidgetProvider,定义了appWidget的基本生命周期
三、创建一个AppWidget的方法
    1、定义AppWidgetProviderInfo对象
        在res/xml文件夹中定义一个名为example_appwidget_info.xml的文件
                            xmlns:android=""
               android:minWidth="294dp"
               android:minHeight="72dp"
               android:updatePeriodMillis="86400000"
                android:initialLayout="@layout/example_appwidget"
               />
    2、为AppWidget添加布局文件
                      android:id="@+id/widgetText"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:text="myWidget"
           />
    3、实现appWidgetProvider的方法
        onUpdate: 在大道指定更新时间后,或者用户想桌面添加appwidget时会调用
        onDelete: 当appWidget被删除时调用
        onEnabled: 当第一次创建appwidget时调用
        onDisabled:当最后一个appwidget被删除时调用
        onRecieve: 接收广播                  appwidget是依靠广播机制的
    4、在AndroidManifest中声明广播
       
            
                
           

                           android:name="android.appwidget.provider"
               android:resource="@xml/example_appwidget_info"
               />
       

四、AppWidget如何使用
     1、pendingIntent  用于appwidget和其他activity交换数据,appwidget和创建它的activity并不在同一个进程
     2、创建pendingIntent的方法:
        (1)、 getActivity()       //用于启动新的activity
        (2)、 getBroadcast()    //用于启动新的广播,广播会在onReceive方法中接收
        (3)、 getService()         //用于启动新的服务
    3、 remoteViews 表示一系列的view对象,这些view对象在另一个线程
    4、为remoteViews 添加监听器的方法 :setOnClickPendingIntent
五、源代码
1、MainActivity.java
    这里什么都不需要操作

点击(此处)折叠或打开

  1. public class MainActivity extends Activity
  2. {

  3.     @Override
  4.     protected void onCreate(Bundle savedInstanceState)
  5.     {
  6.         super.onCreate(savedInstanceState);
  7.         setContentView(R.layout.activity_main);
  8.     }

  9.     @Override
  10.     public boolean onCreateOptionsMenu(Menu menu)
  11.     {
  12.         // Inflate the menu; this adds items to the action bar if it is present.
  13.         getMenuInflater().inflate(R.menu.main, menu);
  14.         return true;
  15.     }

  16. }
2、myAppWidget.java
    这里操作AppWidget

点击(此处)折叠或打开

  1. public class appWidget extends AppWidgetProvider
  2. {
  3.     //定义一个常量,用于自定义action
  4.     private static final String UPDATE_ACTION = "appwidget.UPDATE_APPWIDGET";
  5.     @Override
  6.     public void onDeleted(Context context, int[] appWidgetIds)
  7.     {
  8.         // TODO Auto-generated method stub
  9.         super.onDeleted(context, appWidgetIds);
  10.     }

  11.     @Override
  12.     public void onDisabled(Context context)
  13.     {
  14.         // TODO Auto-generated method stub
  15.         super.onDisabled(context);
  16.     }

  17.     @Override
  18.     public void onEnabled(Context context)
  19.     {
  20.         // TODO Auto-generated method stub
  21.         super.onEnabled(context);
  22.     }

  23.     @Override
  24.     public void onReceive(Context context, Intent intent)
  25.     {
  26.         // TODO Auto-generated method stub
  27.         String action = intent.getAction();
  28.         if(UPDATE_ACTION.equals(action))
  29.         {
  30.             System.out.println(action);
  31.             //得到remoteViews
  32.             RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
  33.             //给remoteViews设置动作
  34.             remoteViews.setTextViewText(R.id.widgetText, "hello");
  35.             //创建AppWidgetManager
  36.             AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
  37.             //创建ComponentName
  38.             ComponentName compenentName = new ComponentName(context, appWidget.class);
  39.             appWidgetManager.updateAppWidget(compenentName, remoteViews);
  40.         }
  41.         else
  42.         {
  43.             super.onReceive(context, intent);    
  44.         }
  45.     }

  46.     @Override
  47.     public void onUpdate(Context context, AppWidgetManager appWidgetManager,
  48.             int[] appWidgetIds)
  49.     {
  50.         //每创建一个appwidget就会添加一个id
  51.         for(int i=0; i<appWidgetIds.length; i++)
  52.         {
  53.             System.out.println(appWidgetIds[i]);
  54.             //创建一个intent对象
  55.             Intent intent = new Intent(context, targetActivity.class);
  56.             //创建一个pendingIntent对象,利用getActivity创建,用于启动新的activity
  57.             PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
  58.             //创建remoteViews
  59.             RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
  60.             //为remoteViews绑定事件处理器,当发生click事件后执行pendingIntent
  61.             //第一个参数是被绑定的控件,第二个参数指定当事件发生时执行的pendingIntent
  62.             remoteViews.setOnClickPendingIntent(R.id.appWidgetBtn1, pendingIntent);
  63.             //更新appwidget
  64.             appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
  65.         }
  66.         
  67.         
  68.         for(int i=0; i<appWidgetIds.length; i++)
  69.         {
  70.             System.out.println(appWidgetIds[i]);
  71.             //创建一个intent对象
  72.             Intent intent = new Intent();
  73.             //设置action,这个action需要在androidManifest中声明
  74.             intent.setAction(UPDATE_ACTION);
  75.             //创建一个pendingIntent对象,利用getBroadcast创建,用于发送广播,发送的广播会在onReceive方法中接收
  76.             PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
  77.             //创建remoteViews
  78.             RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
  79.             //为remoteViews绑定事件处理器,当发生click事件后执行pendingIntent
  80.             //第一个参数是被绑定的控件,第二个参数指定当事件发生时执行的pendingIntent
  81.             remoteViews.setOnClickPendingIntent(R.id.appWidgetBtn2, pendingIntent);
  82.             //更新appwidget
  83.             appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
  84.         }
  85.         // TODO Auto-generated method stub
  86.         super.onUpdate(context, appWidgetManager, appWidgetIds);
  87.     
  88.     }
  89. }
4、example_appwidget_info.xml
    这个xml文件并不是布局文件,它是为AppWidget提供元数据

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <appwidget-provider
  3.     xmlns:android=""
  4.     android:minWidth="294dp"
  5.     android:minHeight="72dp"
  6.     android:updatePeriodMillis="86400000"
  7.     android:initialLayout="@layout/example_appwidget"
  8.     />
5、example_appwiget.xml
    这是AppWidget的布局文件

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical" >
  6.     
  7.     <TextView
  8.      android:id="@+id/widgetText"
  9.      android:layout_width="fill_parent"
  10.      android:layout_height="wrap_content"
  11.      android:text="myWidget"
  12.      android:background="#000000"
  13.      />
  14.     <Button
  15.      android:id = "@+id/appWidgetBtn1"
  16.      android:layout_width="fill_parent"
  17.      android:layout_height="wrap_content"
  18.      android:text="test Button1"
  19.      />
  20.     <Button
  21.      android:id = "@+id/appWidgetBtn2"
  22.      android:layout_width="fill_parent"
  23.      android:layout_height="wrap_content"
  24.      android:text="test Button2"
  25.      />
  26. </LinearLayout>
6、activity_main.xml
    这是MainActivity的布局文件

点击(此处)折叠或打开

  1. <RelativeLayout xmlns:android=""
  2.     xmlns:tools=""
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:paddingBottom="@dimen/activity_vertical_margin"
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"
  7.     android:paddingRight="@dimen/activity_horizontal_margin"
  8.     android:paddingTop="@dimen/activity_vertical_margin"
  9.     tools:context=".MainActivity" >

  10.     <TextView
  11.         android:layout_width="wrap_content"
  12.         android:layout_height="wrap_content"
  13.         android:text="@string/hello_world" />

  14. </RelativeLayout>


阅读(2311) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~