Tuesday, 15 July 2014

android - Issues with tabactivity and mainactivity using service -



android - Issues with tabactivity and mainactivity using service -

hi have app aacplayer im trying implement tab menu achieved issue here when app opens have run mainactivity.java 1 have service of sound added anothers tabs , works perfect ones doesnt utilize services.

here log of error:

02-14 19:24:11.685: e/androidruntime(31027): java.lang.runtimeexception: error receiving broadcast intent { act=started flg=0x10 } in com.webcraftbd.radio.mainactivity$radioupdatereceiver@42e09468

tabactivity.java:

public class androidtablayoutactivity extends tabactivity { /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); tabhost tabhost = gettabhost(); // tab photos tabspec photospec = tabhost.newtabspec("en vivo"); photospec.setindicator("en vivo", getresources().getdrawable(r.drawable.icon_songs_tab)); intent photosintent = new intent(this, mainactivity.class); photospec.setcontent(photosintent); // tab songs tabspec songspec = tabhost.newtabspec("twitter"); // setting title , icon tab songspec.setindicator("twitter", getresources().getdrawable(r.drawable.icon_photos_tab)); intent songsintent = new intent(this, songsactivity.class); songspec.setcontent(songsintent); // tab videos tabspec videospec = tabhost.newtabspec("contactenos"); videospec.setindicator("contactenos", getresources().getdrawable(r.drawable.icon_videos_tab)); intent videosintent = new intent(this, videosactivity.class); videospec.setcontent(videosintent); // adding tabspec tabhost tabhost.addtab(photospec); // adding photos tab tabhost.addtab(songspec); // adding songs tab tabhost.addtab(videospec); // adding videos tab for(int i=0;i<tabhost.gettabwidget().getchildcount();i++) { textview tv = (textview) tabhost.gettabwidget().getchildat(i).findviewbyid(android.r.id.title); tv.settextcolor(color.white); tv.setpadding(0, 0, 0, 5); tv.setshadowlayer(2, 2, 2, color.black); } } }

mainactivity.java:

public class mainactivity extends baseactivity{ private static boolean displayad; private button playbutton; private button pausebutton; private button stopbutton; private button nextbutton; private button previousbutton; private imageview stationimageview; private textview albumtextview; private textview artisttextview; private textview tracktextview; private textview statustextview; private textview timetextview; private intent bindintent; private telephonymanager telephonymanager; private boolean wasplayingbeforephonecall = false; private radioupdatereceiver radioupdatereceiver; private radioservice radioservice; private adview adview; private string status_buffering; private static final string type_aac = "aac"; private static final string type_mp3 = "mp3"; private seekbar volumeseekbar = null; private audiomanager audiomanager = null; private handler handler; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setvolumecontrolstream(audiomanager.stream_music); setcontentview(r.layout.activity_main); initcontrols(); // bind service seek { bindintent = new intent(this, radioservice.class); bindservice(bindintent, radioconnection, context.bind_auto_create); } catch(exception e) { } telephonymanager = (telephonymanager) getsystemservice(telephony_service); if(telephonymanager != null) { telephonymanager.listen(phonestatelistener, phonestatelistener.listen_call_state); } handler = new handler(); initialize(); } private void initcontrols() { seek { volumeseekbar = (seekbar) findviewbyid(r.id.seekbar1); audiomanager = (audiomanager) getsystemservice(context.audio_service); volumeseekbar.setmax(audiomanager .getstreammaxvolume(audiomanager.stream_music)); volumeseekbar.setprogress(audiomanager .getstreamvolume(audiomanager.stream_music)); volumeseekbar .setonseekbarchangelistener(new onseekbarchangelistener() { @override public void onstoptrackingtouch(seekbar arg0) { } @override public void onstarttrackingtouch(seekbar arg0) { } @override public void onprogresschanged(seekbar arg0, int progress, boolean arg2) { audiomanager.setstreamvolume( audiomanager.stream_music, progress, 0); } }); } grab (exception e) { e.printstacktrace(); } } @override public boolean onkeydown(int keycode, keyevent event) { if (keycode == keyevent.keycode_volume_up) { int index = volumeseekbar.getprogress(); volumeseekbar.setprogress(index + 1); homecoming false; } else if (keycode == keyevent.keycode_volume_down) { int index = volumeseekbar.getprogress(); volumeseekbar.setprogress(index - 1); homecoming false; } homecoming super.onkeydown(keycode, event); } @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); if (newconfig.orientation == configuration.orientation_portrait || newconfig.orientation == configuration.orientation_landscape) { setcontentview(r.layout.activity_main); seek { handler.post( new runnable() { public void run() { initialize(); if(radioservice.gettotalstationnumber()<=1) { nextbutton.setenabled(false); nextbutton.setvisibility(view.invisible); previousbutton.setenabled(false); previousbutton.setvisibility(view.invisible); } updatestatus(); updatemetadata(); //updatealbum(); system.out.println("radioservice.ispreparingstarted() = "+radioservice.ispreparingstarted()); } }); } catch(exception e) { e.printstacktrace(); } } } public void initialize() { seek { displayad = (boolean) boolean.parseboolean(getresources().getstring(r.string.is_display_ad)); status_buffering = getresources().getstring(r.string.status_buffering); playbutton = (button) this.findviewbyid(r.id.playbutton); pausebutton = (button) this.findviewbyid(r.id.pausebutton); stopbutton = (button) this.findviewbyid(r.id.stopbutton); nextbutton = (button) this.findviewbyid(r.id.nextbutton); previousbutton = (button) this.findviewbyid(r.id.previousbutton); pausebutton.setenabled(false); pausebutton.setvisibility(view.invisible); stationimageview = (imageview) findviewbyid(r.id.stationimageview); playbutton.setenabled(true); stopbutton.setenabled(false); albumtextview = (textview) this.findviewbyid(r.id.albumtextview); artisttextview = (textview) this.findviewbyid(r.id.artisttextview); tracktextview = (textview) this.findviewbyid(r.id.tracktextview); statustextview = (textview) this.findviewbyid(r.id.statustextview); timetextview = (textview) this.findviewbyid(r.id.timetextview); startservice(new intent(this, radioservice.class)); displayad(); } grab (exception e) { e.printstacktrace(); } } public void displayad() { if(displayad==true) { // create adview seek { if (adview != null) { adview.destroy(); } adview = new adview(this, adsize.smart_banner, this.getstring(r.string.admob_publisher_id)); linearlayout layout = (linearlayout)findviewbyid(r.id.adlayout); layout.addview(adview); adview.loadad(new adrequest()); } grab (outofmemoryerror e) { e.printstacktrace(); } grab (exception e) { e.printstacktrace(); } } else { linearlayout layout = (linearlayout)findviewbyid(r.id.adlayout); layout.setlayoutparams(new linearlayout.layoutparams(0,0)); layout.setvisibility(view.invisible); } } public void updateplaytimer() { timetextview.settext(radioservice.getplayingtime()); final handler handler = new handler(); timer timer = new timer(); timertask doasynchronoustask = new timertask() { @override public void run() { handler.post(new runnable() { public void run() { timetextview.settext(radioservice.getplayingtime()); } }); } }; timer.schedule(doasynchronoustask, 0, 1000); } public void onclickplaybutton(view view) { radioservice.play(); } public void onclickpausebutton(view view) { radioservice.pause(); } public void onclickstopbutton(view view) { radioservice.stop(); //resetmetadata(); //updatedefaultcoverimage(); } public void onclicknextbutton(view view) { resetmetadata(); playnextstation(); //updatedefaultcoverimage(); } public void onclickpreviousbutton(view view) { resetmetadata(); playpreviousstation(); //updatedefaultcoverimage(); } public void playnextstation() { radioservice.stop(); radioservice.setnextstation(); /* if(radioservice.isplaying()) { radioservice.setstatus(status_buffering); updatestatus(); radioservice.stop(); radioservice.play(); } else { radioservice.stop(); } */ } public void playpreviousstation() { radioservice.stop(); radioservice.setpreviousstation(); /* if(radioservice.isplaying()) { radioservice.setstatus(status_buffering); updatestatus(); radioservice.stop(); radioservice.play(); } else { radioservice.stop(); } */ } public void updatedefaultcoverimage() { string mdrawablename = "station_"+(radioservice.getcurrentstationid()+1); int resid = getresources().getidentifier(mdrawablename , "drawable", getpackagename()); stationimageview.setimageresource(resid); albumtextview.settext(""); } public void updatealbum() { string album = radioservice.getalbum(); string artist = radioservice.getartist(); string track = radioservice.gettrack(); bitmap albumcover = radioservice.getalbumcover(); albumtextview.settext(album); if(albumcover==null || (artist.equals("") && track.equals(""))) updatedefaultcoverimage(); else { stationimageview.setimagebitmap(albumcover); radioservice.setalbum(lastfmcover.album); if(radioservice.getalbum().length() + radioservice.getartist().length()>50) { albumtextview.settext(""); } } } public void updatemetadata() { string artist = radioservice.getartist(); string track = radioservice.gettrack(); //if(artist.length()>30) //artist = artist.substring(0, 30)+"..."; artisttextview.settext(artist); tracktextview.settext(track); albumtextview.settext(""); } public void resetmetadata() { radioservice.resetmetadata(); artisttextview.settext(""); albumtextview.settext(""); tracktextview.settext(""); } @override public void ondestroy() { super.ondestroy(); if(radioservice!=null) { if(!radioservice.isplaying() && !radioservice.ispreparingstarted()) { //radioservice.stopself(); radioservice.stopservice(bindintent); } } if (adview != null) { adview.destroy(); } if(telephonymanager != null) { telephonymanager.listen(phonestatelistener, phonestatelistener.listen_none); } } @override protected void onpause() { super.onpause(); if (radioupdatereceiver != null) unregisterreceiver(radioupdatereceiver); } @override protected void onresume() { super.onresume(); /* register receiving broadcast messages */ if (radioupdatereceiver == null) radioupdatereceiver = new radioupdatereceiver(); registerreceiver(radioupdatereceiver, new intentfilter(radioservice.mode_created)); registerreceiver(radioupdatereceiver, new intentfilter(radioservice.mode_destroyed)); registerreceiver(radioupdatereceiver, new intentfilter(radioservice.mode_started)); registerreceiver(radioupdatereceiver, new intentfilter(radioservice.mode_start_preparing)); registerreceiver(radioupdatereceiver, new intentfilter(radioservice.mode_prepared)); registerreceiver(radioupdatereceiver, new intentfilter(radioservice.mode_playing)); registerreceiver(radioupdatereceiver, new intentfilter(radioservice.mode_paused)); registerreceiver(radioupdatereceiver, new intentfilter(radioservice.mode_stopped)); registerreceiver(radioupdatereceiver, new intentfilter(radioservice.mode_completed)); registerreceiver(radioupdatereceiver, new intentfilter(radioservice.mode_error)); registerreceiver(radioupdatereceiver, new intentfilter(radioservice.mode_buffering_start)); registerreceiver(radioupdatereceiver, new intentfilter(radioservice.mode_buffering_end)); registerreceiver(radioupdatereceiver, new intentfilter(radioservice.mode_metadata_updated)); registerreceiver(radioupdatereceiver, new intentfilter(radioservice.mode_album_updated)); if(wasplayingbeforephonecall) { radioservice.play(); wasplayingbeforephonecall = false; } } /* receive broadcast messages radioservice */ private class radioupdatereceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { if (intent.getaction().equals(radioservice.mode_created)) { } else if (intent.getaction().equals(radioservice.mode_destroyed)) { playbutton.setenabled(true); pausebutton.setenabled(false); stopbutton.setenabled(false); playbutton.setvisibility(view.visible); pausebutton.setvisibility(view.invisible); //updatedefaultcoverimage(); updatemetadata(); updatestatus(); } else if (intent.getaction().equals(radioservice.mode_started)) { pausebutton.setenabled(false); playbutton.setvisibility(view.visible); pausebutton.setvisibility(view.invisible); playbutton.setenabled(true); stopbutton.setenabled(false); updatestatus(); } else if (intent.getaction().equals(radioservice.mode_start_preparing)) { pausebutton.setenabled(false); playbutton.setvisibility(view.visible); pausebutton.setvisibility(view.invisible); playbutton.setenabled(false); stopbutton.setenabled(true); updatestatus(); } else if (intent.getaction().equals(radioservice.mode_prepared)) { playbutton.setenabled(true); pausebutton.setenabled(false); stopbutton.setenabled(false); playbutton.setvisibility(view.visible); pausebutton.setvisibility(view.invisible); updatestatus(); } else if (intent.getaction().equals(radioservice.mode_buffering_start)) { updatestatus(); } else if (intent.getaction().equals(radioservice.mode_buffering_end)) { updatestatus(); } else if (intent.getaction().equals(radioservice.mode_playing)) { if(radioservice.getcurrentstationtype().equals(type_aac)) { playbutton.setenabled(false); stopbutton.setenabled(true); } else { playbutton.setenabled(false); pausebutton.setenabled(true); stopbutton.setenabled(true); playbutton.setvisibility(view.invisible); pausebutton.setvisibility(view.visible); } updatestatus(); } else if(intent.getaction().equals(radioservice.mode_paused)) { playbutton.setenabled(true); pausebutton.setenabled(false); stopbutton.setenabled(true); playbutton.setvisibility(view.visible); pausebutton.setvisibility(view.invisible); updatestatus(); } else if(intent.getaction().equals(radioservice.mode_stopped)) { playbutton.setenabled(true); pausebutton.setenabled(false); stopbutton.setenabled(false); playbutton.setvisibility(view.visible); pausebutton.setvisibility(view.invisible); updatestatus(); } else if(intent.getaction().equals(radioservice.mode_completed)) { playbutton.setenabled(true); pausebutton.setenabled(false); stopbutton.setenabled(false); playbutton.setvisibility(view.visible); pausebutton.setvisibility(view.invisible); updatestatus(); } else if(intent.getaction().equals(radioservice.mode_error)) { playbutton.setenabled(true); pausebutton.setenabled(false); stopbutton.setenabled(false); playbutton.setvisibility(view.visible); pausebutton.setvisibility(view.invisible); updatestatus(); } else if(intent.getaction().equals(radioservice.mode_metadata_updated)) { updatemetadata(); updatestatus(); //updatedefaultcoverimage(); } else if(intent.getaction().equals(radioservice.mode_album_updated)) { //updatealbum(); } } } phonestatelistener phonestatelistener = new phonestatelistener() { @override public void oncallstatechanged(int state, string incomingnumber) { if (state == telephonymanager.call_state_ringing) { wasplayingbeforephonecall = radioservice.isplaying(); radioservice.stop(); } else if(state == telephonymanager.call_state_idle) { if(wasplayingbeforephonecall) { radioservice.play(); } } else if(state == telephonymanager.call_state_offhook) { //a phone call dialing, active or on hold wasplayingbeforephonecall = radioservice.isplaying(); radioservice.stop(); } super.oncallstatechanged(state, incomingnumber); } }; public void updatestatus() { string status = radioservice.getstatus(); if(radioservice.gettotalstationnumber() > 1) { if(status!="") status = radioservice.getcurrentstationname()+" - "+status; else status = radioservice.getcurrentstationname(); } seek { statustextview.settext(status); } catch(exception e) { e.printstacktrace(); } } // handles connection between service , activity private serviceconnection radioconnection = new serviceconnection() { public void onserviceconnected(componentname classname, ibinder service) { radioservice = ((radioservice.radiobinder)service).getservice(); if(radioservice.gettotalstationnumber()<=1) { nextbutton.setenabled(false); nextbutton.setvisibility(view.invisible); previousbutton.setenabled(false); previousbutton.setvisibility(view.invisible); } updatestatus(); updatemetadata(); //updatealbum(); updateplaytimer(); radioservice.shownotification(); //radioservice.play(); } public void onservicedisconnected(componentname classname) { radioservice = null; } }; }

baseactivity.java:

public class baseactivity extends activity { private intent bindintent; private radioservice radioservice; private static boolean isexitmenuclicked; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); isexitmenuclicked = false; // bind service bindintent = new intent(this, radioservice.class); bindservice(bindintent, radioconnection, context.bind_auto_create); setvolumecontrolstream(audiomanager.stream_music); } @override protected void onresume() { super.onresume(); if(isexitmenuclicked==true) finish(); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main_menu, menu); homecoming true; } @override public boolean onoptionsitemselected(menuitem item) { intent i; final string thisclassname = this.getclass().getname(); final string thispackagename = this.getpackagename(); switch (item.getitemid()) { case r.id.radio: if(!thisclassname.equals(thispackagename+".mainactivity")) { = new intent(this, mainactivity.class); i.addflags(intent.flag_activity_clear_top); i.addflags(intent.flag_activity_new_task); startactivity(i); homecoming true; } break; case r.id.facebook: if(!thisclassname.equals(thispackagename+".facebookactivity")) { = new intent(this, facebookactivity.class); startactivity(i); homecoming true; } break; case r.id.twitter: if(!thisclassname.equals(thispackagename+".twitteractivity")) { = new intent(this, twitteractivity.class); startactivity(i); homecoming true; } break; case r.id.about: if(!thisclassname.equals(thispackagename+".aboutactivity")) { = new intent(this, aboutactivity.class); startactivity(i); homecoming true; } break; case r.id.exit: string title = "exit radio"; string message = "desea salir de la aplicacion?"; string buttonyesstring = "si"; string buttonnostring = "no"; isexitmenuclicked = true; alertdialog.builder advertisement = new alertdialog.builder(this); //ad.settitle(title); ad.setmessage(message); ad.setcancelable(true); ad.setpositivebutton(buttonyesstring, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { if(radioservice!=null) { radioservice.exitnotification(); radioservice.stop(); radioservice.stopservice(bindintent); isexitmenuclicked = true; finish(); } } }); ad.setnegativebutton(buttonnostring, null); ad.show(); homecoming true; } homecoming super.onoptionsitemselected(item); } // handles connection between service , activity private serviceconnection radioconnection = new serviceconnection() { public void onserviceconnected(componentname classname, ibinder service) { radioservice = ((radioservice.radiobinder)service).getservice(); } public void onservicedisconnected(componentname classname) { radioservice = null; } }; }

i dont know else can prepare main activity.

thank much.

bindservice(bindintent, radioconnection, context.bind_auto_create);

in mainactivity.java, code must below;

getapplicationcontext().bindservice(bindintent, radioconnection, context.bind_auto_create);

android android-layout android-tabhost android-service android-tabactivity

No comments:

Post a Comment