GPS上的Android问题

时间:2011-09-23 14:18:55

标签: android gps

我已经尝试了大约3天来解决这个问题,到目前为止我还没有找到解决方案。我想通过GPS获取我的位置。 它似乎没有收到我尝试通过cmd.exe或DDMS中的模拟器控件发送的coordonates。此外,它重新启动模拟器。我一直在尝试的代码如下。如果有人可以帮助我那么请做,因为我没有找到任何解决这个问题的方法。谢谢!

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class Check_Location extends Activity {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

LocationManager locationManager = (LocationManager)       this.getSystemService(Context.LOCATION_SERVICE);

LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {

TextView tv=(TextView) findViewById(R.id.textView1);
tv.setText(location.getLongitude()+"  "+location.getLatitude());
}

public void onStatusChanged(String provider, int status, Bundle extras) {}

public void onProviderEnabled(String provider) {}

public void onProviderDisabled(String provider) {}
};

//Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,   locationListener);
}
}

我尝试过但也没有用的另一种方法如下:

  import android.app.Activity;
  import android.content.Context;
  import android.location.Criteria;
  import android.location.Location;
  import android.location.LocationListener;
  import android.location.LocationManager;
  import android.os.Bundle;
  import android.util.Log;
  import android.widget.TextView;
  import android.widget.Toast;

 public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

}

/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
    @Override
    public void onLocationChanged(Location loc)
    {
        loc.getLatitude();
        loc.getLongitude();

        String Text = "My current location is: " +
                      "Latitude = " + loc.getLatitude() +
                      "Longitude = " + loc.getLongitude();

        Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onProviderDisabled(String provider)
    {
        Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
    }

    @Override
    public void onProviderEnabled(String provider)
    {
        Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras)
    {
    }

}/* End of Class MyLocationListener */
}

为两者设定的许可是:

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES"></uses-permission>

1 个答案:

答案 0 :(得分:0)

我设法解决了我的问题。我在其他论坛上看过。这两个代码都在工作,但尝试使用低于10级的Android平台。例如,为8级创建一个模拟器。它可以工作!