here’s the thing. i used the same function i did in separated class that only contain specific for gps just for try out. it works although i must say the coordinate value is incorrect. now i tried to implement the same function in my real project and it is not working. it did detect whether i enable or disable the GPS in my phone but the are no values of coordination appears.
for the code, i made two classes. Froyo2Activity and MyLocationListener. Froyo2Activity is the main activity which only extends Activity and MyLocationListener implements LocationListener. For better view, here’s my code.
public class Froyo2Activity extends Activity{
Spinner priorSpin, genderSpin;
ImageView femaleView, maleView;
Button submit, agePrev, ageNext, bpPrev, bpNext, pPrev, pNext, butLoc;
SeekBar seekAge, seekBP;
EditText ageEdit, bpEdit, pEdit, locEdit, fname, lname;
TextView tv;
String commandline="", ageString="", bpString="", pString="";
int age = 0, bp = 0, p = 0;
double latitude, longitude, altitude, pitch = 0;
private static final int REQUEST_CODE = 10;
private LocationManager locationManager;
private String provider;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_froyo2);
fname = (EditText) findViewById(R.id.editText1);
lname = (EditText) findViewById(R.id.editText2);
ageEdit = (EditText) findViewById(R.id.editText7);
pEdit = (EditText) findViewById(R.id.editText9);
locEdit = (EditText) findViewById(R.id.editText3);
femaleView = (ImageView)findViewById(R.id.femaleView);
maleView = (ImageView)findViewById(R.id.maleView);
priorSpin = (Spinner) findViewById(R.id.prior_spin);
genderSpin = (Spinner)findViewById(R.id.gender_spin);
tv = (TextView) findViewById(R.id.textView14);
//---set the GPS---
//Get the location manager
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
if(MyLocationListener.latitude>0)
{
latitude=MyLocationListener.latitude;
longitude=MyLocationListener.longitude;
tv.setText("Latitude: "+ Double.toString(latitude) +" Longitude: "+ Double.toString(longitude)+" Altitude:"+ Double.toString(pitch)+"");
}
else
{
tv.setText("Signal Not Available");
}
} else {
tv.setText("GPS is not turned on...");
}
//---end GPS---
this is the code for MyLocationListener
package com.example.froyo2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.widget.TextView;
import android.widget.Toast;
public class MyLocationListener implements LocationListener {
public static double latitude;
public static double longitude;
public static double salah=90;
@Override
public void onLocationChanged(Location loc)
{
// loc.getLatitude();
// loc.getLongitude();
latitude=loc.getLatitude();
longitude=loc.getLongitude();
salah=1;
}
@Override
public void onProviderDisabled(String provider)
{
salah=2;
}
@Override
public void onProviderEnabled(String provider)
{
salah=3;
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
salah=4;
}
}
does anyone knows how to solve this. like i said before, when im doing tryout with the gps function with its own class; meaning without implementing other function or activity and specifically the class tested only for GPS function, it does work – at least pointing out the number of the location. however, when implementing the GPS with other function, it said the Signal Not Available (please refer to the code). It would be helpful if this matter could be solved.
Thanks for advance.
(SaraBrown)
Use below code:
For locationListener you may use this (Put this inside your Froyo2Activity):
I have used this in my code and it works quite well.