Reputation: 25134
I am simply trying to make a widget with a + button, a - Button, and a progress bar. The bar should show the current volume and the plus and minus buttons should control the phone volume. Right now I am just trying to get the plus button to do its function (the progress bar already shows the right volume when the widget is initiated) but whenever I click the button I get a force close. I've done apps before but never widgets, and I haven't worked with intents much. Here is the code to the only class my Widget has:
package com.habosa.volumeslider;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.util.Log;
import android.widget.RemoteViews;
public class VolumeWidgetProvider extends AppWidgetProvider {
private AudioManager volume;
private int whichStream = AudioManager.STREAM_RING;
public void goRinger() {
whichStream = AudioManager.STREAM_RING;
}
public void goMedia() {
whichStream = AudioManager.STREAM_MUSIC;
}
public void goAlarm() {
whichStream = AudioManager.STREAM_ALARM;
}
public void goNotification() {
whichStream = AudioManager.STREAM_NOTIFICATION;
}
public void volumeUp() {
volume.adjustStreamVolume(whichStream, AudioManager.ADJUST_RAISE, 0);
}
public void volumeDown() {
volume.adjustStreamVolume(whichStream, AudioManager.ADJUST_LOWER, 0);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
volume = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int maxVol = volume.getStreamMaxVolume(whichStream);
int currentVol = volume.getStreamVolume(whichStream);
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.main);
remoteView.setProgressBar(R.id.ProgressBar01, maxVol, currentVol, false);
Intent plus = new Intent(context, VolumeWidgetProvider.class);
plus.putExtra("msg", "plus");
plus.setAction("BUTTONPRESS");
PendingIntent plusPendingIntent = PendingIntent.getBroadcast(context, 0, plus, 0);
remoteView.setOnClickPendingIntent(R.id.PlusButton, plusPendingIntent);
for (int appWidgetId : appWidgetIds) {
appWidgetManager.updateAppWidget(appWidgetId, remoteView);
}
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("BUTTONPRESS")) {
String msg = "null";
try {
msg = intent.getStringExtra("msg");
} catch (NullPointerException e) {
Log.e("Error", "msg = null");
}
if (msg.equals("plus")) {volumeUp();}
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
} else {
// do nothing
}
super.onReceive(context, intent);
}
}
and here is my manifest if that is relevant:
<?xml version="1.0" encoding="utf-8"?>
<receiver android:name="VolumeWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.habosa.volumeslider.VolumeWidgetProvider.BUTTONPRESS"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/volumewidgetproviderinfo" />
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
What am I doing wrong?
Upvotes: 0
Views: 1190
Reputation: 4093
Take a look at https://github.com/commonsguy/cw-andtutorials/tree/master/34-AdvAppWidget for a decent example to follow.
Note that the solution probably won't be to invoke the app widget provider class from the Intent, as you're apparently trying to do currently.
Upvotes: 1