Please help with a very simple click of an Android widget button. Very upset.:(

I poured all the examples that I can find and I went through the official documentation from Google. All I am trying to do is create a toast notification by clicking on the widget.

I can make the widget (and button) appear, but I cannot get my intention to shoot. I have seen several examples that show this in different ways, but I just can't get it to work. I have not felt this helpless with coding for a long time, and I am starting to feel stupid.

This is what I have so far:

public class simpleclick extends AppWidgetProvider { /** Called when the activity is first created. */ @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { final int N = appWidgetIds.length; Toast.makeText(context, "doing on update", Toast.LENGTH_SHORT).show(); for (int i=0; i<N; i++) { int appWidgetId = appWidgetIds[i]; Intent intent = new Intent(context, simpleclick.class); intent.setAction("ham"); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main); views.setOnClickPendingIntent(R.id.Timm, pendingIntent); appWidgetManager.updateAppWidget(appWidgetId, views); } } //@Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Toast.makeText(context, "action is: " + intent.getAction(), Toast.LENGTH_SHORT).show(); super.onReceive(context, intent); } } 

My manifest:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tblabs.simpleclick" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <receiver android:name="simpleclick"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/simpleclick" /> </receiver> </application> <uses-sdk android:minSdkVersion="5" /> </manifest> 

I would be grateful for your help!

Thanks Wendy

+4
source share
2 answers

As bigstones wrote, you need to use translation. Code example:

 protected PendingIntent getPendingSelfIntent(Context context, String action) { Intent intent = new Intent(context, getClass()); intent.setAction(action); return PendingIntent.getBroadcast(context, 0, intent, 0); } views.setOnClickPendingIntent(R.id.Timm, getPendingSelfIntent(context, "ham")); 
+3
source

here is the full version:

 package com.automatic.widget; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.widget.RemoteViews; public class Widget extends AppWidgetProvider { private static final String SYNC_CLICKED = "automaticWidgetSyncButtonClick"; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { RemoteViews remoteViews; ComponentName watchWidget; remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout); watchWidget = new ComponentName(context, Widget.class); remoteViews.setOnClickPendingIntent(R.id.sync_button, getPendingSelfIntent(context, SYNC_CLICKED)); appWidgetManager.updateAppWidget(watchWidget, remoteViews); } @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub super.onReceive(context, intent); if (SYNC_CLICKED.equals(intent.getAction())) { AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); RemoteViews remoteViews; ComponentName watchWidget; remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout); watchWidget = new ComponentName(context, Widget.class); remoteViews.setTextViewText(R.id.sync_button, "TESTING"); appWidgetManager.updateAppWidget(watchWidget, remoteViews); } } protected PendingIntent getPendingSelfIntent(Context context, String action) { Intent intent = new Intent(context, getClass()); intent.setAction(action); return PendingIntent.getBroadcast(context, 0, intent, 0); } } 
+1
source

Source: https://habr.com/ru/post/1339280/


All Articles