android - Notification.setStyle crashes and not able to set lock screen -
i trying make custom notification , following ian lake tutorial in media player , following tutorial in google set notification .set style getting crashed if dont use not able make media player lock screen trying.
private void initmediasession() throws remoteexception { if (mediasessionmanager != null) return; //mediasessionmanager exists componentname mediabuttonreceiver = new componentname(getapplicationcontext(), mediabuttonreceiver.class); mediasession = new mediasessioncompat(getapplicationcontext(), "audioplayer", mediabuttonreceiver, null); //get mediasessions transport controls transportcontrols = mediasession.getcontroller().gettransportcontrols(); //set mediasession -> ready receive media commands mediasession.setactive(true); //indicate mediasession handles transport control commands // through mediasessioncompat.callback. mediasession.setflags(mediasessioncompat.flag_handles_media_buttons | mediasessioncompat.flag_handles_transport_controls); intent mediabuttonintent = new intent(intent.action_media_button); mediabuttonintent.setclass(this, mediabuttonreceiver.class); pendingintent pendingintent = pendingintent.getbroadcast(this, 0, mediabuttonintent, 0); mediasession.setmediabuttonreceiver(pendingintent); //set mediasession's metadata updatemetadata(); // passing data // attach callback receive mediasession updates mediasession.setcallback(new mediasessioncompat.callback() { // implement callbacks @override public void onplay() { super.onplay(); messagesent(); = false; resumemedia(); buildnotification(playbackstatus.playing); } @override public void onpause() { super.onpause(); messagesent(); = true; pausemedia(); buildnotification(playbackstatus.paused); } @override public void onskiptonext() { super.onskiptonext(); skiptonext(); updatemetadata(); buildnotification(playbackstatus.playing); } @override public void onskiptoprevious() { super.onskiptoprevious(); skiptoprevious(); updatemetadata(); buildnotification(playbackstatus.playing); } @override public void onstop() { super.onstop(); removenotification(); //stop service pausemedia(); messagesent(); stopself(); } @override public void onseekto(long position) { super.onseekto(position); } }); } private void updatemetadata() { //replace medias albumart // update current metadata mediametadatacompat.builder metadatabuilder = new mediametadatacompat.builder(); string artist; if (activeaudio.getartist() != null) { artist = activeaudio.getartist(); } else { artist = "unknown"; } string album; if (activeaudio.getalbum() != null) { album = activeaudio.getalbum(); } else { album = "album"; } bitmap albumart; uri myuri = uri.parse(activeaudio.getalbum_art()); try { inputstream image_stream = getcontentresolver().openinputstream(myuri); bitmap bitmap = bitmapfactory.decodestream(image_stream); if (bitmap != null) { albumart = bitmap; } else { albumart = bitmapfactory.decoderesource(getresources(), r.drawable.music); } } catch (filenotfoundexception e) { e.printstacktrace(); albumart = bitmapfactory.decoderesource(getresources(), r.drawable.music); } metadatabuilder.putbitmap(mediametadatacompat.metadata_key_display_icon, albumart); metadatabuilder.putbitmap(mediametadatacompat.metadata_key_album_art, albumart); //lock screen icon pre lollipop metadatabuilder.putbitmap(mediametadatacompat.metadata_key_art, albumart); metadatabuilder.putstring(mediametadatacompat.metadata_key_display_title, activeaudio.gettitle()); metadatabuilder.putstring(mediametadatacompat.metadata_key_display_subtitle, activeaudio.getalbum()); metadatabuilder.putstring(mediametadatacompat.metadata_key_album, album); metadatabuilder.putlong(mediametadatacompat.metadata_key_track_number, 1); metadatabuilder.putlong(mediametadatacompat.metadata_key_num_tracks, 1); mediasession.setmetadata(metadatabuilder.build()); } private void buildnotification(playbackstatus playbackstatus) { /** * notification actions -> playbackaction() * 0 -> play * 1 -> pause * 2 -> next track * 3 -> previous track */ mediacontrollercompat controller = mediasession.getcontroller(); mediametadatacompat mediametadata = controller.getmetadata(); mediadescriptioncompat description = mediametadata.getdescription(); remoteviews views = new remoteviews(getpackagename(), r.layout.customnotification); if (playbackstatus == playbackstatus.playing || singleton.getinstance().getmedia() == 1) { views.setimageviewresource(r.id.imagebutton2, r.drawable.ic_pause_circle_outline_white_48dp); messagesent(); views.setonclickpendingintent(r.id.imagebutton2, playbackaction(1)); } else if (playbackstatus == playbackstatus.paused || singleton.getinstance().getmedia() == 2) { views.setimageviewresource(r.id.imagebutton2, r.drawable.ic_play_circle_outline_white_48dp); messagesent(); views.setonclickpendingintent(r.id.imagebutton2, playbackaction(0)); } views.setviewvisibility(r.id.imageview, view.visible); // intent notificationintent = new intent(this, main2activity.class); // pendingintent pendingintent = pendingintent.getactivity(this, 0, // notificationintent, pendingintent.flag_update_current); views.setonclickpendingintent(r.id.imagebutton3, playbackaction(3)); views.setonclickpendingintent(r.id.imagebutton4, playbackaction(2)); views.setimageviewresource(r.id.imagebutton3, r.drawable.ic_skip_previous_circle_outline_white_36dp); views.setimageviewresource(r.id.imagebutton4, r.drawable.ic_skip_next_circle_outline_white_36dp); views.settextviewtext(r.id.textview, description.gettitle()); views.settextviewtext(r.id.textview2, description.getsubtitle()); views.setimageviewbitmap(r.id.imageview, description.geticonbitmap()); notificationcompat.builder sta = new notificationcompat.builder(this); // sta.setcontentintent(pendingintent); sta.setcontent(views); sta.setsmallicon(r.drawable.ic_audiotrack_white_24dp); sta.setstyle(new notificationcompat.mediastyle().setshowactionsincompactview(0).setmediasession(mediasession.getsessiontoken())); startforeground(constants.notification_id.foreground_service, sta.build()); } public pendingintent playbackaction(int actionnumber) { intent playbackaction = new intent(this, mediaservice.class); switch (actionnumber) { case 0: // play playbackaction.setaction(action_play); return pendingintent.getservice(this, actionnumber, playbackaction, 0); case 1: // pause playbackaction.setaction(action_pause); return pendingintent.getservice(this, actionnumber, playbackaction, 0); case 2: // next track playbackaction.setaction(action_next); return pendingintent.getservice(this, actionnumber, playbackaction, 0); case 3: // previous track playbackaction.setaction(action_previous); return pendingintent.getservice(this, actionnumber, playbackaction, 0); case 4: playbackaction.setaction(constants.action.stopforeground_action); return pendingintent.getservice(this, actionnumber, playbackaction, 0); default: break; } return null; } private void removenotification() { try { stopforeground(true); } catch (exception e) { e.printstacktrace(); } } private void handleincomingactions(intent playbackaction) { if (playbackaction == null || playbackaction.getaction() == null) return; string actionstring = playbackaction.getaction(); if (actionstring.equalsignorecase(action_play)) { transportcontrols.play(); } else if (actionstring.equalsignorecase(action_pause)) { transportcontrols.pause(); } else if (actionstring.equalsignorecase(action_next)) { transportcontrols.skiptonext(); } else if (actionstring.equalsignorecase(action_previous)) { transportcontrols.skiptoprevious(); } else if (actionstring.equalsignorecase(action_stop)) { transportcontrols.stop(); } else if (actionstring.equalsignorecase(constants.action.stopforeground_action)) { transportcontrols.stop(); } }
the notification working fine add style not showing lock screen , when add style getting crashed .i dont understand how show lockscreen , why getting crashed when using style.
Comments
Post a Comment