I am facing an issue with my Android widget. The onReceive method is automatically triggered when the widget is placed on the home screen, but it doesn't get called when I click the buttons with ids (button1, button2) or the ImageView with the ID @+id/imageRightOfButtons.
I have set up PendingIntent with setOnClickPendingIntent for 2 butttons and imageview in the onUpdate method, but the onReceive method is not getting invoked.
NewAppWidget.java
public class NewAppWidget extends AppWidgetProvider {
public static final String ACTION_BUTTON1_CLICK = "com.yourapp.ACTION_BUTTON1_CLICK";
public static final String ACTION_BUTTON2_CLICK = "com.yourapp.ACTION_BUTTON2_CLICK";
private final Long callBackDispatcherId=4944696197L;
FlutterEngine backgroundFlutterEngine;
DartExecutor executor;
BinaryMessenger binaryMessenger;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.d("NewAPPWidget", "latest onUpdate method");
for (int appWidgetId : appWidgetIds) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
Intent button1Intent = new Intent(context, NewAppWidget.class);
button1Intent.setAction(ACTION_BUTTON1_CLICK);
PendingIntent button1PendingIntent = PendingIntent.getBroadcast(context, 0, button1Intent,PendingIntent.FLAG_IMMUTABLE );
views.setOnClickPendingIntent(R.id.button1, button1PendingIntent);
Intent button2Intent = new Intent(context, NewAppWidget.class);
button2Intent.setAction(ACTION_BUTTON2_CLICK);
PendingIntent button2PendingIntent = PendingIntent.getBroadcast(context, 1, button2Intent,PendingIntent.FLAG_IMMUTABLE);
views.setOnClickPendingIntent(R.id.button2, button2PendingIntent);
views.setOnClickPendingIntent(R.id.imageRightOfButtons, getPendingIntent(context,appWidgetId));
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
Log.d("NewAPPWidget", " updateAppWidget");
Intent refreshIntent = new Intent(context, NewAppWidget.class);
refreshIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
refreshIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[]{appWidgetId});
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
String currentTime = DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date());
String currentDate = DateFormat.getDateInstance(DateFormat.SHORT).format(new Date());
views.setTextViewText(R.id.text_time, "last updated on "+currentTime);
views.setTextViewText(R.id.text_date, currentDate);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
public static PendingIntent getPendingIntent(Context context, int appWidgetId) {
Log.d("NewAPPWidget", "get PendingIntent");
Intent intent = new Intent(context, NewAppWidget.class);
intent.setAction("com.example.myapp.DATA_UPDATE");
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
return PendingIntent.getBroadcast(context, 2, intent, PendingIntent.FLAG_IMMUTABLE);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
int clickedButtonBackgroundColor = Color.parseColor("#FD681C");
int unclickedButtonBackgroundColor = Color.WHITE;
int unclickedButtonTextColor = Color.parseColor("#FD681C");
Log.d("NewAPPWidget", "OnReceive method");
Log.d("NewAPPWidget", "Intent name is "+intent.getAction());
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
if (ACTION_BUTTON1_CLICK.equals(intent.getAction())) {
Log.d("NewAPPWidget", "button1");
updateButtonBackground(context, views, R.id.button1, clickedButtonBackgroundColor, Color.WHITE);
updateButtonBackground(context, views, R.id.button2, unclickedButtonBackgroundColor, unclickedButtonTextColor);
} else if (ACTION_BUTTON2_CLICK.equals(intent.getAction())) {
Log.d("NewAPPWidget", "button2");
updateButtonBackground(context, views, R.id.button2, clickedButtonBackgroundColor, Color.WHITE);
updateButtonBackground(context, views, R.id.button1, unclickedButtonBackgroundColor, unclickedButtonTextColor);
}
else if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(intent.getAction()) || "com.example.myapp.DATA_UPDATE".equals(intent.getAction())) {
Log.d("kkkkkkkkkkkkkkkkkkkkkkkk","update hfdhgkjhjkdhgkdshgkjsdhkjsdhgkjsdhgsdhbgksdhbgkbgkjsdbgkdsjgbmjh");
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
fetchDataFromApp(context);
String arg1 = intent.getStringExtra("arg1");
String arg2 = intent.getStringExtra("arg2");
String arg3 = intent.getStringExtra("arg3");
int[] appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
if (appWidgetIds != null) {
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
updateAppWidgetWithData(context, arg1, arg2, arg3);
}
}
private void updateButtonBackground(Context context, RemoteViews views, int buttonId, int backgroundColor, int textColor) {
views.setInt(buttonId, "setBackgroundColor", backgroundColor);
Log.d("NewAPPWidget","updateButtonBackground");
if (buttonId == R.id.button2) {
Log.d("NewAPPWidget", "button2");
views.setTextColor(R.id.button1, Color.WHITE);
views.setTextColor(R.id.button2, textColor);
views.setInt(R.id.button1, "setBackgroundResource", R.drawable.inner_switch_button);
views.setInt(R.id.button2,"setBackgroundColor", android.R.color.transparent);
} else if (buttonId == R.id.button1) {
Log.d("NewAPPWidget", "button1");
views.setTextColor(R.id.button2, Color.WHITE);
views.setTextColor(R.id.button1, textColor);
views.setInt(R.id.button2, "setBackgroundResource", R.drawable.inner_switch_button);
views.setInt(R.id.button1,"setBackgroundColor", android.R.color.transparent);
}
AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(context, NewAppWidget.class), views);
}
private void updateAppWidgetWithData(Context context, String arg1, String arg2, String arg3) {
Log.d("NewAPPWidget","updateAppWidgetWithData");
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, NewAppWidget.class));
for (int appWidgetId : appWidgetIds) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
views.setTextViewText(R.id.text_arg1, arg1);
views.setTextViewText(R.id.text_arg2, arg2);
views.setTextViewText(R.id.text_arg3, arg3);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
public void fetchDataFromApp(@NonNull Context context){
/* Some Implementation */
}
}
AndroidManifest.xml
<receiver
android:name=".NewAppWidget"
android:exported="false">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.example.myapp.DATA_UPDATE" />
<action android:name="com.yourapp.ACTION_BUTTON1_CLICK" />
<action android:name="com.yourapp.ACTION_BUTTON2_CLICK" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/new_app_widget_info" />
</receiver>
new_app_widget.xml
<RelativeLayout
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/imageLeftOfOutbound"
android:foreground="@drawable/darker_grey_border"
android:layout_margin="6dp">
<Button
android:id="@+id/button1"
android:layout_width="70dp"
android:layout_height="25dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="5dp"
android:background="@drawable/inner_switch_button"
android:text="Outbound"
android:textColor="#FFFFFF"
android:textSize="10sp" />
<Button
android:id="@+id/button2"
android:layout_width="70dp"
android:layout_height="25dp"
android:layout_marginTop="5dp"
android:layout_toEndOf="@id/button1"
android:background="@android:color/transparent"
android:elevation="0dp"
android:text="Inbound"
android:textColor="#FD681C"
android:textSize="10sp" />
</RelativeLayout>
<ImageView
android:id="@+id/imageRightOfButtons"
android:layout_width="19dp"
android:layout_height="19dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="24dp"
android:clickable="true"
android:src="@drawable/proper_refresh_icon" />