让gps无法正常工作

时间:2015-10-27 18:29:04

标签: android gps

有人可以解释我为什么我的代码无效吗? 我试图让gps像posible一样简单。调试器没有发现任何错误,但是当我运行它时,它不起作用。我可以启用GPS并禁用GPS但从不gps纬度和经度..

这里是代码:

public class getgps extends AppCompatActivity {

double longtitude;
double latitude;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_getgps);
    LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    LocationListener mlocListener = new MyLocationListener();
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, mlocListener);


}


public class MyLocationListener implements LocationListener
{
    @Override
    public void onLocationChanged(Location loc)
    {

        latitude = loc.getLatitude();
        longtitude = loc.getLongitude();
        String Text = "My current location is: " +
                "Latitude = " + latitude +
                "Longitude = " + longtitude;
        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)
    {
        Toast.makeText( getApplicationContext(), + latitude +" ",
        Toast.LENGTH_SHORT).show();

    }

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_getgps, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

感谢您的想法。 ^ _-

1 个答案:

答案 0 :(得分:0)

正如我在上面的评论中所提到的,请确保您至少在开放天空下测试应用一次。只有这样GPS才能得到修复并获得GPS数据。