'Android Widget: onReceive not being called for Pending Intent Broadcast
Objective: Attempting to play an audio file when the Widget Button is clicked.
Problem: The button is getting added, but after pressing the button, the onReceive method doesn't get called. Is there anything that I am missing? Thanks.
Here are the required files
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<receiver
android:name=".ButtonWidget"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/button_widget_info" />
</receiver>
</application>
Layout: - button_widget.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Widget.MyApplication.AppWidget.Container"
android:layout_width="172dp"
android:layout_height="172dp"
android:theme="@style/Theme.MyApplication.AppWidgetContainer">
<ImageView
android:id="@+id/activityButton"
android:layout_width="139dp"
android:layout_height="137dp"
android:clickable="true"
android:contentDescription="@string/appwidget_text"
android:cropToPadding="false"
android:src="@drawable/ic_cricket" />
</RelativeLayout>
Layout Provider
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/app_widget_description"
android:initialKeyguardLayout="@layout/button_widget"
android:initialLayout="@layout/button_widget"
android:minWidth="40dp"
android:minHeight="40dp"
android:previewImage="@drawable/icon"
android:previewLayout="@layout/button_widget"
android:resizeMode="horizontal|vertical"
android:targetCellWidth="1"
android:targetCellHeight="1"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen" />
Widget Java:
public class ButtonWidget extends AppWidgetProvider {
static String REFRESH_ACTION = AppWidgetManager.ACTION_APPWIDGET_UPDATE;
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
Intent intent = new Intent(context, ButtonWidget.class);
intent.setAction(REFRESH_ACTION);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.button_widget);
views.setOnClickPendingIntent(R.id.activityButton, pendingIntent);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
Toast.makeText(context, REFRESH_ACTION, Toast.LENGTH_SHORT).show();
Toast.makeText(context, intent.getAction(), Toast.LENGTH_SHORT).show();
if (REFRESH_ACTION.equals(intent.getAction())) {
Toast.makeText(context, "onReceive True", Toast.LENGTH_SHORT).show();
MediaPlayer media = MediaPlayer.create(context,R.raw.mediaFile);
media.setLooping(false);
media.start();
media.setOnCompletionListener(MediaPlayer::release);
} else {
Toast.makeText(context, "onReceive False", Toast.LENGTH_SHORT).show();
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|