十六.AppWidget创建与使用

一.如何创建一个AppWidget。
1.在工程的res文件夹下新建一个xml文件夹,并创建一个xml文件用于配置AppWidget的参数。
example_widget.xml文件代码
<!-- android:minWidth AppWidget的宽度 -->
<!-- android:updatePerionMillis AppWidget的更新时间,毫秒为单位 -->
<!-- android:initialLayout AppWidget的布局文件 -->
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="294dp"
    android:minHeight="72dp"
    android:updatePeriodMillis="86400000"
    android:initialLayout="@layout/example_widgetlayout"> 
</appwidget-provider>

2.在res的layout文件夹下新建一个布局文件,该文件是AppWidget的布局文件。
example_widgetlayout.xml文件代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
<TextView 
    android:id="@+id/apptext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/app_name"
    />


<Button android:id="@+id/MyButton" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="打开一个Activity"
    />
    
</LinearLayout>

4.创建一个类文件,该类文件继承AppWidgetProvider,并重写其中的onDeleted、onDisabled、onEnable、onReceive、onUpdate方法。
package paj.AppWidgetTest;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class AppWidgetTest extends AppWidgetProvider {

	/**
	 * 当AppWidget被从桌面上删除时调用该方法
	 */
	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		// TODO Auto-generated method stub
		super.onDeleted(context, appWidgetIds);
	}


	/**
	 * 当桌面上的最后一个AppWidget被删除时调用。
	 */
	@Override
	public void onDisabled(Context context) {
		// TODO Auto-generated method stub
		super.onDisabled(context);
	}


	/**
	 * 当AppWidget的实例被第一次创建时被调用
	 * 可以理解为当向桌面添加第一个该AppWidget时被调用,添加第二个第三个时不被调用
	 */
	@Override
	public void onEnabled(Context context) {
		// TODO Auto-generated method stub
		super.onEnabled(context);
	}


	/**
	 * 用于接收广播事件
	 */
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		super.onReceive(context, intent);
	}


	/**
	 * 当到达AppWidget配置文件中设置的更新时间时开始执行
	 * 当用户向桌面上添加AppWidget时执行该方法
	 */
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// TODO Auto-generated method stub
		
		super.onUpdate(context, appWidgetManager, appWidgetIds);
	}


}


5.在AndroidManifset.xml文件中注册一个receiver
在<application><application/>标签中添加如下代码
<receiver android:name="AppWidgetTest">
    <intent-filter>
       <!-- 定义intent-filter的action为android内置的常量 -->
       <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
    </intent-filter>
    <!-- 设置meta-data的android:name为android内置的常量 -->
    <!-- android:resource为AppWidget的桌面控件的布局文件 -->
    <meta-data android:name="android.appwidget.provider" android:resource="@xml/example_widget"/>
</receiver>

二.如何对AppWidget上的控件绑定监听事件
1.在AppWidget的布局文件中添加一个Button控件。
2.在onUpdate方法中添加代码。
3.创建一个循环,因为用户可能会在桌面中添加多个AppWidget,因此需要向不同的AppWidget绑定控件。
4.创建一个Intent对象,用来启动一个Activity
5.使用getActivity方法创建一个PendingIntent。
6.创建一个RemoteViews对象,使用它的构造方法传入程序名和AppWidget的布局文件ID
7.使用setOnClickPendingIntent方法绑定远程控件的监听事件,并传入要绑定控件的ID和PendingIntent对象
8.使用updateApWidget更新绑定事件,其中第一个参数为当前是第几个AppWidget,第二个参数为绑定的哪个远程控件
9.代码如下:
/**
	 * 当到达AppWidget配置文件中设置的更新时间时开始执行
	 * 当用户向桌面上添加AppWidget时执行该方法
	 */
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// TODO Auto-generated method stub
		for (int i = 0; i < appWidgetIds.length; i++) {
			//创建一个Intent对象,并设置要启动的Activity
			Intent intent = new Intent(context , AppWidget_TestActivity.class);
			//使用getActivty方法创建一个启动Activity的PendingIntent对象
			PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
			//获取远程的控件,第一个参数设置程序名称,第二个参数是设置AppWidget的布局文件
			RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_widgetlayout);
			//绑定远程控件的监听事件。第一个参数是需要绑定监听事件的控件,第二个参数是PendingInten对象。
			remoteViews.setOnClickPendingIntent(R.id.MyButton, pendingIntent);
			//更新AppWidget的绑定事件
			//第一个参数指定绑定到哪一个AppWidget
			//第二个参数是指定要更新哪一个远程控件
			appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
		}
		super.onUpdate(context, appWidgetManager, appWidgetIds);
	}


三.更新AoppWidget中的控件内容
1.在onUpdate中给Button按钮绑定一个单击事件。并绑定发送广播事件。
2.在onReceive中获取广播中的action。
3.判断action是否与软件中自定义的action相同。
4.如果相同就获取AppWidget的所有控件-->使用RemoteViews的set***方法设置控件上的内容
5.使用AppWidgetManager的getInstance方法获取AppWidgetManager的对象
6.使用ComponentName初始化一个AppWidget对象
7.更新AppWidget上的内容
8.onReceive需要保留super.onReceive(context, intent);否则将无法接收系统发送的广播
代码如下:
onUpdate中绑定按钮事件代码
@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
		// TODO Auto-generated method stub    
		 for (int i = 0; i < appWidgetIds.length; i++) {
		 //创建一个Intent对象,并绑定intent-filter
		 Intent intent = new Intent();
		 intent.setAction("paj.AppWidgetTest.UPDATE_APP_WIDGET");
			 
		 //使用getActivty方法创建一个启动Activity的PendingIntent对象
		 PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
		 //获取远程的控件,第一个参数设置程序名称,第二个参数是设置AppWidget的布局文件
		 RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.example_widgetlayout);
		 //绑定远程控件的监听事件。第一个参数是需要绑定监听事件的控件,第二个参数是PendingInten对象。
		 remoteViews.setOnClickPendingIntent(R.id.MyButton, pendingIntent);
		 //更新AppWidget的绑定事件
		 //第一个参数指定绑定到哪一个AppWidget
		 //第二个参数是指定要更新哪一个远程控件
		 appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
		 }
		super.onUpdate(context, appWidgetManager, appWidgetIds);
	}

onReceive中接收广播并更新AppWidget代码
/**
	 * 用于接收广播事件
	 */
	@Override
	public void onReceive(Context context, Intent intent) {
	        //调用父类的onReceive否则无法接收系统广播
		super.onReceive(context, intent);
		//获取广播的intent-filter
		String action = intent.getAction();
		//判断获取到得Intent-filter是否与程序自定义的intent-filter相同
		//使用equals是判断两个变量是否是对同一个变量的引用,而不是值是否相同
		if (ACTION_STRING.equals(action)) {
			//获取AppWidget的所有控件
			RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_widgetlayout);
			//设置远程控件的更新内容
			remoteViews.setTextViewText(R.id.apptext, "测试");
			//更新AppWidget需要使用AppWidgetManager对象,所以可以使用AppWidgetManager的getInstance方法获取
			AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
			//ComponentName代表AppWidget的对象
			ComponentName componentName = new ComponentName(context, AppWidgetTest.class);
			//使用updateAppWidget更新AppWidget
			appWidgetManager.updateAppWidget(componentName, remoteViews);
		}
	}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值