关键词 widget,AppWidgetID,桌面,小部件,Android,安卓,点击,Action
主要是intent传递时候不能用 intent.putExtra 来传递要保留的数据,而是直接用intent.setAction来传递
package hello.world.myapplication;
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.net.Uri;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.Toast;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import static android.content.ContentValues.TAG;
/**
* Implementation of App Widget functionality.
*/
public class NewAppWidget extends AppWidgetProvider {
private static final String CLICK_ACTION="callChangePic";
void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
views.setTextViewText(R.id.appwidget_text, ""+appWidgetId);
views.setOnClickPendingIntent(R.id.appwidget_text,getPendingSelfIntent(context,CLICK_ACTION,appWidgetId));
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
//小部件被添加时或者每次小部件更新时都会调用一次该方法,,每
// 个周期小部件都会自动更新一次,不是点击的时候更新,
// 而是到指定配置文件时间的时候才更新
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onEnabled(Context context) {
// Enter relevant functionality for when the first widget is created
}
@Override
public void onDisabled(Context context) {
// Enter relevant functionality for when the last widget is disabled
}
protected PendingIntent getPendingSelfIntent(Context context, String action, int appWidgetId) {
Intent intent = new Intent(context, getClass());
intent.setAction(CLICK_ACTION+String.valueOf(appWidgetId));
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
String action = intent.getAction();
if (action.startsWith(CLICK_ACTION))
{
int widgetId = Integer.parseInt(action.substring(CLICK_ACTION.length()));
Toast.makeText(context,"当前点击的ID__"+widgetId,Toast.LENGTH_LONG).show();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
}
}
}
下面这种也可以点击显示当前ID号,不过问题是新建时候也会跑进去一次,android:updatePeriodMillis时间到了,又跑进去一次
package com.jessicathornsby.widget;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.widget.RemoteViews;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import java.text.DateFormat;
import java.util.Date;
import android.widget.Toast;
public class NewAppWidget extends AppWidgetProvider {
static void updateAppWidget(Context context,
AppWidgetManager appWidgetManager,
int appWidgetId) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
Intent intentUpdate = new Intent(context, NewAppWidget.class);
intentUpdate.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
int[] idArray = new int[]{appWidgetId};
intentUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, idArray);
PendingIntent pendingUpdate = PendingIntent.getBroadcast(
context, appWidgetId, intentUpdate,
PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.update, pendingUpdate);
//Request that the AppWidgetManager updates the application widget, by passing widgetId and the RemoteViews object//
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
Toast.makeText(context, appWidgetId+"ID" +
"==Widget has been updated! ", Toast.LENGTH_SHORT).show();
}
}
}