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

标签 android android-layout android-widget android-appwidget android-event

我的小部件有 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);
    }
}

关于android - 从小部件列表中拖动时阻止小部件 onUpdate onReceive?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38484820/

相关文章:

android - RelativeLayout 不执行 "match_parent"和 alignBottom

android - 在透明形状上投下阴影?

Android 2D 滚动 ListView ?

android - 如何在 android 的谷歌地图上显示我的位置

android - QML 映射 : get coordinates when tap the screen

android - 为什么无法识别我的自定义布局文件?

android - 将形状添加到 LinearLayout Android

android - 更新大量小部件位图时避免 FAILED BINDER TRANSACTION 错误

android - youtube 视频不在 iframe 的 android webview 中播放

Android SQLite数据库异常