从小部件列表中拖动时阻止小部件 onUpdate onReceive?

Prevent widget onUpdate onReceive while dragging it from widgets list?

我的小部件有 onUpdate 和 onReceive,它们工作正常。但我的问题是,当我将小部件从小部件安装列表拖到 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);
        }
    }

我添加了 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);
    }
}