我在Android Studio中使用地图和地理位置创建了一个应用程序,并取得了巨大的成功。我最近将手机(Samsung J6 Plus)更新为Pie 9.0,但现在该应用程序无法在手机或Android Studio中的虚拟设备上加载。但是,当我的手机和虚拟设备设置为飞行模式时,它将加载。
我已将Android Studio更新至3.4.1,并更新了支持存储库SDK工具,SDK平台工具,仿真器和SDK生成工具29-rc3。
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
//Changed from Stackflow - public class AR_MapsActivity extends FragmentActivity implements OnMapReadyCallback
public class AR_MapsActivity extends AppCompatActivity implements OnMapReadyCallback {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
private GoogleMap mMap;
LocationListener locationListener;
LocationManager locationManager;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) ;
{
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
;
{
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 30, 5, locationListener);
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ar_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//RM Adding the code from the location activity Lecture 97 LocationManager and listener
locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
//Added a Toast - for demo and then added the code for the location
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// Add a marker in Sydney and move the camera
LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());
mMap.clear();
mMap.addMarker(new MarkerOptions().position(userLocation).title("AR Projects HQ"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 16));
//Added 06-03-2019 to add another pointer on map
LatLng randy = new LatLng(50.877644, -2.108941);
mMap.addMarker(new MarkerOptions().position(randy).title("Randy HQ"));
LatLng roddy = new LatLng(50.876722, -2.101839);
mMap.addMarker(new MarkerOptions().position(roddy).title("Roddy HQ"));
//Hardcoded another pointer on the map
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
//RM - Adding code from location activity
//RM - WAS - if (Build.VERSION.SDK_INT < 28)
if (Build.VERSION.SDK_INT < 28)
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30, 5, locationListener);
} else {
//RM - Checks for permission from phone
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30, 5, locationListener);
Location lastKnownLocation = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
LatLng userLocation = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
mMap.clear();
mMap.addMarker(new MarkerOptions().position(userLocation).title("AR Projects HQ"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 16));
}
}
}
}
该应用程序将提示您请求位置,但随后崩溃,该消息将显示该应用程序不断停止。在我的手机和虚拟仿真器上都是如此,这是在Pie 9.0 API 28上设置的。但是,该应用程序将在选择飞行模式时加载,但由于这是基于地图的应用程序,因此不会显示地图。
答案 0 :(得分:0)
这是一个盲目的答案,因为您没有提供日志,但是您可能会遇到一个常见问题, 如果您的应用程序的目标是API级别28(Android 9.0)或更高版本,并且您使用的是play-services-maps依赖版本16.0.0或更低版本,则必须在AndroidManifest文件中实现它:
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
如果您使用
,将为您处理com.google.android.gms:play-services-maps:16.1.0
这是完整的文档:
答案 1 :(得分:0)
谢谢,我在之后添加了代码
</activity>
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
</application>