Android无法解析符号OnRequestPermissionsResultCallback

时间:2016-03-02 17:52:42

标签: android xml google-maps android-fragments gps

我是Android的新手,我正在尝试构建一个应用程序来显示我当前的位置,并且我正在跟踪来自google链接here的apidemos中的设置。这是作业的一部分,所以我不想透露我的最终目标是什么。现在我希望能够在我的应用程序上运行gps位置。我将发布我的活动代码,xml布局代码,清单代码和gradle代码。如果还有什么需要发布的,请告诉我。我试过搜索,但我找不到任何东西。此外,如果它有助于我主要为我的手机制作这个。 Nexus 6p Android 6.0.1

编辑:得到OnRequestPermissionsResultCallback工作但仍然有PermissionUtils给我一个错误。

我会评论我的错误/无法解决的地方

XML代码         

        <Button
            android:id="@+id/StartButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start|bottom"
            android:text="@string/Start"
            android:padding="10dp"
            android:layout_marginBottom="10dp"
            android:paddingLeft="10dp"
            android:onClick="start"/>
        <Button
            android:id="@+id/StopButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start|bottom"
            android:text="@string/Stop"
            android:padding="10dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="107dp"
            android:paddingLeft="10dp"
            android:onClick="stop"/>
        <Button
            android:id="@+id/CameraButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:text="@string/Camera"
            android:padding="10dp"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="107dp"
            android:paddingRight="10dp"
            android:onClick="openCamera"/>
        <Button
            android:id="@+id/ShareButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:text="@string/Share"
            android:padding="10dp"
            android:layout_marginBottom="10dp"
            android:paddingRight="10dp"/>

    </fragment>

这是我的活动     package com.example.name.app;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.FragmentActivity;

import android.view.View;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import android.support.v4.content.ContextCompat;
import com.google.android.gms.maps.GoogleMap.OnMyLocationButtonClickListener;


import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.content.Intent;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.widget.Button;




public class MapsActivity extends AppCompatActivity
        implements
        OnMyLocationButtonClickListener,
        OnMapReadyCallback,
        ActivityCompat.OnRequestPermissionsResultCallback { //ERROR on OnRequest...CallBack
    /**
     * Request code for location permission request.
     *
     * @see #onRequestPermissionsResult(int, String[], int[])
     */
    private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
    /**
     * Flag indicating whether a requested permission has been denied after returning in
     * {@link #onRequestPermissionsResult(int, String[], int[])}.
     */
    private boolean mPermissionDenied = false;

    Button startbutton;
    Button stopbutton;
    Button sharebutton;
    private GoogleMap mMap;
    static final int REQUEST_IMAGE_CAPTURE = 1;
    static final int REQUEST_TAKE_PHOTO = 1;

    String mCurrentPhotoPath;

    @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);

        startbutton= (Button) findViewById(R.id.StartButton);
        stopbutton = (Button) findViewById(R.id.StopButton);
        sharebutton= (Button) findViewById(R.id.ShareButton);

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Venice, Italy, and move the camera.
       /* LatLng venice = new LatLng(45.43, 12.33);
        mMap.addMarker(new MarkerOptions().position(venice).title("Marker in Venice"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(venice));*/

        mMap.setOnMyLocationButtonClickListener(this);
        enableMyLocation();
    }

    private void enableMyLocation() {
        //ERROR CANNOT RESOLVE METHOD checkSelfPermission
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            // Permission to access the location is missing.
            //ERROR CANNOT RESOLVE SYMBOL PermissionUtils
            PermissionUtils.requestPermission(this, LOCATION_PERMISSION_REQUEST_CODE,
                    Manifest.permission.ACCESS_FINE_LOCATION, true);
        } else if (mMap != null) {
            // Access to the location has been granted to the app.
            mMap.setMyLocationEnabled(true);
        }
    }

    @Override
    public boolean onMyLocationButtonClick() {
        Toast.makeText(this, "MyLocation button clicked", Toast.LENGTH_SHORT).show();
        // Return false so that we don't consume the event and the default behavior still occurs
        // (the camera animates to the user's current position).
        return false;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        if (requestCode != LOCATION_PERMISSION_REQUEST_CODE) {
            return;
        }
        //ERROR CANNOT RESOLVE SYMBOL PermissionUtils

        if (PermissionUtils.isPermissionGranted(permissions, grantResults,
                Manifest.permission.ACCESS_FINE_LOCATION)) {
            // Enable the my location layer if the permission has been granted.
            enableMyLocation();
        } else {
            // Display the missing permission error dialog when the fragments resume.
            mPermissionDenied = true;
        }
    }
    @Override
    protected void onResumeFragments() {
        super.onResumeFragments();
        if (mPermissionDenied) {
            // Permission was not granted, display error dialog.
            showMissingPermissionError();
            mPermissionDenied = false;
        }
    }
    //ERROR CANNOT RESOLVE SYMBOL PermissionUtils

    private void showMissingPermissionError() {
        PermissionUtils.PermissionDeniedDialog
                .newInstance(true).show(getSupportFragmentManager(), "dialog");
    }

    public void start(View view){
        startbutton.setText("Running");
        stopbutton.setText("Stop");
    }
    public void stop(View view){
        stopbutton.setText("Stopped");
        startbutton.setText("Start");
    }
    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        //Get location and store it
        File storageDir = new File (Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES)+"/MyMap"); //Saves it in Pictures/MyMapp  but can only be viewd from file explorer
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = "file:" + image.getAbsolutePath();
        return image;
    }
    public void openCamera(View view) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File

            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(photoFile));
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
                galleryAddPic();
            }
        }
    }
    private void galleryAddPic() {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File f = new File(mCurrentPhotoPath);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
    }

}

这是我的Android清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.name.app" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <!--
 The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but are recommended.
    -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.CAMERA"> </uses-permission>

    <uses-feature android:name="android.hardware.camera"
        android:required="true" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="@string/google_maps_key" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

    </manifest>

这是我更新的构建gradle     申请插件:&#39; com.android.application&#39;

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.name.app"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.2.0'
    compile 'com.google.android.gms:play-services:8.4.0'
    compile 'com.android.support:support-v13:23.2.0'
}

2 个答案:

答案 0 :(得分:3)

升级:

  • compileSdkVersion23
  • buildToolsVersion23.0.2
  • com.android.support:appcompat-v7com.android.support:support-v1323.2.0

您遗失的符号来自23.x.y版appcompat-v7;其他变化是保持主要版本同步。

另请注意,7.5.0play-services的旧版本;我建议将其升级到8.4.0

并且,请记住,给定targetSdkVersion 22时,所有运行时权限都是毫无意义的。

答案 1 :(得分:1)

我有类似的问题想知道为什么无法找到PermissionUtil类。可以在Github的googlemap / android-maps示例(https://github.com/googlemaps/android-samples/blob/master/ApiDemos/app/src/main/java/com/example/mapdemo/PermissionUtils.java)找到该类。我只是创建了一个java文件,复制了代码并替换了字符串资源。