Home>

Creating a widget.
Change the newly generated widget in AndroidStudio a little and process it when clicked
I wrote it, but onRead doesn't run when I click it. Is there a problem somewhere?

Thanks for your consideration.

Applicable source code
<? xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns: android = "http://schemas.android.com/apk/res/android"
    package = "com.example.widgetsample">
    
        <receiver android: name = ". NewAppWidget">
            <intent-filter>
                
                 // Add here
            </intent-filter>
            <meta-data
                android: name = "android.appwidget.provider"
                android: resource = "@ xml/new_app_widget_info" />
        </receiver>
    </application>
</manifest>
class NewAppWidget: 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 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
    }
    override fun onReceive (context: Context ?, intent: Intent?) {
        super.onReceive (context, intent)
        // add here
        Log.i ("Log output", "onReceive")
    }
}
internal fun updateAppWidget (context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int) {
    val widgetText = context.getString (R.string.appwidget_text)
    // Construct the RemoteViews object
    val views = RemoteViews (context.packageName, R.layout.new_app_widget)
    views.setTextViewText (R.id.appwidget_text, widgetText)
    // add here
    val intent = Intent ("CLICK_WIDGET")
    val pIntent = PendingIntent.getBroadcast (context, appWidgetId, intent, 0)
    views.setOnClickPendingIntent (R.id.appwidget_text, pIntent)
    // Instruct the widget manager to update the widget
    appWidgetManager.updateAppWidget (appWidgetId, views)
}

OnReceive doesn't run when I click, but it runs when I change the size of the widget.

Supplemental information (FW/tool version etc.)

API 26