3

Apps had been allowed to request to pin an app widget on the current launcher since API level 26. As developers, this gave us a chance to make such request in our own Apps. However, the document says "When a request is denied by the user, the caller app will NOT get any response." Sometimes when there isn't enough room for a widget on the current launcher, the caller app will NOT get any response either.
How to differentiate between these two scenarios? Is there any callback function I can use? Is there any way I can get how much space left on the current launcher in my APP?

Jackie Li
  • 31
  • 2

1 Answers1

1
 private fun pickWidget()
    {
        
        val component = ComponentName(applicationContext,MyWidget::class.java)
        val manager = AppWidgetManager.getInstance(applicationContext)
        val widgetId = (1..999999).random()
        val intent = Intent(applicationContext,MyReceiver::class.java)
        val b = Bundle()
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,widgetId)
        val successCallback = PendingIntent.getBroadcast(applicationContext,101,intent,PendingIntent.FLAG_UPDATE_CURRENT)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            manager.requestPinAppWidget(component,b,successCallback)
        }

    }


class MyReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        Log.e(TAG, "onReceive: ")
        val b= intent.extras
        b?.let {
        val widgetId = b.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID)
        if(widgetId!=AppWidgetManager.INVALID_APPWIDGET_ID) {

          //if widget is placed succssfully
        }

        }
    }
}
Ihor Konovalenko
  • 1,298
  • 2
  • 16
  • 21