Android学习笔记之AppWidget

来源:互联网 发布:华泰证券交易软件 mac 编辑:程序博客网 时间:2024/04/30 17:33
android桌面控件:


appwidget当中的View运行在Home Screen进程中,和我们的应用不在同一进程中。
AppWidgetProviderInfo对象:
为App Widget提供元数据,包括布局,更新频率等,这个对象被定义在xml文件中。


AppWidgetProvider:定义了AppWidget的基本生命周期函数


方法:onUpdate:在到达指定的更新时间之后后者当用户向桌面添加App Widget时会调用
该方法。
onDeleted:当App Widget被删除时,会调用该方法
onEnabled:当一个App Widget的实例第一次被创建时调用
onDisabled:当最后一个App Widget实例被删除后,会调用该方法。
onReveice:接收广播事件


步骤:
1,在res目录下新建目录xml,在里面新建文件widget_info.xml文件。
<?xml version="1.0" encoding="utf-8" ?>

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
android:minWidth="294dp"
android:minHeight="72dp"
android:updatePeriodMillis="86400000"

android:initialLayout="@layout/appwidget">
    
</appwidget-provider>


2,在layout下新建布局文件appwidget.xml文件:

<?xml version="1.0" encoding="utf-8" ?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<TextView 
    android:id="@+id/widgetTextld"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="firstWidgetText"
    android:background="#000000"
   ></TextView>
    
</LinearLayout>
3,AndroidManifest.xml文件中加receiver标签:
<receiver android:name="AppWidgetProviderTest">
            <intent-filter >
                <action 


android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            </intent-filter>
          <meta-data android:name="android.appwidget.provider"
              android:resource="@xml/appwidget_info" 


></meta-data>
     </receiver>
4,新建类:AppWidgetProviderTest.java,继承自AppWidgetProvider实现方法。
package com.gufengxiachen.android;


import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;


public class AppWidgetProviderTest extends AppWidgetProvider{
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
System.out.println("Deleted");
super.onDeleted(context, appWidgetIds);
}

@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
System.out.println("Enabled");
super.onEnabled(context);
}

@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
System.out.println("Disabled");
super.onDisabled(context);
}

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("Receive");
super.onReceive(context, intent);
}

@Override
public void onUpdate(Context context, AppWidgetManager 


appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
System.out.println("Update");
super.onUpdate(context, appWidgetManager, appWidgetIds);
}

}


PendingIntent:封装暂时不用的Intent,延迟执行的Intent。主要和Android特定的的远


程服务打交道(短信、通知、闹铃等),通常的应用无需使用。
构造PendingIntent对象:
静态方法:
getActivity(Content content,int requestCode,Intent intent,int flags);
getBroadcast(Content content,int requestCode,Intent intent,int flags);
getService(Content content,int requestCode,Intent intent,int flags);


RemoteViews:表示一系列的View对象。RemoteViews运行的对象在另外一个进程中。   
    
    
绑定appwidget的控件监听器:
remoteViews.setOnClickPendingIntent(R.id.widgetButtonld,pendingIntent);


主要代码:
for(int i=0;i<appWidgetIds.length;i++){
System.out.println(appWidgetIds[i]);
//定义intent对象
Intent intent = new Intent


(context,TargetActivity.class);
//定义PendingIntent对象,包装Intent对象
PendingIntent pendingIntent = 


PendingIntent.getActivity(context, 0, intent, 0);
//获取RemoteViews对象
RemoteViews remoteViews = new RemoteViews


(context.getPackageName(),R.layout.appwidget);
//控件添加监听器
remoteViews.setOnClickPendingIntent


(R.id.widgetbuttonld, pendingIntent);
//更新App Widget
appWidgetManager.updateAppWidget(appWidgetIds[i], 


remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);


}

接收来自AppWidget的广播:
1,在AndroidManifest.xml中为AppWidgetProvider注册新的intent-filter;
2,使用getBroadcast()方法创建一个PendingIntent;
3,为AppWidget中的控件注册处理器
4,在onReceive()方法中接收广播信息。