I tracked the gps with the following code, But How to get notified If the gps tracker reaches that location ...
Capture.java
public class Capture extends Activity implements LocationListener{
Button btnNWShowLocation,btnsave;
TextView etlat,etlng,alreadyposted,t3,t4,t5;
EditText etplace;
String state1,state2;
protected String v1, v2, v3;
JSONParser jsonParser = new JSONParser();
JSONArray jsonarray;
ArrayList<HashMap<String,String>> arraylist;
JSONObject jsonobject;
String TAG_SUCCESS="success";
double latitude,longitude;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
double lat1,lng1;
String lat2,lng2;
String providernet,speed1,accuracy1;
float speed,accuracy;
Location location;
public LocationManager locationManager;
String provider;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ThreadPolicy tp = ThreadPolicy.LAX;
StrictMode.setThreadPolicy(tp);
btnsave = (Button) findViewById(R.id.buttonsave);
etlat=(TextView)findViewById(R.id.textlatitude);
etlng=(TextView)findViewById(R.id.textlongitude);
alreadyposted=(TextView)findViewById(R.id.alreadyposted);
etplace=(EditText)findViewById(R.id.editplace);
t3=(TextView)findViewById(R.id.provider);
t4=(TextView)findViewById(R.id.speed);
t5=(TextView)findViewById(R.id.accuracyset);
alreadyposted.setVisibility(View.INVISIBLE);
locationManager = (LocationManager)Capture.this.getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setCostAllowed(false);
provider = locationManager.getBestProvider(criteria,false);
Location location = locationManager.getLastKnownLocation(provider);
//Initialize the location fields
if (location!= null){
onLocationChanged(location);
latitude = location.getLatitude();
longitude =location.getLongitude();
} else {
Toast.makeText(getApplicationContext(), "Not available", Toast.LENGTH_SHORT).show();
}
btnsave.setOnClickListener(new View.OnClickListener() {
protected ArrayList<NameValuePair> parameters;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
parameters = new ArrayList<NameValuePair>();
v1 = etlat.getText().toString();
v2 = etlng.getText().toString();
v3 = etplace.getText().toString();
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("http://placementreadyguarantee.com/android/gps_tracking/Jsoncheckcapture.php?latitude="+v1+"&longitude="+v2);
try {
// Locate the array name in JSON
int success = jsonobject.getInt(TAG_SUCCESS);
if(success==1){
alreadyposted.setVisibility(View.VISIBLE);
}
else{
NameValuePair latitudejson = new BasicNameValuePair("latitude",v1);
NameValuePair longitudejson = new BasicNameValuePair("longitude",v2);
NameValuePair placejson = new BasicNameValuePair("place",v3);
parameters.add(latitudejson);
parameters.add(longitudejson);
parameters.add(placejson);
try {
JSONObject json =jsonParser.makeHttpRequest("http://placementreadyguarantee.com/android/gps_tracking/captureupload.php?latitude="
+URLEncoder.encode(v1,"UTF-8")
+"&longitude="
+URLEncoder.encode(v2,"UTF-8")
+"&place="
+URLEncoder.encode(v3,"UTF-8"),
"POST",parameters);
} catch (UnsupportedEncodingException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
alreadyposted.setVisibility(View.INVISIBLE);
}
}catch(Exception e){
e.printStackTrace();
}
}
});
}
/*Request updates at startup */
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider,400,1,this);
//getting gps
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(isGPSEnabled){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,400,1,this);
}
else if(isNetworkEnabled){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,400,1,this);
}
}
/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause(){
super.onPause();
//locationManager.removeUpdates(this);
locationManager.requestLocationUpdates(provider,400,1,this);
//getting gps
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(isGPSEnabled){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,400,1,this);
}
else if(isNetworkEnabled){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,400,1,this);
}
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
lat1=location.getLatitude();
lng1=location.getLongitude();
providernet=location.getProvider();
speed=location.getSpeed();
accuracy=location.getAccuracy();
accuracy1=Float.toString(accuracy);
speed1=Float.toString(speed);
lat2=Double.toString(lat1);
lng2=Double.toString(lng1);
etlat.setText(lat2);
etlng.setText(lng2);
t3.setText(providernet);
t4.setText(speed1);
t5.setText(accuracy1);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
In this i used json to store the latitude and longitude in mysql database(which is in my server space).
By clicking on button it saved current latitude and longitude in database .
Now i need to Do more , with this if the device reached that already saved place or around 10 meters or 20 meteers it send notification , or storing in database like that anything i need to do .
Thanks in advance . :)