so i'm trying figure out why remoteviews objects in notification seem registering first pendingintent give it, when there's 3 buttons , 3 pendingintents being assigned...
my code setting remoteviews:
remoteviews remoteviewsbig = new remoteviews(context.getpackagename(), r.layout.notification_main_big); remoteviews remoteviewssmall = new remoteviews(context.getpackagename(), r.layout. // set buttons intent intent; pendingintent pendingintent; intent = new intent(context, notificationreceiver.class); intent.putextra(notificationreceiver.action_key, notificationreceiver.action_prev); pendingintent = pendingintent.getbroadcast(context, 0, intent, pendingintent.flag_one_shot); remoteviewsbig.setonclickpendingintent(r.id.btn_prev, pendingintent); remoteviewssmall.setonclickpendingintent(r.id.btn_prev, pendingintent); intent = new intent(context, notificationreceiver.class); intent.putextra(notificationreceiver.action_key, notificationreceiver.action_toggle); pendingintent = pendingintent.getbroadcast(context, 0, intent, pendingintent.flag_one_shot); remoteviewsbig.setonclickpendingintent(r.id.btn_play, pendingintent); remoteviewssmall.setonclickpendingintent(r.id.btn_play, pendingintent); intent = new intent(context, notificationreceiver.class); intent.putextra(notificationreceiver.action_key, notificationreceiver.action_next); pendingintent = pendingintent.getbroadcast(context, 0, intent, pendingintent.flag_one_shot); remoteviewsbig.setonclickpendingintent(r.id.btn_next, pendingintent); remoteviewssmall.setonclickpendingintent(r.id.btn_next, pendingintent);
the code notificationreceiver:
public class notificationreceiver extends broadcastreceiver { public static final string action_key = "action"; public static final string action_prev = "prev"; public static final string action_toggle = "toggle"; public static final string action_next = "next"; @override public void onreceive(context context, intent intent) { string action = (string) intent.getextras().get(action_key); if (action == null) throw new illegalargumentexception("must provide action perform."); switch (action) { case action_next: eventbus.getdefault().post(new skipevent(skipevent.direction.next)); break; case action_prev: eventbus.getdefault().post(new skipevent(skipevent.direction.previous)); break; case action_toggle: eventbus.getdefault().post(new playbacktoggleevent()); break; default: throw new illegalargumentexception("must provide action perform."); } } }
and layout notifications measure: notification_main.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/primary" android:orientation="vertical"> <imageview android:id="@+id/img_background" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaletype="matrix" /> <imageview android:id="@+id/img_album_art" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_alignparenttop="true" android:src="@drawable/ic_default_artwork" /> <textview android:id="@+id/txt_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_toendof="@+id/img_album_art" android:layout_torightof="@+id/img_album_art" android:maxlines="1" android:paddingleft="4dp" android:paddingstart="4dp" android:textcolor="@color/primary_text" android:textstyle="bold" tools:text="song title" /> <textview android:id="@+id/txt_artist" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/txt_title" android:layout_toendof="@+id/img_album_art" android:layout_torightof="@+id/img_album_art" android:maxlines="1" android:paddingleft="4dp" android:paddingstart="4dp" android:textcolor="@color/primary_text" tools:text="artist name" /> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_alignparentend="true" android:layout_alignparentright="true" android:orientation="horizontal"> <imagebutton android:id="@+id/btn_prev" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@android:color/transparent" android:src="@drawable/ic_skip_previous_black_36dp" /> <imagebutton android:id="@+id/btn_play" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@android:color/transparent" android:src="@drawable/ic_play_arrow_black_48dp" /> <imagebutton android:id="@+id/btn_next" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@android:color/transparent" android:src="@drawable/ic_skip_next_black_36dp" /> </linearlayout> </relativelayout>
notification_main_big.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="128dp" android:background="@color/primary" android:orientation="vertical"> <imageview android:id="@+id/img_background" android:layout_width="match_parent" android:layout_height="match_parent" android:scaletype="matrix" /> <imageview android:id="@+id/img_album_art" android:layout_width="128dp" android:layout_height="match_parent" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_alignparenttop="true" android:src="@drawable/ic_default_artwork" /> <textview android:id="@+id/txt_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_toendof="@+id/img_album_art" android:layout_torightof="@+id/img_album_art" android:maxlines="1" android:paddingleft="4dp" android:textappearance="?android:attr/textappearancelarge" android:textcolor="@color/primary_text" tools:text="song title" /> <textview android:id="@+id/txt_artist" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/txt_title" android:layout_toendof="@+id/img_album_art" android:layout_torightof="@+id/img_album_art" android:maxlines="1" android:paddingleft="4dp" android:textcolor="@color/primary_text" tools:text="artist name" /> <textview android:id="@+id/txt_album" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/txt_artist" android:layout_toendof="@+id/img_album_art" android:layout_torightof="@+id/img_album_art" android:maxlines="1" android:paddingleft="4dp" android:textcolor="@color/primary_text" tools:text="album title" /> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_alignparentend="true" android:layout_alignparentright="true" android:layout_below="@id/txt_album" android:layout_torightof="@id/img_album_art" android:orientation="horizontal"> <imagebutton android:id="@+id/btn_prev" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@android:color/transparent" android:src="@drawable/ic_skip_previous_black_36dp" /> <imagebutton android:id="@+id/btn_play" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@android:color/transparent" android:src="@drawable/ic_play_arrow_black_48dp" /> <imagebutton android:id="@+id/btn_next" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@android:color/transparent" android:src="@drawable/ic_skip_next_black_36dp" /> </linearlayout> </relativelayout>
whenver hit of 3 buttons, i'm getting action_prev in notificationreceiver instead of appropriate action...
this because of same request code in pendingintent.getbroadcast()
method. zeros in example, have different different intents. count them 0 up.
Comments
Post a Comment