APK桌面小部件重启后无法点击

客户提供了一个apk,第一次在桌面添加APK小部件后能够点击跳转到客户的应用界面,但重启后就无法点击了。

跟踪代码发现,小部件的交互还是蛮复杂的,主要涉及:应用的AppWidgetProvider,Framework端的RemoteViews、AppWidgetHost、AppWidgetHostView,AppWidgetHost又与AppWidgetServiceImpl服务通过binder进行交互,Launcher端的LauncherAppWidgetHostView等。

重启后log:

21273 09-01 00:04:07.238642  2144  2144 W System.err: java.lang.Exception: Alex addAction
21274 09-01 00:04:07.240316  2144  2144 W System.err:     at android.widget.RemoteViews.addAction(RemoteViews.java:2386)
21275 09-01 00:04:07.241386  2144  2144 W System.err:     at android.widget.RemoteViews.setOnClickPendingIntent(RemoteViews.java:2721)
21288 09-01 00:04:07.253802  2144  2144 D Alex     : this=android.widget.RemoteViews@6b86247 addAction=android.widget.RemoteViews$SetOnClickPendingIntent@f891b9d

21292 09-01 00:04:07.273065  2144  2144 W System.err: java.lang.Exception: Alex addAction
21293 09-01 00:04:07.274573  2144  2144 W System.err:     at android.widget.RemoteViews.addAction(RemoteViews.java:2386)
21294 09-01 00:04:07.275508  2144  2144 W System.err:     at android.widget.RemoteViews.setCharSequence(RemoteViews.java:3091)
21295 09-01 00:04:07.276449  2144  2144 W System.err:     at android.widget.RemoteViews.setTextViewText(RemoteViews.java:2485)
21306 09-01 00:04:07.286124  2144  2144 D Alex     : this=android.widget.RemoteViews@c38c812 addAction=android.widget.RemoteViews$ReflectionAction@ee0d499

这里可以看到,客户apk new了2次RemoteViews对象,第一次android.widget.RemoteViews@6b86247设置了点击方法,第二次android.widget.RemoteViews@c38c812 设置text等方法时,没有设置点击方法,导致系统按没有点击方法的RemoteViews将其clone,后续Launcher updateAppWidget时,都clone的是没有setOnClickPendingIntent方法的RemoteViews,因此不再支持点击操作。

对比其他手机,这个Apk直接就不能点击,因此这个问题应该是客户apk自身问题,还是需要apk来修改。

以下是Android时钟小部件的update方法,每次update时,不仅更新了文字显示,而且每次都设置了setOnClickPendingIntent方法:

  private void updateClock(
            Context context, AppWidgetManager appWidgetManager, int appWidgetId, float ratio) {
        RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.digital_appwidget);

        // Launch clock when clicking on the time in the widget only if not a lock screen widget
        Bundle newOptions = appWidgetManager.getAppWidgetOptions(appWidgetId);
        if (newOptions != null &&
                newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY, -1)
                != AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD) {
            final Intent showClock = new Intent(HandleDeskClockApiCalls.ACTION_SHOW_CLOCK)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_FROM_WIDGET, true);
            final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, showClock, 0);
            widget.setOnClickPendingIntent(R.id.digital_appwidget, pendingIntent);
        }

        // Setup formats and font sizes
        refreshDate(context, widget, ratio);
        refreshAlarm(context, widget, ratio);
        WidgetUtils.setTimeFormat(context, widget, false /* showAmPm */, R.id.the_clock);
        WidgetUtils.setClockSize(context, widget, ratio);

        // Set up R.id.digital_appwidget_listview to use a remote views adapter
        // That remote views adapter connects to a RemoteViewsService through intent.

所以时钟小部件不存在这个问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值