添加小部件后Android单击小部件不起作用

Android Click on Widget not working after adding widget(添加小部件后Android单击小部件不起作用)

本文介绍了添加小部件后Android单击小部件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

prefs.java

            Intent resultValue = new Intent();
            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
            setResult(RESULT_OK, resultValue);

            Context context = getApplicationContext();

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
            Intent configIntent = new Intent(context, Prefs.class);
            configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, configIntent, 
                                                            PendingIntent.FLAG_UPDATE_CURRENT);

            views.setOnClickPendingIntent(R.id.callbackwidget, pendingIntent);

            AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, views);

widget.xml

问题是当我添加小部件时它不可点击.重启手机后就正常了.同样在部署新的构建版本后,小部件是可点击的

Problem is when i add widget IT IS NOT CLICKABLE. after rebooting phone it is working ok. also after deploying new build version, widget IS CLICKABLE

有什么想法吗?

推荐答案

太棒了.NOT SENDING ACTION_APPWIDGET_UPDATE 出现问题.所以在关闭首选项之前我发送广播:

great. problem was on NOT SENDING ACTION_APPWIDGET_UPDATE. so before closing preferences i send broadcast:

Intent updateIntent = new Intent(this, CallBackWidget.class);
updateIntent.setAction("PreferencesUpdated");
updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
sendBroadcast(updateIntent);

在小部件的 onreceive 方法中,我检查广播

and in onreceive method of widget i check for broadcast

if ("PreferencesUpdated".equals(action)) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
appWidgetManager.updateAppWidget(appWidgetId, views);
int[] appWidgetIds = new int[] {appWidgetId};
onUpdate(context, appWidgetManager, appWidgetIds);
        }           

现在它就像一个魅力;)

now it works like a charm ;)

这篇关于添加小部件后Android单击小部件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:添加小部件后Android单击小部件不起作用

基础教程推荐