java - Sending GPS Coordinates via sms -
i new android. trying develop app fetch users current gps coordinates , send them via sms hardcoded number. using 2 activities, first 1 has progress bar , text( processing, please wait),it fetching gps coordinates in background. second 1 receiving coordinates first activity , sending sms. problem m facing in mainactivity.java file condition if(latitude>0) never satisfied since gps take time coordinates , cant go second activity. m attaching code, please me on this. can edit code if u like. in advance.
package com.shaji.smartcab; import com.shaji.smartcab.mainactivity; import com.shaji.smartcab.mylocationlistener; import com.shaji.smartcab.r; import android.os.bundle; import android.app.activity; import android.view.menu; import android.widget.textview; import android.widget.toast; import android.location.locationlistener; import android.content.context; import android.content.intent; import android.location.locationmanager; public class mainactivity extends activiy{ double lat; double lon; string coordinates; string coor; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); textview tv= (textview) findviewbyid(r.id.textview1); locationmanager mlocmanager=null; locationlistener mloclistener; mlocmanager = (locationmanager)getsystemservice(context.location_service); mloclistener = new mylocationlistener(); mlocmanager.requestlocationupdates( locationmanager.gps_provider, 600,10, mloclistener); if (mlocmanager.isproviderenabled(locationmanager.gps_provider)) { if(mylocationlistener.latitude>0) { lat = mylocationlistener.latitude; lon = mylocationlistener.longitude; tv.settext("m fetching coordinates"); coordinates= lat+","+lon; intent intent = new intent(mainactivity.this, sec.class); intent.putextra(coor, coordinates); startactivity(intent); //tv1.settext("latitude:" + mylocationlistener.latitude + ",longitude:" + mylocationlistener.longitude ); }} else { context context = getapplicationcontext(); charsequence text = "please turn on gps , try again later!"; int duration = toast.length_short; toast toast = toast.maketext(context, text, duration); toast.show(); tv.settext(" "); } }; @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; }}
enter code here
package com.shaji.smartcab;
import android.location.location; import android.location.locationlistener; import android.os.bundle; public class mylocationlistener implements locationlistener { public static double latitude; public static double longitude; @override public void onlocationchanged(location loc) { loc.getlatitude(); loc.getlongitude(); latitude=loc.getlatitude(); longitude=loc.getlongitude(); } @override public void onproviderdisabled(string provider) { //print "currently gps disabled"; } @override public void onproviderenabled(string provider) { //print "gps got enabled"; } @override public void onstatuschanged(string provider, int status, bundle extras) { } } //this second activity. package com.shaji.smartcab; import com.shaji.smartcab.r; import android.app.activity; import android.os.bundle; import android.telephony.smsmanager; import android.view.view; import android.widget.button; import android.widget.toast; public class sec extends activity { @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.second); final string sms = (string) getintent().getextras().get("coor"); button c = (button) findviewbyid(r.id.button1); c.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { string phoneno = "03136166588"; //string sms = "coordinates"; try { smsmanager smsmanager = smsmanager.getdefault(); smsmanager.sendtextmessage(phoneno, null, sms, null, null); toast.maketext(getapplicationcontext(), "request sent!", toast.length_long).show(); } catch (exception e) { toast.maketext(getapplicationcontext(), " error, please try again later!", toast.length_long).show(); e.printstacktrace(); } }}); }}
i think problem is, location update not done before call next activity.
before updates, co-ordinates using 1 of 3 providers. refer following code.
private location getuserlocation() { location_manager = (locationmanager) getsystemservice(location_service); if (location_manager != null) { list<string > provider=location_manager .getallproviders(); location location = null; for(int i=0;i<provider.size();i++){ location=location_manager .getlastknownlocation(provider.get(i)); if(location!=null){ location_manager.requestlocationupdates(provider.get(i), min_time_bw_updates, min_distance_change_for_updates,mloclistener); break; } } return location; } }
Comments
Post a Comment