HOWTO: Android Audio Widget
I've recently released a couple of audio widgets. Inspired by the Instant Rimshot and Sad Trombone sites, these are "single serving" widget.
For the "Family Fortunes" buzzer widget, you click the icon and you'll hear the famous "EURGH-ERRRR" noise.
I'm also selling a "Dramatic Sound Effect" Widget - download it by scanning this code. Go on, it's a mere 50p!
This is a quick tutorial showing how to build a sound playing widget in Android. It assumes you've already set up Eclipse and know how to create your own projects.
The Source Code
Here's the main Java source code - with comments.
package mobi.shkspr.android.ButtonWidget;
import mobi.shkspr.android.ButtonWidget.R;
//All the Androidy bits and bobs you need to import.
import android.media.*;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
public class ButtonWidget extends AppWidgetProvider {
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
//First up, the icon. I've created a file called h_yellow_x.png and placed it in /res/drawable
int drawableResourse = R.drawable.h_yellow_x;
//Set Up the widget
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
//Set the image which will appear on the screen
remoteViews.setImageViewResource(R.id.ImageView01, drawableResourse );
Intent active = new Intent(context, ButtonWidget.class);
active.setAction(ACTION_WIDGET_RECEIVER);
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.ImageView01, actionPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
// v1.5 fix that doesn't call onDelete Action
final String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
//The widget is being deleted off the desktop
final int appWidgetId = intent.getExtras().getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
this.onDeleted(context, new int[] { appWidgetId });
}
} else {
// check, if our Action was called
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
//Play the audio file
//The audio file is in /res/raw/ and is an OGG file
MediaPlayer mPlay = MediaPlayer.create(context, R.raw.ff);
mPlay.start();
} else {
// do nothing
}
super.onReceive(context, intent);
}
}
}
Also
The Manifest
The XML you'll need to get everything configured.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mobi.shkspr.android.ButtonWidget"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<!-- Broadcast Receiver that will process AppWidget updates -->
<receiver android:name=".ButtonWidget" android:label="@string/app_name">
<intent -filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<!-- Broadcast Receiver that will also process our self created action -->
<action android:name="my.package.name.ButtonWidget.ACTION_WIDGET_RECEIVER"/>
</intent>
<meta -data android:name="android.appwidget.provider" android:resource="@xml/widget_def" />
</receiver>
</application>
<uses -sdk android:minSdkVersion="3" />
</manifest>
Max says:
Youssef says: