Design Patterns for Android -- Ensure GPS Location is set -


i'm writing android app uses gps location. code simple.

public class myactivitything extends activity {  location                    l; mylocationlistener          locationlistener; locationmanager             locationmanager;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      locationmanager = (locationmanager)getsystemservice(context.location_service);     locationlistener = new mylocationlistener();     locationmanager.requestlocationupdates(locationmanager.gps_provider, 5000, 10, locationlistener);   }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.car, menu);     return true; }  public void sethotcoldlocation(view view) {     l = locationlistener.getcurrentlocation(); } } 

here mylocationlistener

public class mylocationlistener implements locationlistener {  private static location thelocation; @override public void onlocationchanged(location location) {      thelocation = location; }  @override public void onstatuschanged(string provider, int status, bundle extras) {  }  @override public void onproviderenabled(string provider) {  }  @override public void onproviderdisabled(string provider) {  }  public location getcurrentlocation() {     return thelocation; } } 

my situation this. have button onclick calls sethotcoldlocation method in myactivitything.

my question is, how should ensure location set when user presses button? onlocationchanged method not called immediately, when app first starts can press button , cause exception....

should make button invisible until location called? should enclose contents of sethotcoldlocation in try catch , throw away users clicks until location set? can think of tons of ways make work, don't know best one...or right one.

any help?

another way put button can start task sets location.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -