關(guān)鍵詞 widget仲锄,AppWidgetID,桌面呢岗,小部件,Android蛹尝,安卓后豫,點擊,Action
主要是intent傳遞時候不能用 intent.putExtra 來傳遞要保留的數(shù)據(jù)突那,而是直接用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);
}
//小部件被添加時或者每次小部件更新時都會調(diào)用一次該方法挫酿,,每
// 個周期小部件都會自動更新一次愕难,不是點擊的時候更新早龟,
// 而是到指定配置文件時間的時候才更新
@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,"當(dāng)前點擊的ID__"+widgetId,Toast.LENGTH_LONG).show();
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
}
}
}
下面這種也可以點擊顯示當(dāng)前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();
}
}
}