i have app goes out , gets time internet. maths on , after done starts activity.
here code in splashscreen activity
package com.firefluxentertainment.retroclicker; import android.content.context; import android.content.intent; import android.content.sharedpreferences; import android.os.asynctask; import android.os.countdowntimer; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.toast; import java.io.inputstream; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.url; import java.util.concurrent.executionexception; import java.util.regex.matcher; import java.util.regex.pattern; public class splashactivity extends appcompatactivity { long endtime; long timedelta; long endtimemaths; long timestamp; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_splash); load(); getwindow().getdecorview().setsystemuivisibility( view.system_ui_flag_layout_stable | view.system_ui_flag_layout_hide_navigation | view.system_ui_flag_layout_fullscreen | view.system_ui_flag_hide_navigation | view.system_ui_flag_fullscreen | view.system_ui_flag_immersive_sticky); downloadtask task = new downloadtask(); string result = null; try { result = task.execute("http://www.currenttimestamp.com/").get(); pattern p = pattern.compile("current_time = (.*?);"); matcher m = p.matcher(result); m.find(); string unixtime = (m.group(1)); timestamp = integer.parseint(unixtime); endtimemaths = endtime/1000; } catch (interruptedexception e) { e.printstacktrace(); } catch (executionexception e) { e.printstacktrace(); toast.maketext(splashactivity.this, "you aint got no internet man", toast.length_short).show(); } timedelta = timestamp-endtimemaths; log.i("timedelta", timedelta+""); } @override public void onwindowfocuschanged(boolean hasfocus) { super.onwindowfocuschanged(hasfocus); if (hasfocus) { getwindow().getdecorview().setsystemuivisibility( view.system_ui_flag_layout_stable | view.system_ui_flag_layout_hide_navigation | view.system_ui_flag_layout_fullscreen | view.system_ui_flag_hide_navigation | view.system_ui_flag_fullscreen | view.system_ui_flag_immersive_sticky); } } @override public void onresume() { super.onresume(); new countdowntimer(2000, 1000) { public void ontick(long millisuntilfinished) { } public void onfinish() { intent = new intent(getapplicationcontext(), mainactivity.class); startactivity(i); } }.start(); } public class downloadtask extends asynctask<string, void, string> { @override protected string doinbackground(string... urls) { string result = ""; url url; httpurlconnection urlconnection = null; try { url = new url(urls[0]); urlconnection = (httpurlconnection) url.openconnection(); inputstream in = urlconnection.getinputstream(); inputstreamreader reader = new inputstreamreader(in); int data = reader.read(); while (data != -1) { char current = (char) data; result += current; data = reader.read(); } return result; } catch (exception e) { e.printstacktrace(); } return null; } } void save(){ sharedpreferences pref = this.getsharedpreferences("test_prefs", context.mode_private); sharedpreferences.editor editor = pref.edit(); editor.putlong("endtime", endtime); editor.commit(); } void load() { sharedpreferences pref = this.getsharedpreferences("test_prefs", context.mode_private); sharedpreferences.editor editor = pref.edit(); endtime = pref.getlong("endtime", system.currenttimemillis()/1000); editor.apply(); } }
i have webpage content containing time download in background , run pattern , matcher on it. problem of time being wasted running matcher rather downloading webpage. since matcher running on main thread delaying ui thread causing logo have set in splashscreen layout not display until matcher finished. how make matcher runs in background ui not delayed?
thank help
the problem have asynctask
run long activity
alive. decouple activity
have couple options:
the best thing sent
intent
intentservice
handles this. need persist results (like insharedprefs
) and/or create binder/callback or use other methods data.put
asynctask
inapplication
class. not great idea because not purpose of class, work.in splashscreen, make call data, pass in intent next
activity
- , inactivity
runasynctask
pattern matching. breaks processing, result in user waiting these calls on both screens. still, better see app "doing something" rather having long wait.
there other ways handle this, these best approaches without getting complex.
Comments
Post a Comment