Saturday, 15 February 2014

play/pause button image in notification ,Android -



play/pause button image in notification ,Android -

i have implemented music player fires custom notification when stream sound playing

everything working , can play /pause sound using button in notification. problem have image button , can't alter image on click button indicate play / or pause

using remoteviews.setimageviewresource() in remotereciever nothing

the command done using broadcastreceiver

this code of firing notification player activity

public void setnotification(string songname){ string ns = context.notification_service; notificationmanager notificationmanager = (notificationmanager) getsystemservice(ns); @suppresswarnings("deprecation") notification notification = new notification(r.drawable.ic_launcher, null, system.currenttimemillis()); remoteviews notificationview = new remoteviews(getpackagename(), r.layout.notification_view); notificationview.setimageviewresource(r.id.button1, r.drawable.pause); notificationview.settextviewtext(r.id.textview1, songname); //the intent started when notification clicked (works) intent notificationintent = new intent(this, playeractivity.class); pendingintent pendingnotificationintent = pendingintent.getactivity(this, 0, notificationintent, pendingintent.flag_update_current); notification.contentview = notificationview; notification.contentintent = pendingnotificationintent; intent switchintent = new intent("com.example.test.action_play"); pendingintent pendingswitchintent = pendingintent.getbroadcast(this, 0, switchintent, pendingintent.flag_update_current); notificationview.setonclickpendingintent(r.id.play_pause, pendingswitchintent); notificationmanager.notify(1, notification); }

the notification xml

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <imageview android:layout_alignparentleft="true" android:layout_width="60dp" android:layout_height="60dp" android:src="@drawable/ic_launcher" android:id="@+id/imgappic" /> <textview android:layout_torightof="@id/imgappic" android:singleline="true" android:id="@+id/textview1" android:ellipsize="marquee" android:layout_marginleft="7dp" android:layout_margintop="10dp" android:marqueerepeatlimit ="marquee_forever" android:focusable="true" android:focusableintouchmode="true" android:scrollhorizontally="true" android:layout_width="170dp" android:layout_height="wrap_content"/> <imagebutton android:id="@+id/play_pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toleftof="@+id/play" /> </relativelayout>

this remoterecivier class

public class remotecontrolreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { remoteviews remoteviews = new remoteviews(context.getpackagename(), r.layout.notification_view); if(action.equalsignorecase("com.example.test.action_play")){ if(mediaplayer.isplaying()){ mediaplayer.pause(); remoteviews.setimageviewresource(r.id.button1, r.drawable.play); } else { mediaplayer.start(); remoteviews.setimageviewresource(r.id.button1, r.drawable.pause); } } } }

and manifest

<receiver android:name=".remotecontrolreceiver"> <intent-filter> <action android:name="com.music.app.action_play" /> </intent-filter> </receiver> <activity android:name="playeractivity" />

please , help solve , i've been stuck long time in advance

you have set notification.contentview new remoteview after changed can update notification view itself.

meaning, after receive action in broadcastreceiver, re-build notification desired button display ( pause or play )

hope helps

android notifications media-player android-remoteview

1 comment: