在Android Studio中使用Java实现后台服务获取当前位置
后台服务迁移后位置获取功能失效
此前已在MapsActivity中成功实现获取当前位置并在地图上显示的功能,但将位置获取代码迁移至后台服务MyService后,功能无法正常运行。以下是之前可用的MapsActivity代码:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private GoogleMap mMap; private LocationManager manager; private LocationListener listener; private static final int LOCATION_PERMISSIONS_REQUEST_CODE = 3; private MyService myService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = ActivityMapsBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); // Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.GoogleMap); mapFragment.getMapAsync(this); manager = (LocationManager) getSystemService(LOCATION_SERVICE); listener = new LocationListener() { @Override public void onLocationChanged(@NonNull Location location) { if(mMap!=null) { LatLng currentLocation = new LatLng(location.getLatitude(), location.getLongitude()); mMap.addMarker(new MarkerOptions().position(currentLocation)); mMap.moveCamera(CameraUpdateFactory.newLatLng(currentLocation)); } } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }; getLocation(); //initialize start and cancel button findViewById(R.id.startButton).setOnClickListener(view -> { Intent intent = new Intent(); intent.setClass(getApplicationContext(), MyService.class); //startService(intent); }); findViewById(R.id.cancelButton).setOnClickListener(view -> { Intent serviceIntent = new Intent(this, MyService.class); //stopService(serviceIntent); //return back to main activity Intent mainActivity = new Intent(this, MainActivity.class); startActivity(mainActivity); }); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode==LOCATION_PERMISSIONS_REQUEST_CODE) { int count =0; for(String permission: permissions) { if(permission==android.Manifest.permission.ACCESS_FINE_LOCATION){ if(grantResults[count]==PackageManager.PERMISSION_GRANTED){ getLocation(); } } count++; } } } private void getLocation() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 50, listener); } else { Toast.makeText(this, "Location permission is denied, please allow the permission", Toast.LENGTH_LONG).show(); requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},LOCATION_PERMISSIONS_REQUEST_CODE); } } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; getLocation(); }
内容的提问来源于stack exchange,提问作者Nafsika
相关产品推荐
相关产品推荐

