android - I am using AsyncTask for a splashscreen and database load in multiplaces. Is this a good idea? -
i have splash screen , main activity. idea show splash while doing db loads. can not use emulation mode not work same apk install directly on phone. when apk installs splash screen shows 5 seconds db listview screen shows. correct. when press application launch icon on phone splash not appear, white screen shows splash screen time , db listview appears. here associated files, supply more if needed:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.loadrunner" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="18" /> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.loadrunner.splashscreenactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name="com.loadrunner.mainactivity" android:exported="true" android:label="@string/app_name" > <intent-filter> <category android:name="android.intent.category.default" /> </intent-filter> </activity> </application>
package com.loadrunner; import android.app.activity; import android.content.intent; import android.os.asynctask; import android.os.bundle; public class splashscreenactivity extends activity { private static final int splash_show_time = 5000; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); new backgroundsplashtask().execute(); } /*@override protected void onstart(){ setcontentview(r.layout.activity_main); new backgroundsplashtask().execute(); }*/ /** * async task: can used load db, images during splash screen * shown user */ private class backgroundsplashtask extends asynctask<void, void, void> { @override protected void onpreexecute() { super.onpreexecute(); } @override protected void doinbackground(void... arg0) { // have give sleep thread // if want load database, make // network calls, load images // can here , remove following // sleep // not worry thread.sleep // async task, not disrupt ui try { thread.sleep(splash_show_time); } catch (interruptedexception e) { e.printstacktrace(); } return null; } @override protected void onpostexecute(void result) { super.onpostexecute(result); intent = new intent(splashscreenactivity.this, mainactivity.class); // info loaded can during splash_show // can passed main activity using // below i.putextra("loaded_info", " "); startactivity(i); finish(); } } } package com.loadrunner; import android.app.activity; import android.os.bundle; import android.util.log; import com.table.tablemainlayout; public class mainactivity extends activity { final string tag = "mainactivity.java"; public static databasehandler db; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //************************************************************************************************* // remove title bar. //requestwindowfeature(window.feature_no_title); //splash //setcontentview(r.layout.activity_main); //splash /* loads next module */ // splash image //imageview splashimage = (imageview) mainactivity.this.findviewbyid(r.id.imageviewsplashlogo); //splash //imageview splashimage = (imageview) findviewbyid(r.id.imageviewsplashlogo); //splash //log.d("loadrunner", "splashimage: " + splashimage); //splashimage.setimageresource(r.drawable.shoppingcart); // make splash image invisible //splashimage.setvisibility(view.gone); //splash // splashimage.setvisibility(view.visible); //splash // specify animation //animation animfadeout = animationutils.loadanimation(mainactivity.this, r.anim.splash_screen_fadeout); //splash // apply animattion //splashimage.startanimation(animfadeout); //splash //************************************************************************************************* db = new databasehandler(this); db.getwritabledatabase(); this.db.insertfast(100); int dbreccnt = db.countrecords(); log.d("apploadrunner ", "loadrunner record count " + dbreccnt); setcontentview(new tablemainlayout(this)); log.d("apploadrunner ", "loadrunner mainactivity content set"); }
}
package com.loadrunner; import android.os.asynctask; import android.util.log; public class asyncinsertdata extends asynctask<string, string, string> { databasehandler databasehandler; string type; long timeelapsed; protected asyncinsertdata(string type){ this.type = type; //this.databasehandler = new databasehandler(this); //(mainactivity.this); } //@type - can 'normal' or 'fast' @override protected void onpreexecute() { super.onpreexecute(); //tvstatus.settext("inserting " + edittextrecordnum.gettext() + " records..."); } @override protected string doinbackground(string... aurl) { log.d("appsynch", "asynchinsertdata.java"); try { // empty table databasehandler.deleterecords(); // keep track of execution time long lstarttime = system.nanotime(); type = "fast"; int insertcount = 20; // never seems called if (type.equals("normal")) { databasehandler.insertnormal(insertcount); } else { databasehandler.insertfast(insertcount); } // execution finished long lendtime = system.nanotime(); // display execution time timeelapsed = lendtime - lstarttime; } catch (exception e) { e.printstacktrace(); } return null; } protected void onpostexecute(string unused) { //toast.maketext(getapplicationcontext(),"this android toast message", toast.length_long).show(); //tvstatus.settext("done " + choice + " inserting " + databasehandler.countrecords() + " records table: [" + this.databasehandler.tablename + "]. time elapsed: " + timeelapsed / 1000000 + " ms."); }
}
package com.loadrunner; import android.app.activity; import android.os.bundle; import android.util.log; import com.table.tablemainlayout; public class mainactivity extends activity { final string tag = "mainactivity.java"; public static databasehandler db; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //************************************************************************************************* // remove title bar. //requestwindowfeature(window.feature_no_title); //splash //setcontentview(r.layout.activity_main); //splash /* loads next module */ // splash image //imageview splashimage = (imageview) mainactivity.this.findviewbyid(r.id.imageviewsplashlogo); //splash //imageview splashimage = (imageview) findviewbyid(r.id.imageviewsplashlogo); //splash //log.d("loadrunner", "splashimage: " + splashimage); //splashimage.setimageresource(r.drawable.shoppingcart); // make splash image invisible //splashimage.setvisibility(view.gone); //splash // splashimage.setvisibility(view.visible); //splash // specify animation //animation animfadeout = animationutils.loadanimation(mainactivity.this, r.anim.splash_screen_fadeout); //splash // apply animattion //splashimage.startanimation(animfadeout); //splash //************************************************************************************************* db = new databasehandler(this); db.getwritabledatabase(); this.db.insertfast(100); int dbreccnt = db.countrecords(); log.d("apploadrunner ", "loadrunner record count " + dbreccnt); setcontentview(new tablemainlayout(this)); log.d("apploadrunner ", "loadrunner mainactivity content set"); }
}
i think because once install app , reopen it, app running. if not kill before restarting it, resume activity on rather starting splashscreen. in short, verify works, try killing app after installation or set shows loading screen when resumes (might require significant refactor). you'd need refactor loading starting in oncreate()
.
Comments
Post a Comment