【问题标题】:MapsActivity.java doesn't work on android versions higher than 6.0MapsActivity.java 不适用于高于 6.0 的 android 版本
【发布时间】:2019-02-18 07:31:49
【问题描述】:

我正在开发地图活动。我的代码在 android 4.4 上正常工作,但在另一台 6.0 设备上不起作用。

即使我在 onCreate() 下编写的 toast 也不行。也就是说,mapsActivity.java 不起作用。我不知道为什么。下面是我的 java、manifest 和 gradle。

谢谢。

ma​​psActivity.java

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    ActionBar bar = getActionBar();
    bar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.colorPrimary)));
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; }
    Toast.makeText(this, "ok", Toast.LENGTH_SHORT).show(); //toast and the rest of the code doesn't work on 6.0 and high.
    database = FirebaseDatabase.getInstance();
    ref = database.getReference("ref");

我的 manifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true">

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

我的 gradle.build

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
compileSdkVersion 27
defaultConfig {
    applicationId "com.android.app"
    minSdkVersion 17
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
     }
   }

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.google.android.gms:play-services-maps:16.1.0'
testImplementation 'junit:junit:4.12'

androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-  core:3.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.firebase:firebase-database:16.0.1'
  }

【问题讨论】:

  • 您需要调试您的应用程序以了解它为什么不起作用
  • 好的,我知道了。那是因为如果你没有承诺,你会做return;。仅检查权限是不够的,您还必须请求它们
  • 是什么导致应用崩溃?

标签: java android version android-manifest


【解决方案1】:

它无法运行,因为在 android 6.0 marshmallow 中,您需要询问运行时权限以获得危险权限,您可以查看此https://developer.android.com/guide/topics/permissions/overview 所以它确实可以在 kitkat 上工作,因为已经给出了权限,并且在运行 6.0 的设备中执行了以下代码,从而停止了代码执行。

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; }

您可以这样请求许可:

 ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                1);

并像这样处理它的结果:

 @Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1: {

          // If request is cancelled, the result arrays are empty.
          if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.          
            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }}

【讨论】:

  • 在句柄代码中,“PackageManager.ACCESS_FINE_LOCATION”给出错误。 PackageManager 下没有“ACCESS_FINE_LOCATION”。
  • 对不起它的 PackageManager.PermissionGranted
【解决方案2】:

尝试在您的第一个活动的 onCreate 方法中添加此代码,如果仍然存在任何错误,请随时再次与我联系。这将检查 LOCATION 权限,并在未获得授权的情况下要求提供。

自 Android M(API 23) 以来,每个 Android 应用都需要运行时权限

Here, 'this' is the current activity

if ((ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION))
        != PackageManager.PERMISSION_GRANTED) {

// Permission is not granted
// Should we show an explanation?

if (ActivityCompat.shouldShowRequestPermissionRationale(this,"Manifest.permission.ACCESS_FINE_LOCATION")) {

    // Show an explanation to the user *asynchronously* -- don't block
    // this thread waiting for the user's response! After the user
    // sees the explanation, try again to request the permission.

} else {

    // No explanation needed; request the permission
    ActivityCompat.requestPermissions(this,
                new String[]{"Manifest.permission.ACCESS_FINE_LOCATION"},
                REQUEST_CODE);

     // REQUEST_CODE is an
     // app-defined int constant. The callback method gets the
     // result of the request.
  }
}

else {
        // Permission has already been granted
}

【讨论】:

  • 谢谢。在我在检查之间请求许可后,它已修复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-09
  • 1970-01-01
  • 2014-06-14
相关资源
最近更新 更多