当onDatasetChanged方法包含异步网络调用Android时,Widget ListView不会刷新

Jas*_*son 6 android widget kotlin rx-java rx-java2

我想将互联网上的汽车列表加载到Widget中的ListView中.当我在onDatasetChanged方法中请求汽车时,它不会更新列表.然而,当我手动点击我的刷新按钮时,它会刷新它.可能是什么问题呢?为了提供尽可能多的信息,我将完整的源代码放入ZIP文件中,该文件可以解压缩并在Android Studio中打开.我也将它嵌入下面.注意:我使用RxJava作为异步请求,代码是用Kotlin编写的,尽管这个问题不是每个语言都绑定的.

CarsWidgetProvider

class CarsWidgetProvider : AppWidgetProvider() {
    override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
        Log.d("CAR_LOG", "Updating")
        val widgets = appWidgetIds.size

        for (i in 0 until widgets) {
            Log.d("CAR_LOG", "Updating number $i")
            val appWidgetId = appWidgetIds[i]

            // Get the layout
            val views = RemoteViews(context.packageName, R.layout.widget_layout)
            val otherIntent = Intent(context, ListViewWidgetService::class.java)
            views.setRemoteAdapter(R.id.cars_list, otherIntent)

            // Set an onclick listener for the action and the refresh button
            views.setPendingIntentTemplate(R.id.cars_list, getPendingSelfIntent(context, "action"))
            views.setOnClickPendingIntent(R.id.refresh, getPendingSelfIntent(context, "refresh"))

            // Tell the AppWidgetManager to perform an update on the current app widget
            appWidgetManager.updateAppWidget(appWidgetId, views)
        }
    }

    private fun onUpdate(context: Context) {
        val appWidgetManager = AppWidgetManager.getInstance(context)
        val thisAppWidgetComponentName = ComponentName(context.packageName, javaClass.name)
        val appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidgetComponentName)
        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.cars_list)
        onUpdate(context, appWidgetManager, appWidgetIds)
    }

    private fun getPendingSelfIntent(context: Context, action: String): PendingIntent {
        val intent = Intent(context, javaClass)
        intent.action = action
        return PendingIntent.getBroadcast(context, 0, intent, 0)
    }

    @SuppressLint("CheckResult")
    override fun onReceive(context: Context?, intent: Intent?) {
        super.onReceive(context, intent)
        if (context != null) {
            if ("action" == intent!!.action) {
                val car = intent.getSerializableExtra("car") as Car

                // Toggle car state
                Api.toggleCar(car)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe {
                            Log.d("CAR_LOG", "We've got a new state: $it")
                            car.state = it
                            onUpdate(context)
                        }

            } else if ("refresh" == intent.action) {
                onUpdate(context)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

ListViewWidgetService

class ListViewWidgetService : RemoteViewsService() {
    override fun onGetViewFactory(intent: Intent): RemoteViewsService.RemoteViewsFactory {
        return ListViewRemoteViewsFactory(this.applicationContext, intent)
    }
}

internal class ListViewRemoteViewsFactory(private val context: Context, intent: Intent) : RemoteViewsService.RemoteViewsFactory {
    private var cars: ArrayList<Car> = ArrayList()

    override fun onCreate() {
        Log.d("CAR_LOG", "Oncreate")
        cars = ArrayList()
    }

    override fun getViewAt(position: Int): RemoteViews {
        val remoteViews = RemoteViews(context.packageName, R.layout.widget_list_item_car)

        // Get the current car
        val car = cars[position]
        Log.d("CAR_LOG", "Get view at $position")
        Log.d("CAR_LOG", "Car: $car")

        // Fill the list item with data
        remoteViews.setTextViewText(R.id.title, car.title)
        remoteViews.setTextViewText(R.id.description, car.model)
        remoteViews.setTextViewText(R.id.action, "${car.state}")
        remoteViews.setInt(R.id.action, "setBackgroundColor",
                if (car.state == 1) context.getColor(R.color.colorGreen) else context.getColor(R.color.colorRed))

        val extras = Bundle()
        extras.putSerializable("car", car)

        val fillInIntent = Intent()
        fillInIntent.putExtras(extras)

        remoteViews.setOnClickFillInIntent(R.id.activity_chooser_view_content, fillInIntent)
        return remoteViews
    }

    override fun getCount(): Int {
        Log.d("CAR_LOG", "Counting")
        return cars.size
    }

    @SuppressLint("CheckResult")
    override fun onDataSetChanged() {
        Log.d("CAR_LOG", "Data set changed")

        // Get a token
        Api.getToken()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe { token ->
                    if (token.isNotEmpty()) {
                        Log.d("CAR_LOG", "Retrieved token")
                        DataModel.token = token

                        // Get the cars
                        Api.getCars()
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe {
                                    cars = it as ArrayList<Car>

                                    Log.d("CAR_LOG", "Found cars: $cars")
                                }
                    } else {
                        Api.getCars()
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe {
                                    cars = it as ArrayList<Car>

                                    Log.d("CAR_LOG", "Found cars: $cars")
                                }
                    }
                }
    }

    override fun getViewTypeCount(): Int {
        Log.d("CAR_LOG", "Get view type count")
        return 1
    }

    override fun getItemId(position: Int): Long {
        Log.d("CAR_LOG", "Get item ID")
        return position.toLong()
    }

    override fun onDestroy() {
        Log.d("CAR_LOG", "On Destroy")
        cars.clear()
    }

    override fun hasStableIds(): Boolean {
        Log.d("CAR_LOG", "Has stable IDs")
        return true
    }

    override fun getLoadingView(): RemoteViews? {
        Log.d("CAR_LOG", "Get loading view")
        return null
    }
}
Run Code Online (Sandbox Code Playgroud)

的DataModel

object DataModel {
    var token: String = ""
    var currentCar: Car = Car()
    var cars: ArrayList<Car> = ArrayList()

    init {
        Log.d("CAR_LOG", "Creating data model")
    }

    override fun toString(): String {
        return "Token : $token \n currentCar: $currentCar \n Cars: $cars"
    }
}
Run Code Online (Sandbox Code Playgroud)

汽车

class Car : Serializable {
    var id : String = ""
    var title: String = ""
    var model: String = ""
    var state: Int = 0
}
Run Code Online (Sandbox Code Playgroud)


编辑

在我收到一些答案之后,我开始找出一个临时修复,它在AsyncTask上调用.get(),这使它同步

@SuppressLint("CheckResult")
override fun onDataSetChanged() {
    cars = GetCars().execute().get() as ArrayList<Car>
}

class GetCars : AsyncTask<String, Void, List<Car>>() {
    override fun doInBackground(vararg params: String?): List<Device> {
        return Api.getCars().blockingFirst();
    }
}
Run Code Online (Sandbox Code Playgroud)

Hey*_*lex 6

正如@ferini 所说,你的 rx 链是一个异步调用,所以当你调用时实际发生了什么appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.cars_list)在此处输入图片说明

如您所见,在将调用notifyAppWidgetViewDataChanged() onDataSetChange() 之后,但您的调用是异步的,这就是 RemoteViewFactory 进一步访问 FetchMetaData(检查图像)的原因。因此,在第一次启动小部件时,您将汽车初始化为 ArrayList(),这就是您的 ListView 为空的原因。当您发送刷新操作时,您的汽车不为空,因为您在 onDataSetChange() 中启动的异步调用已经获取数据并填充您的汽车,这就是为什么在刷新操作时您通过上次异步调用填充 ListView。

如您所见, onDataSetChanged() 旨在从数据库或类似内容中填充一些数据。您有一些如何更新小部件的选项:

  1. 通过调用IntentService中的 notifyAppWidgetViewDataChanged() 保存在数据库中并在 onDataSetChange()中获取。处理完整更新并为带有可分割数据的列表视图适配器和 RemoteView 上的 setRemoteAdapter() 制定新的意图,因此在onCreate RemoteViewsService 中,将意图传递给 RemoteViewsFactory,您可以从意图获取汽车数组列表 并将其设置为 RemoteViewsFactory,在这种情况下,您不需要根本不需要 onDataSetChange() 回调。

  2. 只需在 onDataSetChange() 中进行所有同步(阻塞)调用,因为它不是 UI 线程。

您可以通过在onDataSetChanged() 中进行阻塞 rx 调用来轻松检查这一点,并且您的小部件将按您的需要工作。

override fun onDataSetChanged() {
    Log.d("CAR_LOG", "Data set changed")
    cars = Api.getCars().blockingFirst() as ArrayList<Car>
}
Run Code Online (Sandbox Code Playgroud)

更新: 实际上,如果您将在 RemoteViewFactory 回调中记录当前线程,您将看到只有 onCreate() 在主线程上,其他回调在绑定线程池的线程上工作。所以可以在 onDataSetChanged() 中进行同步调用,因为它不在 UI 线程上,所以进行阻塞调用是可以的,你不会得到 ANR