android - Getting exception when closing the application -
i have music player app , have problem. when phone call on backpressed or finish() jumps onpause method not finish(). why? can't finish app. give thanks much help.
my code
public class musicplayer extends themableactivity implements seekbar.onseekbarchangelistener { mediaplayer musicplayer; int abc; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.media_player_layout); intent intent = getintent(); string track = intent.getdatastring(); textview txt = (textview)findviewbyid(r.id.txt); txt.setgravity(gravity.center); txt.settext(track); imageview img = (imageview)findviewbyid(r.id.img); img.setlayertype(view.layer_type_software, null); final uri uri = getintent() != null ? getintent().getdata() : null; final sharedpreferences premiumsettings = getsharedpreferences("premium", context.mode_private); final boolean ispremium = premiumsettings.getboolean("ispremium", false); final seekbar seekbar = (seekbar) findviewbyid(r.id.seekbar); textview max = (textview)findviewbyid(r.id.max); final textview actual = (textview)findviewbyid(r.id.actual); seekbar.setonseekbarchangelistener(this); button loop = (button)findviewbyid(r.id.loop); if (ispremium) { musicplayer = mediaplayer.create(musicplayer.this, uri); musicplayer.setwakemode(this.getapplicationcontext(), powermanager.partial_wake_lock); seekbar.setmax(musicplayer.getduration()); musicplayer.start(); max.settext(string.format("%dm %ds", timeunit.milliseconds.tominutes((long) musicplayer.getduration() ), timeunit.milliseconds.toseconds((long) musicplayer.getduration()) - timeunit.minutes.toseconds(timeunit.milliseconds.tominutes((long) musicplayer.getduration())))); final imagebutton play = (imagebutton) findviewbyid(r.id.play); if(!musicplayer.isplaying()){ play.setimageresource(android.r.drawable.ic_media_play); } else{ play.setimageresource(android.r.drawable.ic_media_pause); } play.setonlongclicklistener(new view.onlongclicklistener() { @override public boolean onlongclick(view view) { if(musicplayer.isplaying()){ play.setimageresource(android.r.drawable.ic_media_play); musicplayer.stop(); homecoming true; } homecoming false; } }); play.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { int position = musicplayer.getcurrentposition(); if(musicplayer.isplaying()){ musicplayer.pause(); play.setimageresource(android.r.drawable.ic_media_play); } else if(!musicplayer.isplaying()) { musicplayer.seekto(position); musicplayer.start(); play.setimageresource(android.r.drawable.ic_media_pause); }} }); loop.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { musicplayer.setlooping(true); } }); final handler mhandler = new handler(); runnable mrunnable; mrunnable = new runnable() { @override public void run() { int mcurrentposition = 0; if (musicplayer != null) { mcurrentposition = musicplayer.getcurrentposition(); seekbar.setprogress(mcurrentposition); } mhandler.postdelayed(this, 1000); actual.settext(string.format("%dm %ds", timeunit.milliseconds.tominutes((long) mcurrentposition), timeunit.milliseconds.toseconds((long) mcurrentposition) - timeunit.minutes.toseconds(timeunit.milliseconds.tominutes((long) mcurrentposition)))); } }; mrunnable.run(); imagebutton exit = (imagebutton) findviewbyid(r.id.exit); exit.setimageresource(android.r.drawable.btn_dialog); exit.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { if(musicplayer.isplaying()) { musicplayer.setlooping(false); musicplayer.stop(); } musicplayer = null; finish(); } }); } else { alertdialog.builder builder = new alertdialog.builder(musicplayer.this); builder.settitle("error"); builder.setmessage("you not premium user. please come in promocode or purchase total version"); builder.seticon(r.drawable.holo_dark_action_info); builder.setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { finish(); } }); alertdialog dialog = builder.create(); dialog.show(); }} public void onbackpressed () { if(musicplayer.isplaying()) { musicplayer.setlooping(false); musicplayer.stop(); } musicplayer = null; finish(); } @override public void onpause(){ super.onpause(); abc = musicplayer.getcurrentposition(); musicplayer.pause(); } @override public void onresume(){ super.onresume(); musicplayer.seekto(abc); musicplayer.start(); } @override public void onprogresschanged(seekbar seekbar, int i, boolean b) { if (musicplayer != null && b){ musicplayer.seekto(i); } } @override public void onstarttrackingtouch(seekbar seekbar) { } @override public void onstoptrackingtouch(seekbar seekbar) { }
that's because onpause called before when exit application.
make sure musicplayer not null when phone call onpause.
@override public void onpause(){ super.onpause(); if(musicplayer == null ) return; abc = musicplayer.getcurrentposition(); musicplayer.pause(); }
more info here: http://developer.android.com/training/basics/activity-lifecycle/stopping.html
android onpause
No comments:
Post a Comment