GPS坐标未传递给仿真器

时间:2012-03-02 13:11:43

标签: android gps

我尝试通过DDMS manualy和GPX传递GPS坐标。没什么作用@我的结局。始终显示“GPS可用性差”。从调试我看到GPS已启用。 感谢。

public class SurroundgpsActivity extends Activity implements LocationListener {
/** Called when the activity is first created. */

//For GPS
Timer timer = null;
TimerTask scanTask = null;

private LocationManager locationManager;
private Location location;
private Button BtnGpsCoord=null;

private String gpsFolder = "/sdcard/assignment1/gps1/";

//MapView mapView = (MapView) findViewById(R.id.mapview);    
//mapView.setBuiltInZoomControls(true);

protected boolean isRouteDisplayed() {
    return false;
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    BtnGpsCoord=(Button) findViewById(R.id.button1);

    BtnGpsCoord.setOnClickListener(new View.OnClickListener() {

        public void onClick(View V) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Getting GPS...", Toast.LENGTH_LONG).show();
            final Handler handler=new Handler();
            timer =new Timer();

            scanTask = new TimerTask(){

                public void run() 
                {
                    handler.post(new Runnable() 
                    {
                        public void run() 
                        {
                            getGpsAutomatically();
                            System.out.println("Calling getGpsAutomatically");
                        }
                   });
                }
            };
            // setting the timer of 10 minutes
               timer.schedule(scanTask, 300, 600000);
            }
        });
    }

private void getGpsAutomatically()
{   
    // Get the location manager
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    System.out.println("\nisGpsEnabled:"+isGpsEnabled);

    if (isGpsEnabled == false)
    {
        String err_msg = "GPS is disabled, Kindly enable it.";
        showGPSError(err_msg);                  
    }
    else
    {   
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);

        //String provider = locationManager.getBestProvider(criteria, true);    
        String provider = LocationManager.GPS_PROVIDER;
        locationManager.requestLocationUpdates(provider, 1000L, 500.0f, this);

        location = locationManager.getLastKnownLocation(provider);

        System.out.println("\nLocation:"+location);
        try 
        {               
            printGpsLocation(location);     
        }       
        catch (IOException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }
}
private void printGpsLocation(Location location)  throws IOException
{       
    FileOutputStream fOut = null;
    OutputStreamWriter osw = null;

    if (location != null)
    {
        double lat = location.getLatitude();
        double lng = location.getLongitude();

        String destination ="latitude = "+lat+";longitude = "+lng;
        fOut = openFileOutput(gpsFolder + "location.txt", Context.MODE_WORLD_WRITEABLE);

        osw = new OutputStreamWriter(fOut);

        osw.write(destination);
        osw.flush();
        osw.close();

        fOut.close();               

        locationManager.removeUpdates(this);
        location.reset();      
    }
    else
    {   
        String err_msg = "GPS is working poorly.";
        showGPSError(err_msg);  
    }
}

private void showGPSError(String errorMsg)
{       
    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Error");
    alertDialog.setMessage(errorMsg);
    alertDialog.setButton("OK", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int which) 
        {               
            return;
        }
    });     
    alertDialog.show();
}           

@Override
public void onLocationChanged(Location location) 
{
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    String provider = locationManager.getBestProvider(criteria, true);

    location = locationManager.getLastKnownLocation(provider);
    locationManager.requestLocationUpdates(provider, 1000L, 500.0f, this);      
}

@Override
public void onProviderDisabled(String provider) 
{
    // TODO Auto-generated method stub      
}

@Override
public void onProviderEnabled(String provider) 
{
    // TODO Auto-generated method stub      
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) 
{
    // TODO Auto-generated method stub      
}

@Override
public void onDestroy() 
{       
    super.onDestroy();
}            

}

2 个答案:

答案 0 :(得分:0)

找到同样的问题和一些有用的答案......

DDMS not able to send location to emulator

希望这会有所帮助.......

答案 1 :(得分:0)

如果您正在使用Eclipse,请转到Window-> Open Perspective-> DDMS,然后在位置控件中输入一个并点击发送。