Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to implement Broadcast receiver on Button click event. when Button will be click. it will broadcast &amp; receiver catch this action. Implement your get data from DB in Broadcast Receiver.</p> <p>Check this <a href="http://javatechig.com/android/app-widgets-example-in-android/" rel="nofollow">Tutorial</a> . </p> <p>Put</p> <pre><code>onUpdate() { RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetview); remoteViews.setOnClickPendingIntent(R.id.toggleButton1, buildButtonPendingIntent(context)); } public static PendingIntent buildButtonPendingIntent(Context context) { Intent intent = new Intent(); intent.setAction("pl.looksok.intent.action.CHANGE_TEXT"); return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); } </code></pre> <p><strong>EDIT</strong></p> <pre><code> public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) { ComponentName myWidget = new ComponentName(context, MyWidgetProvider.class); AppWidgetManager manager = AppWidgetManager.getInstance(context); manager.updateAppWidget(myWidget, remoteViews); } </code></pre> <p>Now , Register it in manifest</p> <pre><code> &lt;receiver android:name=".MyWidgetIntentReceiver" android:label="widgetBroadcastReceiver" &gt; &lt;intent-filter&gt; &lt;action android:name="pl.looksok.intent.action.CHANGE_TEXT" /&gt; &lt;/intent-filter&gt; &lt;meta-data android:name="android.appwidget.provider" android:resource="@xml/demo_widget_provider" /&gt; &lt;/receiver&gt; </code></pre> <p>Now MyWidgetIntentReceiver</p> <pre><code>public class MyWidgetIntentReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals("pl.looksok.intent.action.CHANGE_TEXT")){ updateWidgetPictureAndButtonListener(context); } } public static void updateWidgetPictureAndButtonListener(Context context) { RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetview); // ..Get Text from DB // remoteViews.setTextViewText(R.id.widgettext, "...text"); //REMEMBER TO ALWAYS REFRESH YOUR BUTTON CLICK LISTENERS!!! remoteViews.setOnClickPendingIntent(R.id.toggleButton1, MyWidgetProvider.buildButtonPendingIntent(context)); MyWidgetProvider.pushWidgetUpdate(context.getApplicationContext(), remoteViews); } } </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