Reputation: 11
i want to add a widget to the home screen , and when i click it , the open with dialog should pop up and open the link associated with the widget ... nothing happens when i click the widget , absolutely nothing
class MyAppWidgetProvider:AppWidgetProvider() {
companion object { lateinit var text:String }
override fun onReceive(context: Context, intent: Intent) {
Log.d("onReceive", intent.action.toString())
super.onReceive(context, intent)
if (intent.action == AppWidgetManager.ACTION_APPWIDGET_UPDATE) {
val appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS)
if (appWidgetIds != null && appWidgetIds.isNotEmpty()) {
// This is the ID of the newly created widget
updateWidget(context, appWidgetIds[0])
}
}
if (intent.action == "GETEDITTEXT") {
val edittext = intent.getStringExtra("EditText text")
text = edittext.toString()
}
if (intent.action == "OPENBROWSER") {
val random = Random
val color = Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256))
val remoteViews = RemoteViews(context.packageName, R.layout.widget_layout)
remoteViews.setTextColor(R.id.widget_text, color)
}
}
private fun updateWidget(context: Context, appWidgetId: Int) {
val views = RemoteViews(context.packageName, R.layout.widget_layout)
views.setTextViewText(R.id.widget_text, text)
val appWidgetManager = AppWidgetManager.getInstance(context)
appWidgetManager.updateAppWidget(appWidgetId, views)
}
override fun onUpdate(
context: Context?,
appWidgetManager: AppWidgetManager?,
appWidgetIds: IntArray?
) {
super.onUpdate(context, appWidgetManager, appWidgetIds)
if (appWidgetIds != null) {
for (appWidgetId in appWidgetIds) {
val views = RemoteViews(context?.packageName, R.layout.widget_layout)
// Intent to open the link
val intent = Intent(context, MyAppWidgetProvider::class.java).apply {
action = "OPENBROWSER"
data = Uri.parse(text)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
putExtra("appWidgetId", appWidgetId)
}
// Create a PendingIntent to be triggered when the TextView is clicked
val pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, PendingIntent.FLAG_IMMUTABLE)
// Set the PendingIntent to the TextView in the widget
views.setOnClickPendingIntent(R.id.widget_text, pendingIntent)
appWidgetManager?.updateAppWidget(appWidgetId, views)
}
}
}
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
val button:Button = findViewById(R.id.button)
button.setOnClickListener {
val intent = Intent(this, MyAppWidgetProvider::class.java).apply{
action = "GETEDITTEXT"
putExtra("EditText text", findViewById<EditText>(R.id.editTextText).text.toString())
}
sendBroadcast(intent)
addWidgetToHomeScreen()
}
}
private fun addWidgetToHomeScreen() {
val appWidgetManager = AppWidgetManager.getInstance(this)
val myProvider = ComponentName(this, MyAppWidgetProvider::class.java)
if (appWidgetManager.isRequestPinAppWidgetSupported) {
appWidgetManager.requestPinAppWidget(myProvider, null, null)
} else {
Toast.makeText(this, "Can't add widget.", Toast.LENGTH_SHORT).show()
}
}
}
i tried using pending intent , i tried all the life cycle methods , i tried to handle it in onReceive , nothing works
Upvotes: 1
Views: 55