我是Android Studio的新手,我的问题是我的小部件没有更新。我们说我改变了我的数据JSON。我已经阅读了一些线程,并说小部件的刷新时间间隔为30分钟。但我等了30分钟我的模拟器并没有改变。有人可以帮我解决这个问题吗?因为我希望我的小部件更新至少30分钟默认..
class TestWidget : AppWidgetProvider() {
private val httpClient = AsyncHttpClient()
private var title = String.toString()
private var imageUrl = String.toString()
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
// There may be multiple widgets active, so update all of them
httpClient.get(jsonUrl, object : JsonHttpResponseHandler() {
override fun onSuccess(statusCode: Int, headers: Array<out Header>, response: JSONArray) {
val json = response.getJSONObject(0)
title = json.getString("title")
imageUrl = json.getString("imageUrl")
val views = RemoteViews(context.packageName, R.layout.test_widget)
views.setTextViewText(R.id.text, title)
Picasso.with(context)
.load(imageUrl)
.into(views, R.id.image, appWidgetIds)
}
override fun onFailure(statusCode: Int, headers: Array<out Header>?, throwable: Throwable?, errorResponse: JSONObject?) {
println(throwable?.localizedMessage)
}
})
for (appWidgetId in appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId)
}
}
override fun onEnabled(context: Context) {
// Enter relevant functionality for when the first widget is created
}
override fun onDisabled(context: Context) {
// Enter relevant functionality for when the last widget is disabled
}
companion object {
internal fun updateAppWidget(context: Context, appWidgetManager: AppWidgetManager,
appWidgetId: Int) {
// Construct the RemoteViews object
val views = RemoteViews(context.packageName, R.layout.test_widget)
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views)
}
}
}
答案 0 :(得分:0)
OH,我想我找到了解决方案。
我的Widget Info XML上有一个android:updatePeriodMillis
。这应该可以解决问题。