问题描述
我是android新手,我正在尝试构建一个应用程序来显示我的当前位置,并且我正在关注来自google链接的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/>
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$ b $ android:layout_marginLeft =107dp
android:paddingLeft =10dp
android:onClick =stop/>
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/>
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 = 10dp/>
< / fragment>
以下是我的活动
package com.example.name.app;
import android.Manifest;
导入android.app.Activity;
导入android.content.pm.PackageManager;
导入android.net.Uri;
导入android.os.Bundle;
导入android.os.Environment;
导入android.provider.MediaStore;
导入android.support.v4.app.FragmentActivity;
导入android.view.View;
导入com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
导入android.support.v4.content.ContextCompat;
import com.google.android.gms.maps.GoogleMap.OnMyLocationButtonClickListener;
导入android.support.annotation.NonNull;
导入android.support.v4.app.ActivityCompat;
导入android.support.v4.content.ContextCompat;
导入android.support.v7.app.AppCompatActivity;
导入android.widget.Toast;
import com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.MarkerOptions;
导入android.content.Intent;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
导入android.widget.Button;
$ b公共类MapsActivity扩展AppCompatActivity
实现
OnMyLocationButtonClickListener,
OnMapReadyCallback,
ActivityCompat.OnRequestPermissionsResultCallback { //错误OnRequest ... CallBack
/ **
*位置权限请求的请求代码。
*
* @see #onRequestPermissionsResult(int,String [],int [])
* /
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
/ **
*表示在返回
* {@link #onRequestPermissionsResult(int,String [],int [])}后请求的权限是否被拒绝的标志。
* /
private boolean mPermissionDenied = false;
Button startbutton;
按钮停止按钮;
按钮分享按钮;
私有GoogleMap mMap;
static final int REQUEST_IMAGE_CAPTURE = 1;
static final int REQUEST_TAKE_PHOTO = 1;
字符串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;
//在意大利威尼斯添加一个标记,并移动相机。
/ * LatLng venice = new LatLng(45.43,12.33);
mMap.addMarker(new MarkerOptions()。position(venice).title(威尼斯标记));
mMap.moveCamera(CameraUpdateFactory.newLatLng(venice)); * /
mMap.setOnMyLocationButtonClickListener(this);
enableMyLocation();
$ b $ private void enableMyLocation(){
//错误无法解决方法checkSelfPermission
if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED){
//访问位置的权限丢失。
//错误无法解析符号PermissionUtils
PermissionUtils.requestPermission(this,LOCATION_PERMISSION_REQUEST_CODE,
Manifest.permission.ACCESS_FINE_LOCATION,true);
} else if(mMap!= null){
//访问位置已被授予应用程序。
mMap.setMyLocationEnabled(true);
@Override
public boolean onMyLocationButtonClick(){
Toast.makeText(this,MyLocation button clicked,Toast.LENGTH_SHORT) 。显示();
//返回false,以便我们不消耗事件并且默认行为仍然发生
//(相机动画到用户的当前位置)。
返回false;
}
@Override
public void onRequestPermissionsResult(int requestCode,@NonNull String [] permissions,
@NonNull int [] grantResults){
if (requestCode!= LOCATION_PERMISSION_REQUEST_CODE){
return;
//错误无法解析符号PermissionUtils
if(PermissionUtils.isPermissionGranted(permissions,grantResults,$ b $ Manifest.permission.ACCESS_FINE_LOCATION)){
//如果已授予权限,则启用我的位置图层。
enableMyLocation();
} else {
//片段恢复时显示丢失的权限错误对话框。
mPermissionDenied = true;
$ b $ @覆盖
保护无效onResumeFragments(){
super.onResumeFragments();
if(mPermissionDenied){
//权限未被授予,显示错误对话框。
showMissingPermissionError();
mPermissionDenied = false;
//错误无法解析符号PermissionUtils
$ b $ 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文件createImageFile()抛出IOException {
//创建一个图像文件名
String timeStamp = new SimpleDateFormat(yyyyMMdd_HHmmss)。format(new Date()) ;
String imageFileName =JPEG_+ timeStamp +_;
//获取位置并存储
文件storageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES)+/ MyMap); //保存在Pictures / MyMapp中,但只能从文件浏览器中查看
File image = File.createTempFile(
imageFileName,/ *前缀* /
.jpg,/ *后缀* /
storageDir / *目录* /
);
//保存文件:用于ACTION_VIEW意图的路径
mCurrentPhotoPath =file:+ image.getAbsolutePath();
返回图片;
}
public void openCamera(View view){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//确保有相机活动来处理意图
if(takePictureIntent.resolveActivity(getPackageManager())!= null){
//创建照片应该到达的文件
档案photoFile = null;
尝试{
photoFile = createImageFile();
} catch(IOException ex){
//创建文件时发生错误
}
//仅在文件被成功创建时继续
if(photoFile!= null){
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent,REQUEST_TAKE_PHOTO);
galleryAddPic(); (Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
}
文件f =新文件(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
}
这是我的Android清单 p>
<?xml version =1.0encoding =utf-8?>
< manifest xmlns:android =http://schemas.android.com/apk/res/android
package =com.example.name.app>
<使用权限android:name =android.permission.INTERNET/>
< uses-permission android:name =android.permission.ACCESS_NETWORK_STATE/>
< uses-permission android:name =android.permission.WRITE_EXTERNAL_STORAGE/>
<使用权限android:name =com.google.android.providers.gsf.permission.READ_GSERVICES/>
使用
Google Maps Android API v2时不需要ACCESS_COARSE / FINE_LOCATION权限,但建议您使用。
- >
< uses-permission android:name =android.permission.ACCESS_COARSE_LOCATION/>
< uses-permission android:name =android.permission.ACCESS_FINE_LOCATION/>
<使用权限android:name =android.permission.CAMERA> < /使用的许可>
< uses-feature android:name =android.hardware.camera
android:required =true/>
< uses-permission android:name =android.permission.WRITE_EXTERNAL_STORAGE/>
<使用权限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>
< category android:name =android.intent.category.LAUNCHER/>
< / intent-filter>
< / activity>
< / application>
< / manifest>
这里是我更新后的build gradle
apply plugin:'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion23.0.2
defaultConfig {
applicationIdcom.example.name.app
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName1.0
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
编译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'
}
-
compileSdkVersion
至23
-
buildToolsVersion
至23.0.2
com.android.support:appcompat-v7 和com.android.support: support-v13
至23.2.0
您缺少的符号来自 appcompat-v7
的23.xy版本;其他变化是保持主要版本同步。
另外,请注意 7.5.0
是一个宁可旧版的 play-services
;我建议将它升级到 8.4.0
。
而且,请记住,您的所有运行时权限是毫无意义的,给定 targetSdkVersion 22
。
I am new to android and I am trying to build an app to show my current location and I am following the setup in apidemos from google linked here. This is part of an assignment so I do not want to disclose what my ultimate goal is. For now I want to be able to have gps location working on my app. I will post my activity code, xml layout code, manifest code and gradle code. If there is anything else I need to post please let me know. I have tried searching but I couldn't find anything. Also if it helps I am mainly making this for my phone. Nexus 6p Android 6.0.1
EDIT:Got OnRequestPermissionsResultCallback Working but still have PermissionUtils giving me an error.
I will comment where my errors/cannot resolves are occurring
XML Code
<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>
Here is my activity 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);
}
}
Here is my android manifest
<?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>
And here is my updated build gradle apply plugin: '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'
}
Upgrade:
compileSdkVersion
to23
buildToolsVersion
to23.0.2
com.android.support:appcompat-v7
andcom.android.support:support-v13
to23.2.0
Your missing symbol is from a 23.x.y edition of appcompat-v7
; the other changes are to keep the major version in sync.
Also, note that 7.5.0
is a rather old edition of play-services
; I recommend upgrading that to 8.4.0
.
And, bear in mind that all your runtime permission stuff is pointless, given targetSdkVersion 22
.
这篇关于Android无法解析符号OnRequestPermissionsResultCallback的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!