我有onUpdate和onReceive for my widget他们工作正常。但我的问题是,当我将小部件从小部件安装列表拖到Android主屏幕时,它们是否被触发,以防止此行为?
这是我的方法:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.e(TAG, "onUpdate()");
remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
watchWidget = new ComponentName(context, WidgetProvider.class);
Intent intentClick = new Intent(context, WidgetProvider.class);
intentClick.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, "" + appWidgetIds[0]);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetIds[0], intentClick, 0);
remoteViews.setOnClickPendingIntent(R.id.img_btn, pendingIntent);
remoteViews.setInt(R.id.img_btn, "setBackgroundResource", R.drawable.play);
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Log.e(TAG, "onReceive()");
Bundle extras = intent.getExtras();
if (extras != null) {
remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
//If isPlaying
if (isPlaying) {
//setBackground as PLAY
remoteViews.setInt(R.id.img_btn, "setBackgroundResource", R.drawable.play);
isPlaying = false;
Intent i = new Intent(context, MediaPlayerService.class);
i.putExtra("command", "stopSong");
context.startService(i);
//If NOT playing
} else {
//setBackground as STOP
remoteViews.setInt(R.id.img_btn, "setBackgroundResource", R.drawable.stop);
isPlaying = true;
Intent i = new Intent(context, MediaPlayerService.class);
i.putExtra("command", "playRandomSong");
context.startService(i);
}
watchWidget = new ComponentName(context, WidgetProvider.class);
(AppWidgetManager.getInstance(context)).updateAppWidget(watchWidget, remoteViews);
}
}
答案 0 :(得分:2)
我添加了intentClick.setAction(WIDGET_CLICKED);
,只有在点击小工具时才会触发。
public class WidgetProvider extends AppWidgetProvider {
private static final String TAG = WidgetProvider.class.getSimpleName();
private static String WIDGET_CLICKED = "widget_clicked";
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.e(TAG, "onUpdate()");
remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
watchWidget = new ComponentName(context, WidgetProvider.class);
Intent intentClick = new Intent(context, WidgetProvider.class);
intentClick.setAction(WIDGET_CLICKED);
intentClick.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, "" + appWidgetIds[0]);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetIds[0], intentClick, 0);
remoteViews.setOnClickPendingIntent(R.id.img_btn, pendingIntent);
remoteViews.setInt(R.id.img_btn, "setBackgroundResource", R.drawable.play);
appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Log.e(TAG, "onReceive()");
Bundle extras = intent.getExtras();
//If WIDGET_CLICKED
if (extras != null && intent.getAction().equals(WIDGET_CLICKED)) {
remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
//If isPlaying
if (isPlaying) {
//setBackground as PLAY
remoteViews.setInt(R.id.img_btn, "setBackgroundResource", R.drawable.play);
isPlaying = false;
Intent i = new Intent(context, MediaPlayerService.class);
i.putExtra("command", "stopSong");
context.startService(i);
//If NOT playing
} else {
//setBackground as STOP
remoteViews.setInt(R.id.img_btn, "setBackgroundResource", R.drawable.stop);
isPlaying = true;
Intent i = new Intent(context, MediaPlayerService.class);
i.putExtra("command", "playRandomSong");
context.startService(i);
}
watchWidget = new ComponentName(context, WidgetProvider.class);
(AppWidgetManager.getInstance(context)).updateAppWidget(watchWidget, remoteViews);
}
}