Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I tried to look over your code but did not find why it doesn't work for you except I found some other bugs in your code. Anyway I have built a quick working code that you can try out.</p> <p><strong>WidgetProvider.java</strong></p> <pre><code>package com.example.helloclickablewidget; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content.Intent; import android.widget.RemoteViews; import android.widget.Toast; public class WidgetProvider extends AppWidgetProvider { public static final String WIDGET_CLICKED = "com.example.helloclickablewidget.WIDGET_CLICKED"; @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (action != null &amp;&amp; action.equals(WIDGET_CLICKED)) { int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); Toast.makeText(context, "Widget clicked. ID: " + widgetId, Toast.LENGTH_LONG).show(); } else { super.onReceive(context, intent); } } @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { final int N = appWidgetIds.length; // Perform this loop procedure for each App Widget that belongs to this provider for (int i = 0; i &lt; N; i++) { int appWidgetId = appWidgetIds[i]; // Create an Intent to launch ExampleActivity Intent intent = new Intent(context, WidgetProvider.class); intent.setAction(WIDGET_CLICKED); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT); // Get the layout for the App Widget and attach an on-click listener // to the button RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget); views.setOnClickPendingIntent(R.id.text, pendingIntent); // Tell the AppWidgetManager to perform an update on the current app widget appWidgetManager.updateAppWidget(appWidgetId, views); } } } </code></pre> <p><strong>layout/widget.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/app_name" &gt; &lt;/TextView&gt; </code></pre> <p><strong>xml/widget.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialLayout="@layout/widget" android:minHeight="50dp" android:minWidth="50dp" android:updatePeriodMillis="86400000" &gt; &lt;/appwidget-provider&gt; </code></pre> <p><strong>AndroidManifest.xml</strong></p> <pre><code>&lt;manifest package="com.example.helloclickablewidget" android:versionCode="1" android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /&gt; &lt;application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" &gt; &lt;receiver android:name="WidgetProvider" android:exported="false"&gt; &lt;intent-filter&gt; &lt;action android:name="android.appwidget.action.APPWIDGET_UPDATE" /&gt; &lt;action android:name="com.example.helloclickablewidget.WIDGET_CLICKED"/&gt; &lt;/intent-filter&gt; &lt;meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" /&gt; &lt;/receiver&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload