Android - 获取当前位置

时间:2015-12-05 04:06:50

标签: java android gps location

我正在尝试使用此应用获取当前位置,并将其显示在ID为tv_results的文本视图中。以下是我获取位置的代码:

public class MainActivity extends AppCompatActivity {
TextView tvResults;
LocationManager locationManager;

//GPS can be from the satellites or from networks (routers or cell networks).
//satellites and networks are Providers
//GPS is always accurate than networks.
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tvResults = (TextView) findViewById(R.id.tv_results);

    locationManager = (LocationManager) getSystemService(Service.LOCATION_SERVICE);

    Log.i("HAHA", "GOING!");
    try {
        Log.i("HAHA", "HERE!");
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        Log.i("HAHA", "IS NOW HERE!");
    } catch (SecurityException e) {
        Log.i("PERMISSION_EXCEPTION", "PERMISSION_NOT_GRANTED");
    }

}

@Override
protected void onResume() {
    super.onResume();
    try {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, locationListener);

    } catch (SecurityException e) {
        Log.i("PERMISSION_EXCEPTION", "PERMISSION_NOT_GRANTED");
    }

}

LocationListener locationListener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        tvResults.setText(location.getLatitude() + ", " + location.getLongitude());
        Log.i("HAHA", String.valueOf(location.getLatitude()) + " " + String.valueOf(location.getLongitude()));
        // to get the ways location.getBearing();
    }

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

    }

    @Override
    public void onProviderEnabled(String provider) {
        //if naka bukas na siya, tanggalin nyo na yung error message
    }

    @Override
    public void onProviderDisabled(String provider) {

    }
};

@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_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. ThdroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatemente action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in An
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

以下是我的权限:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

现在,当用户打开应用程序时,您可以看到我正在尝试获取该位置。但是,它没有显示任何内容。什么似乎是问题?

0 个答案:

没有答案