Android Collection Widget无法在KitKat上运行

时间:2017-01-18 09:39:07

标签: android android-widget android-4.4-kitkat

我在Android上有一个带有ListView的集合小部件,它适用于19以上的版本。但是,使用KitKat(这是我们应用程序中支持的最低版本)它不起作用。

我可以看到小部件 - 当小部件更新时,有一个ImageView和一个带有另一个ImageView和animated-rotate的FrameLayout - 但是它有一个空的ListView,并且前面提到的ImageView的点击不起作用。 / p>

有什么特别的事情可以在KitKat上运行这个小部件吗?我错过了什么吗?

我意识到setRemoteAdapter工作,甚至调用了RemoteViewsService.RemoteViewsFactory,但它只是没有反映屏幕上的变化,也没有点击事件在静态视图上工作 - 不是ListView我'使用,甚至没有显示项目。

这就是我这样做的方式:

private fun buildWidget(appWidgetIds: IntArray, appWidgetManager: AppWidgetManager, context: Context) {
val remoteViews = getRemoveViews(context)

    // Set up the intent that starts the AppWidgetService, which will provide the views for this collection.
    for (i in appWidgetIds.iterator()) {
        val intent = Intent(context, AppWidgetService::class.java)

        // Add the app widget ID to the intent extras.
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, i)
        intent.action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
        intent.data = Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))

        // Check for a valid session in order to either show the positions list or a view to ask the user to log in
        if (userSession.hasValidSession()) {
            // Set up the RemoteViews object to use a RemoteViews adapter. This adapter connects to a RemoteViewsService  through the specified intent. This is how you populate the data.
            remoteViews.setRemoteAdapter(R.id.app_widget_portfolio_list, intent)

            // The empty view is displayed when the collection has no items. It should be in the same layout used to instantiate the RemoteViews object above.
            remoteViews.setEmptyView(R.id.app_widget_portfolio_list, R.id.app_widget_empty_view)
            setOpenAppOnClickPendingIntent(context, remoteViews, R.id.app_widget_empty_view)
            // Bind a click listener template for the contents of the weather list.  Note that we
            // need to update the intent's data if we set an extra, since the extras will be
            // ignored otherwise.
            val onClickIntent = Intent(ACTION_LIST_ITEM_CLICK, null, context, AppWidget::class.java)
            val onClickPendingIntent = PendingIntent.getBroadcast(context, number++, onClickIntent, PendingIntent.FLAG_UPDATE_CURRENT)
            remoteViews.setPendingIntentTemplate(R.id.app_widget_portfolio_list, onClickPendingIntent)

            val refreshPositionsIntent = Intent(ACTION_REFRESH, null, context, AppWidget::class.java)
            refreshPositionsIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, i)
            val refreshPositionsPendingIntent = PendingIntent.getBroadcast(context, 0, refreshPositionsIntent, 0)
            remoteViews.setOnClickPendingIntent(R.id.app_widget_refresh_button, refreshPositionsPendingIntent)

setOpenAppOnClickPendingIntent(context, remoteViews, R.id.app_widget_logo)
            appWidgetManager.partiallyUpdateAppWidget(i, remoteViews)
}

private fun setOpenAppOnClickPendingIntent(context: Context, remoteViews: RemoteViews, @IdRes viewId: Int) {
        val openAppIntent = MainActivity.newNavigateIntent(context, AppUrl.getHomeUrl())
        val loginPendingIntent = PendingIntent.getActivity(context, 0, openAppIntent, PendingIntent.FLAG_UPDATE_CURRENT)
        remoteViews.setOnClickPendingIntent(viewId, loginPendingIntent)
    }

到目前为止,我已尝试更改requestCode的{​​{1}}并使用随机数,但仍然无效。我还尝试PendingIntent.getBroadcast()代替appWidgetManager.updateAppWidget(i, remoteViews),但仍然没有运气。

提前多多感谢

0 个答案:

没有答案