'How to send information from Activity to Widget in Kotlin?
My MainActivity class:
package com.example.fastview
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.GsonBuilder
import com.google.gson.annotations.SerializedName
import com.squareup.picasso.Picasso
import okhttp3.*
import java.io.IOException
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val submitButton = findViewById<Button>(R.id.btn_click_me)
val result = findViewById<TextView>(R.id.textView)
val image = findViewById<ImageView>(R.id.profilePhoto)
val imageView: ImageView = findViewById(R.id.profilePhoto)
submitButton.setOnClickListener {
val intent = Intent(this, SubCount::class.java)
val extras = Bundle()
extras.putString("payload", "test")
intent.putExtras(extras)
startService(intent)
println("payload pushed")
}
My SubCount (widget) class:
package com.example.fastview
import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.Context
import android.content.Intent
import android.content.Intent.getIntent
import android.os.Handler
import android.os.Looper
import android.widget.RemoteViews
import com.google.gson.GsonBuilder
import okhttp3.*
import java.io.IOException
/**
* Implementation of App Widget functionality.
*/
const val WIDGET_SYNC = "WIDGET_SYNC"
class SubCount : AppWidgetProvider() {
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 onReceive(context: Context, intent: Intent?) {
if(WIDGET_SYNC == intent?.action)
{
val appWidgetId = intent.getIntExtra("appWidgetId",0)
updateAppWidget(context, AppWidgetManager.getInstance(context), appWidgetId)
}
val bundle = intent?.extras
println(bundle)
val payload = intent?.getStringExtra("payload")
println("----- [ PAYLOAD BEGIN ] ------")
println(payload)
println("----- [ PAYLOAD END ] -------")
super.onReceive(context, intent)
}
}
internal fun updateAppWidget(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetId: Int
) {
val intent = Intent(context, SubCount::class.java)
intent.action = WIDGET_SYNC
intent.putExtra("appWidgetId", appWidgetId)
val pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0)
val views = RemoteViews(context.packageName, R.layout.sub_count)
views.setOnClickPendingIntent(R.id.iv_sync, pendingIntent)
appWidgetManager.updateAppWidget(appWidgetId, views)
}
In my MainActivity class, I have code (didn't include it here) that querys the YouTube API and retrieves information. I want to send that information to my widget. My widget currently has a sync buenter code heretton (WIDGET_SYNC) that (I believe) calls the updateAppWidget function. However, when I try to set the intent in MainActivity (payload), it's always null. In other words, the data extras doesn't contain what I sent in MainActivity. Any advice?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
