I have a widget I am working on that controls a media player service. I have 4 buttons on my widget:
Previous
Play/Pause
Stop
Forward
The four buttons are assigned a pending intent which sends a Broadcast to the player service with the required action contained in it as an extra.
The problem I am having is that when I assign the pending intents to the buttons as below, they all seem to have the same broadcast value. For example, with the code below all 4 buttons seem to send Globals.PlaybackActions.SKIP_FORWARD. However, if I comment out the code that assigns the pending intents except for the "Play" button then pressing "Play" broadcasts the correct value.
Is there something I'm not aware of with using multiple pending intents as broadcasts on a widget or have I made a silly mistake in my code that I cannot spot? Or secret option C?
I'm hopelessly lost and confused and would be very grateful for any help. Thanks.
public class PlayerWidget extends AppWidgetProvider {
private boolean serviceIsPlaying = false;
private PendingIntent pendingIntentPrev = null;
private PendingIntent pendingIntentNext = null;
private PendingIntent pendingIntentStop = null;
private PendingIntent pendingIntentPlay = null;
public void onReceive(Context context, Intent intent) {
if (!intent.getAction().equals(Globals.BROADCAST_WIDGET_UPDATE)) {
return;
}
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), PlayerWidget.class.getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
this.updateViews(context, appWidgetManager, appWidgetIds);
}
private void updateViews(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_player);
this.setClickIntents(context);
if (this.serviceIsPlaying) {
remoteViews.setImageViewResource(R.id.btnPlay, R.drawable.ic_action_pause);
}
else {
remoteViews.setImageViewResource(R.id.btnPlay, R.drawable.ic_action_play);
}
remoteViews.setTextViewText(R.id.txtArtistName, currentTrack.getArtistName());
remoteViews.setTextViewText(R.id.txtSongName, currentTrack.getSongTitle());
remoteViews.setTextViewText(R.id.txtAlbumName, currentTrack.getAlbumName());
remoteViews.setImageViewBitmap(R.id.imgAlbumArt, BitmapFactory.decodeFile(currentTrack.getAlbumArtPath()));
remoteViews.setOnClickPendingIntent(R.id.btnPrev, pendingIntentPrev);
remoteViews.setOnClickPendingIntent(R.id.btnNext, pendingIntentNext);
remoteViews.setOnClickPendingIntent(R.id.btnStop, pendingIntentStop);
remoteViews.setOnClickPendingIntent(R.id.btnPlay, pendingIntentPlay);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
private void setClickIntents(Context context) {
Intent intentPrev = new Intent(Globals.BROADCAST_PLAYBACK_ACTION);
Intent intentNext = new Intent(Globals.BROADCAST_PLAYBACK_ACTION);
Intent intentStop = new Intent(Globals.BROADCAST_PLAYBACK_ACTION);
Intent intentPlay = new Intent(Globals.BROADCAST_PLAYBACK_ACTION);
intentPrev.putExtra(Globals.EXTRA_PLAYBACK_ACTION, Globals.PlaybackActions.SKIP_BACKWARD);
intentNext.putExtra(Globals.EXTRA_PLAYBACK_ACTION, Globals.PlaybackActions.SKIP_FORWARD);
intentStop.putExtra(Globals.EXTRA_PLAYBACK_ACTION, Globals.PlaybackActions.STOP);
if (this.serviceIsPlaying) {
intentPlay.putExtra(Globals.EXTRA_PLAYBACK_ACTION, Globals.PlaybackActions.PAUSE);
}
else {
intentPlay.putExtra(Globals.EXTRA_PLAYBACK_ACTION, Globals.PlaybackActions.PLAY);
}
pendingIntentPrev = PendingIntent.getBroadcast(context, 0, intentPrev, 0);
pendingIntentNext = PendingIntent.getBroadcast(context, 0, intentNext, 0);
pendingIntentStop = PendingIntent.getBroadcast(context, 0, intentStop, 0);
pendingIntentPlay = PendingIntent.getBroadcast(context, 0, intentPlay, 0);
}
}
I think the problem is a second parameter in PendingIntent.getBroadcast() method. The second parameter is requestCode and it should be different for all 4 PendingIntent-s you are using.For example:
pendingIntentPrev = PendingIntent.getBroadcast(context, 0, intentPrev, 0);
pendingIntentNext = PendingIntent.getBroadcast(context, 1, intentNext, 0);
pendingIntentStop = PendingIntent.getBroadcast(context, 2, intentStop, 0);
pendingIntentPlay = PendingIntent.getBroadcast(context, 3, intentPlay, 0);
I have the same problem few days ago and this helped me. Hope it will help you too.
Related
I have an app which plays music in a Service. There is also a Widget to control the music player. Everything works fine.
Now after the user closes the app or if it crashes or if I use System.exit(); or getActivity().finish(); to close it there are some problems. Everything seems to work fine, but somehow the widget doesn't receive broadcasts anymore even though another Activity still receives them.
The widget is still able to send broadcasts, it is only unable to receive them.
Could somebody please help me out?
Code from my widget:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
ma = (GlobalSongList)context.getApplicationContext();
context.getApplicationContext().registerReceiver(broadcastCoverReceiver, new IntentFilter(Music_service.BROADCAST_COVER));
context.getApplicationContext().registerReceiver(broadcastPlayPause, new IntentFilter(BROADCAST_PLAYPAUSE));
context.getApplicationContext().registerReceiver(checkagain, new IntentFilter("xyz"));
context.getApplicationContext().registerReceiver(intentshuttingdown, new IntentFilter("shutdown"));
RemoteViews controlButtons = new RemoteViews(context.getPackageName(),R.layout.widget);
Intent playIntent = new Intent(BROADCAST_PLAYPAUSE);
PendingIntent playPendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, playIntent, PendingIntent.FLAG_UPDATE_CURRENT);
controlButtons.setOnClickPendingIntent(R.id.bPlay, playPendingIntent);
Intent previousIntent = new Intent(why);
PendingIntent previousPendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, previousIntent, PendingIntent.FLAG_UPDATE_CURRENT);
controlButtons.setOnClickPendingIntent(R.id.bprev, previousPendingIntent);
if(ma.NP_List.size()>0) {
controlButtons.setTextViewText(R.id.song, ma.NP_List.get(ma.position).song);
controlButtons.setTextViewText(R.id.artist, ma.NP_List.get(ma.position).getArtist());
Intent open_activityIntent = new Intent(BROADCAST_open_activity);
PendingIntent openactivityPendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, open_activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
controlButtons.setOnClickPendingIntent(R.id.parent, openactivityPendingIntent);
if(ma.boolMusicPlaying1 == true) {
controlButtons.setImageViewResource(R.id.bPlay, R.drawable.pause2);
} else {
controlButtons.setImageViewResource(R.id.bPlay, R.drawable.play);
}
} else if(ma.NP_List.size() == 0) {
controlButtons.setTextViewText(R.id.song, "");
controlButtons.setTextViewText(R.id.artist, "");
}
Intent nextIntent = new Intent(BROADCAST_SWAP);
nextIntent.putExtra("nextprev", 1);
PendingIntent nextPendingIntent = PendingIntent.getBroadcast(context, 0, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
controlButtons.setOnClickPendingIntent(R.id.bNext, nextPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, controlButtons);
}
public BroadcastReceiver broadcastCoverReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent serviceIntent) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());
ComponentName thisWidget = new ComponentName(context.getApplicationContext(), MyWidget.class);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
if (appWidgetIds != null && appWidgetIds.length > 0) {
onUpdate(context, appWidgetManager, appWidgetIds);
}
}
};
You should use android app widget and not use broadcasts since it is the most conventional way of doing it....which is also mentioned in the docs
You should bind and unbind background service on onResume and onPause respectively in Activity.
Visit Android Activity and Service relationship - after Pause, after Stop to get to know relationship between Service and Activity
I have a widget that has a refresh button and a textview. Refresh updates the content and when user clicks on textview it starts a new activity.
Problem is it works fine for a few hours and then onclick and refresh button doesn't do anything. Nothing is captured in logcat. Also If user deletes widget and put a new one it starts working for a few hours and then the same story :(...what am I doing wrong!
Broadcast receiver.
onUpdate
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
long interval = getrefresInterval();
final Intent intent = new Intent(context, UpdateService.class);
final PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);
final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pending);
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),interval, pending);
// Build the intent to call the service
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget);
// To react to a click we have to use a pending intent as the
// onClickListener is excecuted by the homescreen application
Intent ClickIntent = new Intent(context.getApplicationContext(),widgetHadith.class);
Intent UpdateIntent = new Intent(context.getApplicationContext(),UpdateService.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, ClickIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntentUpdate = PendingIntent.getService(context.getApplicationContext(), 0, UpdateIntent,
PendingIntent.FLAG_UPDATE_CURRENT); //use this to update text on widget. if use this put UpdateService.class to intent
remoteViews.setOnClickPendingIntent(R.id.widget_textview, pendingIntent);
remoteViews.setOnClickPendingIntent(R.id.widget_refresh, pendingIntentUpdate);
// Finally update all widgets with the information about the click listener
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
// Update the widgets via the service
context.startService(intent);
}
onReceive
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)) {
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 {
super.onReceive(context, intent);
}
}
onDelete
public void onDeleted(Context context, int[] appWidgetIds) {
// Toast.makeText(context, "onDelete", Toast.LENGTH_SHORT).show();
super.onDeleted(context, appWidgetIds);
}
Service onstart where I am updating
#Override
public void onStart(Intent intent, int startId) {
RemoteViews updateViews = new RemoteViews(this.getPackageName(),R.layout.widget);
processDatabase();
Spanned text = LoadHadith();
String hadith = text.toString();
Log.d("BR", "service---> ");
// set the text of component TextView with id 'message'
updateViews.setTextViewText(R.id.widget_textview, text);
//Push update for this widget to the home screen
ComponentName thisWidget = new ComponentName(this, HelloWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
}
The problem is that you can't do a partiall update for a widget, you must set all the widget features, such as the set of PendingIntent's every time you push a new remoteView.
(Partiall updates are only available for API14 and up...).
The reason your widgets are loosing their pendingIntents is that the android system saves the remoteView, and rebuilds your widget with it, in case it resets the widget (shortage of memmory, TaskManager/taskKiller in use, etc...), so you must set all the update code for the widget in the remoteView in your updateService.
Otherwise, it's just won't set the pendingIntents again.
So just add the code setting the pendingIntents to the service and your problem will be solved =]
I'm starting with widgets and got a very nice tutorial on the internet, got the example run perfectly, but when i tried to change somethings I got stuck.
The thing is: I just want to change the image from my imageButton when i press it, I've tried somethings but nothing seems to work. I didn't get how the RemoteView and Intent works exactly. So if someone can explain it shortly I would appreciate it =)
Here's the code:
public class HelloWidget extends AppWidgetProvider {
private ImageButton wifi;
public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
Intent configIntent = new Intent(context, ClickOneActivity.class);
configIntent.setAction(ACTION_WIDGET_CONFIGURE);
Intent active = new Intent(context, HelloWidget.class);
active.setAction(ACTION_WIDGET_RECEIVER);
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.button_wifi, actionPendingIntent);
remoteViews.setOnClickPendingIntent(R.id.button_two, configPendingIntent);
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)) {
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)) {
Toast.makeText(context, "Teste", Toast.LENGTH_LONG).show();
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
remoteViews.setInt(R.id.button_wifi, "toogleOnOff", R.drawable.icon);
}
super.onReceive(context, intent);
}
}
}
There's a lot of the tutorial code i got as you can see =p
Thx since now
Looks like you need to understand a little more about RemoteViews. When you call functions like setOnClickPendingIntent, setInt, etc. on the RemoteViews object it basically just stores these operations and arguments internally. Then when the widget is displayed it just plays those operations back to construct the widget's views. So after giving the RemoteViews to the AppWidgetManager by calling updateAppWidget, you can't change them again unless you rebuild the whole RemoteViews and call updateAppWidget again.
As an answer to your question, you want to use a state list as the background for the button. There's a good example here.
I'm working on a music player-widget (home screen widget)..
It only needs to play 1 song, (Preferably with the MediaPlayer class). But I'm not sure how to implement it. I'm kind of inexperienced with Android development, just so that's mentioned.
The class I have so far extends AppWidgetProvider, and I guess it is not a good idea to let this class handle the music-playing part, but rather a Service. And if so, how?
Furthermore, I have 3 buttons: play, pause and stop, and I can distinguish which one has been pressed in onReceive(...).
Thanks in advance!
Here is the class.
public class MusicManager extends AppWidgetProvider {
private final String ACTION_WIDGET_PLAY = "PlaySong";
private final String ACTION_WIDGET_PAUSE = "PauseSong";
private final String ACTION_WIDGET_STOP = "StopSong";
private final int INTENT_FLAGS = 0;
private final int REQUEST_CODE = 0;
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews controlButtons = new RemoteViews(context.getPackageName(),
R.layout.main);
Intent playIntent = new Intent(context, MusicService.class);
Intent pauseIntent = new Intent(context, MusicService.class);
Intent stopIntent = new Intent(context, MusicService.class);
PendingIntent playPendingIntent = PendingIntent.getService(
context, REQUEST_CODE, playIntent, INTENT_FLAGS);
PendingIntent pausePendingIntent = PendingIntent.getService(
context, REQUEST_CODE, pauseIntent, INTENT_FLAGS);
PendingIntent stopPendingIntent = PendingIntent.getService(
context, REQUEST_CODE, stopIntent, INTENT_FLAGS);
controlButtons.setOnClickPendingIntent(
R.id.btnPlay, playPendingIntent);
controlButtons.setOnClickPendingIntent(
R.id.btnPause, pausePendingIntent);
controlButtons.setOnClickPendingIntent(
R.id.btnStop, stopPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, controlButtons);
}
}
Added <service android:name=".MusicService" android:enabled="true" />
to the manifest!
In the onUpdate(...) method of your AppWidgetProvider, use something like this to start a service (this example associates the service to a button click event):
Intent intent = new Intent(context, MusicService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
For more info look here
My code:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Intent active = new Intent(context, DialerWidget.class);
active.setAction(ACTION_WIDGET_RECEIVER);
active.putExtra("com.anirudha.android.appwidget.Number", currentNumber);
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context,
0, active, 0);
views.setOnClickPendingIntent(R.id.one,
actionPendingIntent);
views.setTextViewText(R.id.number, currentNumber);
appWidgetManager.updateAppWidget(appWidgetIds, views);
}
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
currentNumber = intent.getStringExtra("Number");
currentNumber += "1";
Intent active = new Intent(context, DialerWidget.class);
active.setAction(ACTION_WIDGET_RECEIVER);
active.putExtra("com.anirudha.android.appwidget.Number", CurrentNumber);
active.putExtra("com.anirudha.android.appwidget.Key", "1");
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context,
0, active, 0);
views.setOnClickPendingIntent(R.id.one,
actionPendingIntent);
views.setTextViewText(R.id.number, currentNumber);
ComponentName cn = new ComponentName(context.getPackageName(),
DialerWidget.class.getName());
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
appWidgetManager.updateAppWidget(cn, views);
Toast.makeText(context, currentNumber, Toast.LENGTH_SHORT).show();
}
super.onReceive(context, intent);
}
So, basically i want to change the extras i should receive with the Intent. But whenever the button is pressed i receive the same value with the Intent. I'm new to android development. Any help would be appreciated. Thanks
If you want to update an existing pending intent, you have to use the FLAG_UPDATE_CURRENT flag when you create it.
PendingIntent actionPendingIntent =
PendingIntent.getBroadcast(context, 0, active, PendingIntent.FLAG_UPDATE_CURRENT);
What type is currentNumber? It's not shown but from the code I guess it's an int.
You are putting it into the extra as an int, but you are reading it out as a string. The string will always we empty because there is no string extra with that name so currentnumber will also be 0, then increased by 1, so always be 1.
Also you are writing it with "com.anirudha.android.appwidget.Number" but reading with "Number". This must be the same key for writing and reading.
Solution: use getIntExtra()