添加小部件后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单击小部件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)