客户提供了一个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.
所以时钟小部件不存在这个问题。

723

被折叠的 条评论
为什么被折叠?



