Не могу передать данные в виджет
У меня есть приложение которое с сервера получает информацию и добавляет ее на экран, с этим все хорошо. Нужно чтобы приложение добавляла эту же информацию в виджет, информация получается приложение выводит информацию в тост, но не отображается на виджете.
class NewAppWidget : AppWidgetProvider() {
@RequiresApi(Build.VERSION_CODES.O)
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
// There may be multiple widgets active, so update all of them
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
}
}
private fun xmlParse(str:String, views: RemoteViews, context: Context)
{
val parser= XmlPullParserFactory.newInstance().newPullParser()
parser.setInput(str.reader())
var eventType=parser.eventType
val tags= Stack<String>()
var RecordCode=0
var Buy=""
var Sell=""
var Date=""
var Records= emptyList<Record>().toMutableList()
while (eventType!= XmlPullParser.END_DOCUMENT)
{
when(eventType)
{
XmlPullParser.START_TAG->
{
tags.push(parser.name)
when(parser.name)
{
"Record"->
{
RecordCode=parser.getAttributeValue("","Code").toInt()
Date=parser.getAttributeValue("","Date")
}
}
}
XmlPullParser.TEXT->
{
when(tags.peek())
{
"Buy"->
{
Buy=parser.text
}
"Sell"->
{
Sell=parser.text
}
}
}
XmlPullParser.END_TAG->
{
when(parser.name)
{
"Record"->
{
Records.add(Record(RecordCode,Date,Buy,Sell))
}
}
}
}
eventType=parser.next()
}
Toast.makeText(context,Records[Records.count()-1].RecordDate , Toast.LENGTH_SHORT).show()
views.setTextViewText(R.id.appwidget_text, "77")
}
@RequiresApi(Build.VERSION_CODES.O)
private fun GetMetal(context: Context, views: RemoteViews)
{
val format= DateTimeFormatter.ofPattern("dd/MM/yyyy")
val date= LocalDateTime.now()
val dateTime2= date.format(format)
var period = Period.of(0, 0, 2)
val dateTime1=date.minus(period).format(format)
val url="http://www.cbr.ru/scripts/xml_metall.asp?date_req1=$dateTime1&date_req2=$dateTime2"
val queue= Volley.newRequestQueue(context)
val stringRequest= StringRequest(
Request.Method.GET,url,
{response->xmlParse(response, views, context)
Toast.makeText(context, "ii", Toast.LENGTH_SHORT).show()
}, { Toast.makeText(context, "Ошибка на сервере", Toast.LENGTH_SHORT).show()})
queue.add(stringRequest)
}
private fun g(views: RemoteViews)
{
views.setTextViewText(R.id.appwidget_text,"54545")
}
@RequiresApi(Build.VERSION_CODES.O)
internal fun updateAppWidget(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetId: Int
) {
val widgetText = context.getString(R.string.appwidget_text)+"5"
val widgetText2 = context.getString(R.string.appwidget_text)+"2"
val widgetText3 = context.getString(R.string.appwidget_text)+"3"
// Construct the RemoteViews object
val views = RemoteViews(context.packageName, R.layout.new_app_widget)
// views.setTextViewText(R.id.appwidget_text, widgetText)
views.setTextViewText(R.id.appwidget_text2,widgetText2)
views.setTextViewText(R.id.appwidget_text3,widgetText3)
GetMetal(context, views)
views.setTextViewText(R.id.appwidget_text3, widgetText)
//g(views)
// Instruct the widget manager to update the widget
sleep(300)
appWidgetManager.updateAppWidget(appWidgetId, views)
Нужно чтобы виджет обновлялся каждый день, тоже не совсем понимаю, как это сделать.
Источник: Stack Overflow на русском