Difference in android code execution -
i have 2 different devices running same code executing them in different ways. whenever minimize application , pull on tablet works way wanted to, not creating timer. when run on phone though , minimize/maximize timer started, having 2 run @ same time. why work differently on 2 devices or there else happening not seeing. (i know need create background service , way doing incorrect)
tablet specs
android version: 4.4.2
kernal version: 3.4.67
model number: dl701q
phone specs
android version: 4.4.2
kernal version: 3.4.0+
software/model: vs450pp1
code
main class
package temp; import android.app.activity; import android.content.context; import android.content.intent; import android.location.location; import android.net.connectivitymanager; import android.net.networkinfo; import android.net.uri; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.button; import android.widget.imagebutton; import android.widget.textview; import android.widget.toast; import android.location.geocoder; import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.googleplayservicesutil; import com.google.android.gms.common.api.googleapiclient; import com.google.android.gms.common.api.pendingresult; import com.google.android.gms.common.api.status; import com.google.android.gms.location.locationlistener; import com.google.android.gms.location.locationrequest; import com.google.android.gms.location.locationservices; import java.text.dateformat; import java.util.date; import java.util.locale; public class mainactivity extends activity implements locationlistener, googleapiclient.connectioncallbacks, googleapiclient.onconnectionfailedlistener{ private button blogout, bwebsite; private imagebutton blogdata; private textview etlabel; private userlocalstore userlocalstore; private string mlastupdatetime; private locationrequest mlocationrequest; private googleapiclient mgoogleapiclient; private static final string tag = "mainactivity"; private static final long interval = 1000 * 15; private static final long fatest_interval = 1000 * 30; private geocoder geocoder; addressops addressops; timerupdate timerupdate; int count = 0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); log.e(tag, "on create . . . . ."); if(!isgoogleplayservicesavailable()){ startactivity(new intent(mainactivity.this, login.class)); finish(); toast.maketext(getapplicationcontext(), "please update googleplay servies use application", toast.length_long).show(); }else { mgoogleapiclient = new googleapiclient.builder(this) .addapi(locationservices.api) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .build(); createlocationrequest(); userlocalstore = new userlocalstore(this); this.geocoder = new geocoder(mainactivity.this, locale.getdefault()); addressops = new addressops(this.geocoder); etlabel = (textview) findviewbyid(r.id.etemaillabel); blogout = (button) findviewbyid(r.id.blogout); blogdata = (imagebutton) findviewbyid(r.id.datalog); bwebsite = (button) findviewbyid(r.id.website); blogdata.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { string pressstatus = "3"; timerupdate.update(pressstatus); } }); blogout.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { userlocalstore.clearuserdata(); userlocalstore.setuserloggedin(false); timerupdate.stoptimertask(); startactivity(new intent(mainactivity.this, login.class)); finish(); } }); bwebsite.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { intent browserintent = new intent(intent.action_view, uri.parse("http://temp")); startactivity(browserintent); } }); } } private void displayuserdetails(){ user user = userlocalstore.getloggedinuser(); string userdisplay = "logged in as: " + user.username; etlabel.settext(userdisplay); } private boolean authenticate(){ return userlocalstore.getuserloggedin(); } private boolean isnetworkavailable() { connectivitymanager connectivitymanager = (connectivitymanager) getsystemservice(context.connectivity_service); networkinfo activenetworkinfo = connectivitymanager.getactivenetworkinfo(); log.e(tag, "network check"); return activenetworkinfo != null && activenetworkinfo.isconnected(); } private boolean isgoogleplayservicesavailable() { int status = googleplayservicesutil.isgoogleplayservicesavailable(this); if (connectionresult.success == status) { return true; } else { googleplayservicesutil.geterrordialog(status, this, 0).show(); return false; } } protected void createlocationrequest(){ mlocationrequest = new locationrequest(); mlocationrequest.setinterval(interval); mlocationrequest.setfastestinterval(fatest_interval); mlocationrequest.setpriority(locationrequest.priority_high_accuracy); } @override public void onconnected(bundle bundle) { log.e(tag, "onconnected: connected - " + mgoogleapiclient.isconnected()); startlocationupdates(); } protected void startlocationupdates() { pendingresult<status> pendingresult = locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient, mlocationrequest, this); log.e(tag, "location update started "); } @override public void onconnectionsuspended(int i) { stoplocationupdates(); log.e(tag, "on connection suspended " + mgoogleapiclient.isconnected()); toast.maketext(getapplicationcontext(), "no network connection", toast.length_long).show(); } @override public void onconnectionfailed(connectionresult connectionresult) { log.e(tag, "connection failed " + connectionresult.tostring()); stoplocationupdates(); log.e(tag, "onconnectionfailed " + mgoogleapiclient.isconnected()); toast.maketext(getapplicationcontext(), "no network connection", toast.length_long).show(); } @override public void onlocationchanged(location location) { log.e(tag, "firing onlocationchanged........."); if(this.timerupdate != null) { timerupdate.location = location; }else{ log.e(tag, "timer null"); } mlastupdatetime = dateformat.gettimeinstance().format(new date()); } protected void stoplocationupdates() { locationservices.fusedlocationapi.removelocationupdates(mgoogleapiclient, this); log.e(tag, "location update stopped"); } @override protected void onpause() { super.onpause(); log.e(tag, "mainactivity paused"); } @override public void onresume() { super.onresume(); log.e(tag, "mainactivity resumed"); if (mgoogleapiclient.isconnected()) { if(!isgoogleplayservicesavailable()){ startactivity(new intent(mainactivity.this, login.class)); toast.maketext(getapplicationcontext(), "please update googleplay servies use application", toast.length_long).show(); finish(); } } } @override public void onstart() { super.onstart(); if(authenticate() == true){ displayuserdetails(); if(this.timerupdate == null) { this.timerupdate = new timerupdate(this, addressops); log.e(tag, "timer created: " + count); timerupdate.starttimer(); } }else{ startactivity(new intent(mainactivity.this, login.class)); finish(); } mgoogleapiclient.connect(); log.e(tag, "mainactivity started, googleapi connection: " + mgoogleapiclient.isconnected()); } @override public void onstop() { super.onstop(); log.e(tag, "mainactivity stopped"); } }
timer class
package temp; import android.content.context; import android.location.location; import android.net.connectivitymanager; import android.net.networkinfo; import android.os.handler; import android.util.log; import android.widget.toast; import java.util.timer; import java.util.timertask; public class timerupdate { private timer timer; private timertask timertask; public boolean timerscheduled = false; private final handler handler = new handler(); private static final string tag = "updatetimer"; addressops addressops; private context maincontext; private userlocalstore userlocalstore; public location location; public timerupdate(context context, addressops ops){ log.e(tag, "constructor"); initializetimertask(); this.maincontext = context; this.addressops = ops; userlocalstore = new userlocalstore(context); } private void initializetimertask(){ log.e(tag, "initializetimertask"); timertask = new timertask() { public void run(){ handler.post(new runnable(){ public void run(){ log.e(tag, "timertask ran"); string status = "5"; update(status); } }); } }; } public void starttimer(){ log.e(tag, "starttimer"); timer = new timer(); timer.schedule(timertask, 1000 * 30, 1000 * 60 * 2); timerscheduled = true; log.e(tag, "start schedule created"); } public void stoptimertask(){ log.e(tag, "stoptimer"); if (timer != null){ timer.cancel(); timer = null; log.e(tag, "timer stopped"); } } public void update(string status) { log.e(tag, "update initiated ............."); if (location != null) { double lat = location.getlatitude(); double lng = location.getlongitude(); if(isnetworkavailable()){ string address = addressops.getaddressstring(lng, lat); if(address != null) { user user = userlocalstore.getloggedinuser(); serverrequest request = new serverrequest(this.maincontext); request.storelocationinbackground(lat, lng, user.username, address, status); toast.maketext(this.maincontext, "longitude: " + lng + "\nlatitude: " + lat + "\naddress: " + address, toast.length_short).show(); }else{ toast.maketext(this.maincontext, "unable retrieve address", toast.length_short).show(); } }else{ toast.maketext(this.maincontext, "no network connection" + "\nlatitude: " + lat + "\nlongitude: " + lng, toast.length_long).show(); } } else { log.e(tag, "there no current location data in update"); toast.maketext(this.maincontext, "there no current location data ....", toast.length_short).show(); } } private boolean isnetworkavailable() { connectivitymanager connectivitymanager = (connectivitymanager)maincontext.getsystemservice(context.connectivity_service); networkinfo activenetworkinfo = connectivitymanager.getactivenetworkinfo(); log.e(tag, "network check"); return activenetworkinfo != null && activenetworkinfo.isconnected(); } }
onstart()
called when "minimizing/maximize" (see details of activity
lifecycle.) if authenticate()
method returns false, timer class re-created blindly. old instance of timer may stick around, depending on doing / registering other components.
Comments
Post a Comment