ANDROID_MARS学习笔记_S02_006_APPWIDGET3_AppWidget发送广播及更新AppWidget

一、简介

ANDROID_MARS学习笔记_S02_006_APPWIDGET3_AppWidget发送广播及更新AppWidget

ANDROID_MARS学习笔记_S02_006_APPWIDGET3_AppWidget发送广播及更新AppWidget

ANDROID_MARS学习笔记_S02_006_APPWIDGET3_AppWidget发送广播及更新AppWidget

二、代码
1.xml
(1)example_appwidget.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"
>
<TextView
android:id="@+id/widgetTextId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="firstWidgetText"
android:background="#000000"
/>
<ImageView
android:id="@+id/imageId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/xiao"
/>
<Button
android:id="@+id/widgetButtonId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="测试用按钮"
/>
</LinearLayout>

(2)AndroidManifest.xml

增加action类型以接收广播

  <receiver android:name="ExampleAppWidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
<action android:name="tony.appwidget03.UPDATE_APP_WIDGET"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/example_appwidget_info"/> </receiver>

2.java
(1)ExampleAppWidgetProvider.java

 package com.appwidget;

 import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews; public class ExampleAppWidgetProvider extends AppWidgetProvider{ //定义一个常量字符串,该常量用于命名Action
private static final String UPDATE_ACTION = "tony.appwidget03.UPDATE_APP_WIDGET"; // 使按钮被click时发送广播
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
//创建一个Intent对象
Intent intent = new Intent();
//为Intent对象设置Action
intent.setAction(UPDATE_ACTION);
//使用getBroadcast方法,得到一个PendingIntent对象,当该对象执行时,会发送一个广播
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
remoteViews.setOnClickPendingIntent(R.id.widgetButtonId, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
} /**
* 接收广播,如果查UPDATE_ACTION,则更新文本和图片
* @param context
* @param intent
*/
@Override
public void onReceive(Context context, Intent intent) {
System.out.println(UPDATE_ACTION + "----------->" + intent.getAction());
if(UPDATE_ACTION.equals(intent.getAction())) {
//获取代表此appwidget控件的remoteviews
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
//用remoteviews设置文本、图片
remoteViews.setImageViewResource(R.id.imageId, R.drawable.ku);
remoteViews.setTextViewText(R.id.widgetTextId, "更新的文本");
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName componentName = new ComponentName(context, ExampleAppWidgetProvider.class);
appWidgetManager.updateAppWidget(componentName, remoteViews);
} else {
super.onReceive(context, intent);
}
} /*@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) { //每个widget都有id
System.out.println("onupdate");
for(int i = 0; i < appWidgetIds.length ; i++) {
System.out.println(appWidgetIds[i]);
//创建一个Intent对象
Intent intent = new Intent(context, TargetActivity.class);
//创建一个PendingIntent
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
//为按钮绑定事件处理器
//第一个参数用来指定被绑定处理器的控件的ID
//第二个参数用来指定当事件发生时,哪个PendingIntent将会被执行
remoteViews.setOnClickPendingIntent(R.id.widgetButtonId, pendingIntent);
//更新AppWidget
//第一个参数用于指定被更新AppWidget的ID
appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}*/ @Override
public void onDeleted(Context context, int[] appWidgetIds) {
System.out.println("onDeleted");
super.onDeleted(context, appWidgetIds);
} @Override
public void onDisabled(Context context) {
System.out.println("onDisabled");
super.onDisabled(context);
} @Override
public void onEnabled(Context context) {
System.out.println("onEnabled");
super.onEnabled(context);
} }

(2).java

上一篇:python基础:交互式解释器


下一篇:SharDingJDBC-4.0.0-RC1按月水平分表