在设备版本21及更高版本上运行应用程序时出错?

时间:2017-03-03 11:24:05

标签: java android geofencing

我正在实施goofence app来检测用户位置,它不支持21及以上的设备如何为所有设备实现它。

MainActivity.java

     Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

                            if (location == null) {
                                LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, MainActivity.this);

接收用户位置并从中接收更新的代码。
     这是我的主要活动类,如果我添加权限,我也会收到错误      if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

 Error:(100, 64) error: incompatible types: <anonymous ConnectionCallbacks> cannot be converted to Context.
after implementing permission i get error like this.

     Build:gradle(app)


    apply plugin: 'com.android.application'

        android {
            compileSdkVersion 23
            buildToolsVersion "25.0.2"
            defaultConfig {
                applicationId "com.example.barathkumarjt.geo"
                minSdkVersion 14
                targetSdkVersion 23
                versionCode 1
                versionName "1.0"
                testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            }
            buildTypes {
                release {
                    minifyEnabled false
                    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                }
            }
        }

        dependencies {
            compile ('com.android.support:appcompat-v7:23.2.1'){ exclude module: 'support-v4' }
            compile fileTree(dir: 'libs', include: ['*.jar'])
            androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
                exclude group: 'com.android.support', module: 'support-annotations'
            })


            compile'com.android.support:design:23.1.1'
            compile'com.google.android.gms:play-services:8.4.0'
            testCompile 'junit:junit:4.12'
        }

        Manifest file

         <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="com.example.barathkumarjt.geo">
            <uses-permission android:name="android.permission.INTERNET" />
            <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
            <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

            <application
                android:allowBackup="true"
                android:icon="@mipmap/ic_launcher"
                android:label="@string/app_name"
                android:theme="@style/AppTheme.NoActionBar">
                <meta-data
                    android:name="com.google.android.gms.version"
                    android:value="@integer/google_play_services_version" />

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

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

                <service android:name=".GeofenceTransitionsIntentService"  android:exported="true" />
            </application>
        </manifest> 
    Errors

    E/AndroidRuntime: FATAL EXCEPTION: main
                      Process: com.example.barathkumarjt.geo, PID: 711
                      java.lang.SecurityException: Client must have ACCESS_FINE_LOCATION permission to request PRIORITY_HIGH_ACCURACY locations.
                          at android.os.Parcel.readException(Parcel.java:1683)
                          at android.os.Parcel.readException(Parcel.java:1636)
                          at com.google.android.gms.location.internal.zzi$zza$zza.zza(Unknown Source)
                          at com.google.android.gms.location.internal.zzk.zza(Unknown Source)
                          at com.google.android.gms.location.internal.zzl.zza(Unknown Source)
                          at com.google.android.gms.location.internal.zzd$1.zza(Unknown Source)
                          at com.google.android.gms.location.internal.zzd$1.zza(Unknown Source)
                          at com.google.android.gms.common.api.internal.zza$zza.zzb(Unknown Source)
                          at com.google.android.gms.common.api.internal.zzg.zza(Unknown Source)
                          at com.google.android.gms.common.api.internal.zzg.zzb(Unknown Source)
                          at com.google.android.gms.common.api.internal.zzl.zzb(Unknown Source)
                          at com.google.android.gms.common.api.internal.zzj.zzb(Unknown Source)
                          at com.google.android.gms.location.internal.zzd.requestLocationUpdates(Unknown Source)
                          at com.example.barathkumarjt.geo.MainActivity$1.onConnected(MainActivity.java:97)
                          at com.google.android.gms.common.internal.zzk.zzk(Unknown Source)
                          at com.google.android.gms.common.api.internal.zzj.zzi(Unknown Source)
                          at com.google.android.gms.common.api.internal.zzh.zzpx(Unknown Source)
                          at com.google.android.gms.common.api.internal.zzh.onConnected(Unknown Source)
                          at com.google.android.gms.common.api.internal.zzl.onConnected(Unknown Source)
                          at com.google.android.gms.common.api.internal.zzc.onConnected(Unknown Source)
                          at com.google.android.gms.common.internal.zzj$zzg.zzqL(Unknown Source)
                          at com.google.android.gms.common.internal.zzj$zza.zzc(Unknown Source)
                          at com.google.android.gms.common.internal.zzj$zza.zzw(Unknown Source)
                          at com.google.android.gms.common.internal.zzj$zzc.zzqN(Unknown Source)
                          at com.google.android.gms.common.internal.zzj$zzb.handleMessage(Unknown Source)
                          at android.os.Handler.dispatchMessage(Handler.java:102)
                          at android.os.Looper.loop(Looper.java:154)
                          at android.app.ActivityThread.main(ActivityThread.java:6077)
                          at java.lang.reflect.Method.invoke(Native Method)
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

        My app is running on the device Api level 4.4.4 and while running on devices 21 and above the app is opening and get closed suddenly and i want to implement this app that support all devices please help me to implement it.
        Thanks and help in advance! 

2 个答案:

答案 0 :(得分:0)

问题是您需要启用运行时权限才能促进此类操作。

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

      ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
            12000);

}

请阅读此页:https://developer.android.com/training/permissions/requesting.html 当应用程序变得可见时执行此检查,

答案 1 :(得分:0)

  1. 首先调用checkSelfPermission:

    如果授予了权限,则获取位置
    否则,转到下面的第2步。

  2. 请求许可:

    int PERMISSION_REQ_LOCATION = 0100;
    
    requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQ_LOCATION);
    
  3. 在请求权限后重写此方法以检查结果:

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQ_LOCATION:  
            //TODO fetch location
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }