2016-03-02 116 views
0

我是新来的android和我想建立一个应用程序来显示我的当前位置,我正在跟随从apidemos链接here设置。这是一项任务的一部分,所以我不想透露我的最终目标是什么。现在我想能够在我的应用上使用gps位置。我将发布我的活动代码,xml布局代码,清单代码和gradle代码。如果还有其他东西需要发布,请告诉我。我尝试过搜索,但找不到任何东西。如果它有帮助,我主要是为我的手机做这个。 Nexus 6p Android 6.0.1Android无法解析符号OnRequestPermissionsResultCallback

编辑:得到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> 

这里是我的活动 包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构建应用 插件: 'com.android.application'

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' 
} 
+0

你可以将compilesdkversion和targetsdk版本更改为23吗? –

+0

@RavjitSingh我到底该怎么做。我会右键点击并打开模块设置并改变它吗? – SamuelJacksonJr

+0

不,在gradle文件中,你发布的最后一个文件在你的问题 –

回答

3

升级:

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

你丢失的符号是从appcompat-v7一个23.x.y版;其他更改是保持主版本同步。

此外,请注意7.5.0是一个相当旧的版本play-services;我建议将其升级到8.4.0

而且,请记住,由于targetSdkVersion 22,所有运行时权限的内容都是毫无意义的。

+0

嗨,我按照你的指示改变了前两个项目符号。我不完全确定第三者的含义。对不起,我对此很新。我的targetsdkversion现在是23。现在我上课时,我会在大约一个小时内更新我的播放服务。我很快就会回来。感谢您的帮助 – SamuelJacksonJr

+0

@SamuelJacksonJr:“我不完全确定你的意思第三个” - 它指的是你的'build.gradle'文件中的两个'依赖关系'。 – CommonsWare

+0

嘿,我附加了我更新的build.gradle文件,其中包含所有建议的更改。 onRequestPermissionResultsCallback不再给我一个错误。 PermissionUtils的三个实例确实给了我一个错误。有什么建议么? – SamuelJacksonJr