gpt4 book ai didi

java - 如何在不在 android 中的 Activity 上添加 map fragment 的情况下获取我的位置?

转载 作者:行者123 更新时间:2023-11-30 00:19:50 26 4
gpt4 key购买 nike

我获得了位置权限并创建了服务,但是我不知道如何在不添加 map fragment 的情况下从服务中获取我的位置。我需要获取我的位置并在我的 TextView 上设置文本。//我不需要在这个 Activity 中获取 fragment 我需要在 TextView 中使用 post 方法吗?以及如何从服务中获取位置信息?请帮我!谢谢

这是我的 MainActivity.java 扩展 PermissionActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
switch (compoundButton.getId()) {
case R.id.locationSwitch:
Constants.checkFineLocationPermission(MainActivity.this);
if(compoundButton.isChecked()){
Intent intent = new Intent(MainActivity.this, LocationService.class);
startService(intent);
}else {
Intent intent = new Intent(MainActivity.this, LocationService.class);
stopService(intent);
}
break;
}

}

这是 PermissionActivity。

    public void onRequestPermissionsResult(int requestCode, String permissions[],int[] grantResults){
switch (requestCode){
case MY_PERMISSIONS_REQUEST_FINE_LOCATION:{
if(grantResults.length>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED){
Intent intent = new Intent(getApplicationContext(),getClass());
startActivity(intent);
finish();
}else{
// Log.d("Permission always denyed");
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
.setData(Uri.parse("package:"+ getApplicationContext().getPackageName()));
startActivity(intent);
}
return;
}

}
}

这就是服务

private class LocationListener implements android.location.LocationListener
{
Location mLastLocation;

public LocationListener(String provider)
{
Log.e(TAG, "LocationListener " + provider);
mLastLocation = new Location(provider);
}

@Override
public void onLocationChanged(Location location)
{
Log.e(TAG, "onLocationChanged: " + location);
mLastLocation.set(location);
}

@Override
public void onProviderDisabled(String provider)
{
Log.e(TAG, "onProviderDisabled: " + provider);
}

@Override
public void onProviderEnabled(String provider)
{
Log.e(TAG, "onProviderEnabled: " + provider);
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.e(TAG, "onStatusChanged: " + provider);
}
}

LocationListener[] mLocationListeners = new LocationListener[] {
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
};

@Override
public IBinder onBind(Intent arg0)
{
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}

@Override
public void onCreate()
{
Log.e(TAG, "onCreate");
initializeLocationManager();
try {
mLocationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[1]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
}
try {
mLocationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
mLocationListeners[0]);
} catch (java.lang.SecurityException ex) {
Log.i(TAG, "fail to request location update, ignore", ex);
} catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage());
}
}

@Override
public void onDestroy()
{
Log.e(TAG, "onDestroy");
super.onDestroy();
if (mLocationManager != null) {
for (int i = 0; i < mLocationListeners.length; i++) {
try {
mLocationManager.removeUpdates(mLocationListeners[i]);
} catch (Exception ex) {
Log.i(TAG, "fail to remove location listners, ignore", ex);
}
}
}
}

private void initializeLocationManager() {
Log.e(TAG, "initializeLocationManager");
if (mLocationManager == null) {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}

最佳答案

您应该使用 Google Fused Location Service。它提供了最准确的位置。拿个教程herehere .所以基本上只是把这些东西投入使用。

关于java - 如何在不在 android 中的 Activity 上添加 map fragment 的情况下获取我的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46520756/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com