事情就是这样。我使用了与我在单独的类中所做的相同的功能,该功能仅包含特定于 gps 的功能,仅供试用。它有效,尽管我必须说坐标值不正确。现在我尝试在我的真实项目中实现相同的功能,但它不起作用。它确实检测到我是否在手机中启用或禁用了 GPS,但没有出现协调值。
对于代码,我做了两个类。Froyo2Activity 和 MyLocationListener。Froyo2Activity 是仅扩展 Activity 和 MyLocationListener 实现 LocationListener 的主要活动。为了更好地查看,这是我的代码。
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---
这是 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;
}
}
有谁知道如何解决这个问题。就像我之前说的,当我用自己的类对 gps 功能进行试用时;意思是没有实现其他功能或活动,特别是仅针对 GPS 功能测试的类,它确实有效 - 至少指出了位置的数量。但是,在用其他功能实现 GPS 时,它说信号不可用(请参阅代码)。如果这个问题能得到解决,那将是有帮助的。
感谢提前。(萨拉布朗)