How to debug Android GPS tracking project? -
i posted question: how track gps if reaches particular location or around 20 meters? that, learned how start, created code track.
activity:
import java.text.decimalformat; import java.text.numberformat; import android.app.activity; import android.app.pendingintent; import android.content.context; import android.content.intent; import android.content.intentfilter; import android.content.sharedpreferences; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.toast; public class proxalertactivity extends activity { private static final long minimum_distancechange_for_update = 1; // in meters private static final long minimum_time_between_update = 1000; // in milliseconds private static final long point_radius = 500; // in meters private static final long prox_alert_expiration = -1; private static final string point_latitude_key = "point_latitude_key"; private static final string point_longitude_key = "point_longitude_key"; private static final string prox_alert_intent = "com.javacodegeeks.android.lbs.proximityalert"; private static final numberformat nf = new decimalformat("##.########"); private locationmanager locationmanager; private edittext latitudeedittext; private edittext longitudeedittext; private button findcoordinatesbutton; private button savepointbutton; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); locationmanager = (locationmanager) getsystemservice(context.location_service); locationmanager.requestlocationupdates( locationmanager.gps_provider, minimum_time_between_update, minimum_distancechange_for_update, new mylocationlistener() ); latitudeedittext = (edittext) findviewbyid(r.id.point_latitude); latitudeedittext.settext("13.030729"); longitudeedittext = (edittext) findviewbyid(r.id.point_longitude); longitudeedittext.settext("80.208975"); findcoordinatesbutton = (button) findviewbyid(r.id.find_coordinates_button); findcoordinatesbutton.setvisibility(view.gone); savepointbutton = (button) findviewbyid(r.id.save_point_button); findcoordinatesbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { populatecoordinatesfromlastknownlocation(); } }); savepointbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { saveproximityalertpoint(); } }); } private void saveproximityalertpoint() { location location = locationmanager.getlastknownlocation(locationmanager.gps_provider); if (location==null) { toast.maketext(this, "no last known location. aborting...", toast.length_long).show(); return; } savecoordinatesinpreferences((float)location.getlatitude(), (float)location.getlongitude()); addproximityalert(location.getlatitude(), location.getlongitude()); } private void addproximityalert(double latitude, double longitude) { intent intent = new intent(prox_alert_intent); pendingintent proximityintent = pendingintent.getbroadcast(this, 0, intent, 0); locationmanager.addproximityalert( latitude, // latitude of central point of alert region longitude, // longitude of central point of alert region point_radius, // radius of central point of alert region, in meters prox_alert_expiration, // time proximity alert, in milliseconds, or -1 indicate no expiration proximityintent // used generate intent fire when entry or exit alert region detected ); intentfilter filter = new intentfilter(prox_alert_intent); registerreceiver(new proximityintentreceiver(), filter); } private void populatecoordinatesfromlastknownlocation() { location location = locationmanager.getlastknownlocation(locationmanager.gps_provider); if (location!=null) { latitudeedittext.settext(nf.format(location.getlatitude())); longitudeedittext.settext(nf.format(location.getlongitude())); } } private void savecoordinatesinpreferences(float latitude, float longitude) { sharedpreferences prefs = this.getsharedpreferences(getclass().getsimplename(), context.mode_private); sharedpreferences.editor prefseditor = prefs.edit(); prefseditor.putfloat(point_latitude_key, latitude); prefseditor.putfloat(point_longitude_key, longitude); prefseditor.commit(); } private location retrievelocationfrompreferences() { sharedpreferences prefs = this.getsharedpreferences(getclass().getsimplename(), context.mode_private); location location = new location("point_location"); location.setlatitude(prefs.getfloat(point_latitude_key, 0)); location.setlongitude(prefs.getfloat(point_longitude_key, 0)); return location; } public class mylocationlistener implements locationlistener { public void onlocationchanged(location location) { location pointlocation = retrievelocationfrompreferences(); float distance = location.distanceto(pointlocation); toast.maketext(proxalertactivity.this, "distance point:"+distance, toast.length_short).show(); } public void onstatuschanged(string s, int i, bundle b) { } public void onproviderdisabled(string s) { } public void onproviderenabled(string s) { } } }
broadcast:
import android.app.notification; import android.app.notificationmanager; import android.app.pendingintent; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.graphics.color; import android.location.locationmanager; import android.util.log; public class proximityintentreceiver extends broadcastreceiver { private static final int notification_id = 1000; @suppresswarnings("deprecation") @override public void onreceive(context context, intent intent) { string key = locationmanager.key_proximity_entering; boolean entering = intent.getbooleanextra(key, false); if (entering) { log.d(getclass().getsimplename(), "entering"); } else { log.d(getclass().getsimplename(), "exiting"); } notificationmanager notificationmanager = (notificationmanager) context.getsystemservice(context.notification_service); pendingintent pendingintent = pendingintent.getactivity(context, 0, null, 0); notification notification = createnotification(); notification.setlatesteventinfo(context, "proximity alert!", "you near point of interest.", pendingintent); notificationmanager.notify(notification_id, notification); } private notification createnotification() { notification notification = new notification(); notification.icon = r.drawable.ic_menu_notifications; notification.when = system.currenttimemillis(); notification.flags |= notification.flag_auto_cancel; notification.flags |= notification.flag_show_lights; notification.defaults |= notification.default_vibrate; notification.defaults |= notification.default_lights; notification.ledargb = color.white; notification.ledonms = 1500; notification.ledoffms = 1500; return notification; } }
manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.javacodegeeks.android.lbs" android:versioncode="1" android:versionname="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".proxalertactivity" android:label="mytrack"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> <uses-sdk android:minsdkversion="3" /> <uses-permission android:name="android.permission.access_fine_location" /> <uses-permission android:name="android.permission.access_mock_location" /> <uses-permission android:name="android.permission.access_coarse_location" /> <uses-permission android:name="android.permission.vibrate" /> </manifest>
i gave static location that, entering 500 meter radius, guess alert. after starting this, gone more 1000 meters , started returning.
but didn't alert until reach place.
use google services location api location. locationmanager class create problem while giving location.
code goes below..
add these permissions in manifest..
<uses-permission android:name="android.permission.access_fine_location" /> <uses-permission android:name="android.permission.access_coarse_location" /> <uses-permission android:name="android.permission.internet" />
add following code under application tag in manifest file..
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
java code goes here..
package com.example.location_test; import android.app.activity; import android.location.location; import android.os.bundle; import android.widget.button; import android.widget.edittext; import android.widget.toast; import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.googleplayservicesclient; import com.google.android.gms.location.locationclient; import com.google.android.gms.location.locationlistener; import com.google.android.gms.location.locationrequest; public class activity1 extends activity implements googleplayservicesclient.connectioncallbacks, googleplayservicesclient.onconnectionfailedlistener, locationlistener { edittext e1, e2; button b; locationclient mlocationclient; location mcurrentlocation; locationrequest mlocationrequest; // milliseconds per second private static final int milliseconds_per_second = 1000; // update frequency in seconds public static final int update_interval_in_seconds = 5; // update frequency in milliseconds private static final long update_interval = milliseconds_per_second * update_interval_in_seconds; // fastest update frequency, in seconds private static final int fastest_interval_in_seconds = 1; // fast frequency ceiling in milliseconds private static final long fastest_interval = milliseconds_per_second * fastest_interval_in_seconds; private static final string provider = "flp"; private static final float accuracy = 3.0f; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity1); b = (button) findviewbyid(r.id.b); e1 = (edittext) findviewbyid(r.id.e1); e2 = (edittext) findviewbyid(r.id.e2); try { // create locationrequest object mlocationrequest = locationrequest.create(); // use high accuracy mlocationrequest.setpriority(locationrequest.priority_high_accuracy); // set update interval 5 seconds mlocationrequest.setinterval(update_interval); // set fastest update interval 1 second mlocationrequest.setfastestinterval(fastest_interval); mlocationclient = new locationclient(activity1.this, activity1.this, activity1.this); mlocationclient.connect(); // mlocationclient.setmockmode(true); } catch (exception e) { e.printstacktrace(); } } @override public void onconnectionfailed(connectionresult arg0) { // todo auto-generated method stub toast.maketext(this, "connection failed.", toast.length_short).show(); } @override public void onconnected(bundle arg0) { // todo auto-generated method stub toast.maketext(this, "connected", toast.length_short).show(); mlocationclient.requestlocationupdates(mlocationrequest, this); } @override public void ondisconnected() { // todo auto-generated method stub toast.maketext(this, "connection disconnected.", toast.length_short) .show(); } @override public void onlocationchanged(location location) { // todo auto-generated method stub string msg = "updated location: " + double.tostring(location.getlatitude()) + "," + double.tostring(location.getlongitude()); toast.maketext(this, msg, toast.length_short).show(); } }
Comments
Post a Comment