java - how to cancel method of count down timer to stop working -
i have list of music user set time play. want have button cancel m count down timer . test way doesn't work @ all. here code play , set time play.
public class main extends activity {
public static int hour_device, minute_device; public static int hour_user, minute_user; splash splash; listview listview; adaptor adaptor; private mediaplayer mediaplayer; static view lastview = null; static myindexstore indexstore; list<string> lines1 = new arraylist<string>(); list<string> lines2 = new arraylist<string>(); static list<string> array_audio = new arraylist<string>(); inputstream in; bufferedreader reader; string line = "1"; string[] tracks; string[] names; string[] infos; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); indexstore = new myindexstore(getapplicationcontext()); setcontentview(r.layout.main_activity); splash = new splash(this); splash.set_identity("1"); initiate(); mediaplayer = mediaplayer.create(getapplicationcontext(), r.raw.a1); //lastview = null; listview = (listview) findviewbyid(r.id.list); readtext1(); names = lines1.toarray(new string[0]);// = {"track one","the seconnd track","a nice track","name name name","the seconnd track","a nice track","name name name"}; readtext2(); infos = lines2.toarray(new string[0]); tracks = array_audio.toarray(new string[0]); adaptor = new adaptor(getapplicationcontext(), tracks, names, infos); listview.setadapter(adaptor); mediaplayer.setoncompletionlistener(new oncompletionlistener() { @override public void oncompletion(mediaplayer arg0) { if (lastview != null) { imageview play = (imageview) lastview.findviewbyid(r.id.play_stop); play.setimageresource(r.drawable.n_btn_play_unselected); } } }); } private static void initiate() { field[] fields = r.raw.class.getfields(); array_audio.clear(); (int count = 0; count < fields.length; count++) { array_audio.add("a" + (count + 1)); } } private void play(int index) { mediaplayer.release(); index++; string s = "a" + index; resources resources = getresources(); final int resourceid = resources.getidentifier(s, "raw", getpackagename()); try { mediaplayer = mediaplayer.create(this, resourceid); mediaplayer.start(); } catch (exception e) { e.printstacktrace(); } } @override protected void onpause() { mediaplayer.release(); listview.invalidateviews(); super.onpause(); } private void readtext1() { lines1.clear(); line = "1"; try { in = this.getassets().open("names.txt"); reader = new bufferedreader(new inputstreamreader(in)); while (line != null) { line = reader.readline(); if (line != null) lines1.add(line); else break; } } catch (ioexception e) { e.printstacktrace(); } } private void readtext2() { lines2.clear(); line = "1"; try { in = this.getassets().open("infos.txt"); reader = new bufferedreader(new inputstreamreader(in)); while (line != null) { line = reader.readline(); if (line != null) lines2.add(line); else break; } } catch (ioexception e) { e.printstacktrace(); } } public class adaptor extends arrayadapter<string> { private final context context; private final string[] tracks; private final string[] names; private final string[] infos; private hashmap<integer,string> textmap; typeface type_face; public adaptor(context context, string[] tracks, string[] names, string[] infos) { super(context, r.layout.track, tracks); this.context = context; this.tracks = tracks; this.names = names; this.infos = infos; type_face = typeface.createfromasset(context.getassets(), "btitrbd.ttf"); this.textmap = new hashmap<>(); } @override public view getview(final int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); final view rowview = inflater.inflate(r.layout.track, parent, false); textview name = (textview) rowview.findviewbyid(r.id.track_name); final textview time = (textview) rowview.findviewbyid(r.id.time); //populate textview map if(textmap!=null && textmap.get(new integer(position))!=null){ time.settext(textmap.get(new integer(position))); } name.settext(names[position]); name.settypeface(type_face); name.settypeface(type_face); final imageview ringtone = (imageview) rowview.findviewbyid(r.id.ringtone); if (position == indexstore.getindex()) ringtone.setimageresource(r.drawable.n_btn_ringtone_seted); final imageview play = (imageview) rowview.findviewbyid(r.id.play_stop); ringtone.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { layoutinflater factory = layoutinflater.from(main.this); final view deletedialogview = factory.inflate( r.layout.mylayout, null); final alertdialog deletedialog = new alertdialog.builder(main.this).create(); deletedialog.setview(deletedialogview); textview device_time = (textview) deletedialogview.findviewbyid(r.id.current_time); calendar c = calendar.getinstance(); int hour = c.get(calendar.hour_of_day); int minute = c.get(calendar.minute); hour_device = hour; minute_device = minute; device_time.settext(hour_device + ":" + minute_device); deletedialogview.findviewbyid(r.id.set).setonclicklistener(new onclicklistener() { @override public void onclick(view v) { timepicker timepicker = (timepicker) deletedialogview.findviewbyid(r.id.timepicker); timepicker.setis24hourview(true); hour_user = timepicker.getcurrenthour(); minute_user = timepicker.getcurrentminute(); string time1 = hour_device + ":" + minute_device; string time2 = hour_user + ":" + minute_user; simpledateformat format = new simpledateformat("hh:mm"); date date1 = null; try { date1 = format.parse(time1); } catch (parseexception e) { e.printstacktrace(); } date date2 = null; try { date2 = format.parse(time2); } catch (parseexception e) { e.printstacktrace(); } long result = date2.gettime() - date1.gettime(); new countdowntimer(result, 1000) { public void ontick(long millisuntilfinished) { time.settext(("seconds remaining: " + millisuntilfinished / 1000)); //create hashmap<integer,string> textmap @ constructer of adapter //now fill info int'o textmap.put(new integer(position), "seconds remaining: " + millisuntilfinished / 1000); //notify data change notifydatasetchanged(); } public void onfinish() { time.setvisibility(view.invisible); //create hashmap<integer,string> textmap @ constructer of adapter //now fill info textmap.put(new integer(position),null); //notify data change notifydatasetchanged(); toast.maketext(getapplicationcontext(), "finish", toast.length_long).show(); if (rowview != lastview || mediaplayer == null) { play(position); if (lastview != null) lastview = rowview; } else { play.setimageresource(r.drawable.n_btn_play_unselected); mediaplayer.release(); lastview = null; } } }.start(); deletedialog.dismiss(); } }); deletedialog.show(); } }); play.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { if (rowview != lastview || mediaplayer == null) { play(position); if (lastview != null) lastview = rowview; } else { play.setimageresource(r.drawable.n_btn_play_unselected); mediaplayer.release(); lastview = null; } } }); return rowview; } }
}
make count down timer instance variable;
private countdowntimer timer;
then delegate count variable:
@override public void onclick(view v) { ... timer = new countdowntimer(result, 1000) { ... } }
now can stop timer whenever want to:
timer.cancel();
Comments
Post a Comment