我创建了一个简单的应用程序,其中包含一个登录页面,当用户单击登录按钮时,它将导航到另一个页面。每当我使用react-native run-android运行我的应用程序时,它都能正常工作。但是,只要我在更高版本的Android设备上运行.apk文件,应用程序就会崩溃(在android 5和6中运行正常)。
package.json
{
"name": "awsUpload",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"aws-sdk": "^2.395.0",
"link": "^0.1.5",
"react": "16.6.3",
"react-native": "^0.58.3",
"react-native-aws3": "0.0.8",
"react-native-device-info": "^0.26.2",
"react-native-file-upload": "^1.0.4",
"react-native-image-picker": "^0.28.0",
"react-native-material-dropdown": "^0.11.1",
"react-native-router-flux": "^4.0.6",
"uniqid": "^5.0.3"
},
"devDependencies": {
"babel-core": "7.0.0-bridge.0",
"babel-jest": "24.0.0",
"jest": "24.0.0",
"metro-react-native-babel-preset": "0.51.1",
"react-test-renderer": "16.6.3"
},
"jest": {
"preset": "react-native"
}
}
我正在使用以下代码来获取地理位置。据我所知,由于受到保护,在更高版本的Android设备中进行地理位置定位已经存在问题,并且堆栈溢出有解决方案。但是我不知道如何使用它。
我的代码是,
import React, {Component} from 'react';
import {Text, View} from 'react-native';
export default class App extends Component<Props> {
constructor(props) {
super(props);
this.state = {
currentLongitude: 'unknown',//Initial Longitude
currentLatitude: 'unknown',//Initial Latitude
}
}
componentDidMount = () => {
navigator.geolocation.getCurrentPosition(
//Will give you the current location
(position) => {
const currentLongitude = JSON.stringify(position.coords.longitude);
//getting the Longitude from the location json
const currentLatitude = JSON.stringify(position.coords.latitude);
//getting the Latitude from the location json
this.setState({ currentLongitude:currentLongitude });
//Setting state Longitude to re re-render the Longitude Text
this.setState({ currentLatitude:currentLatitude });
//Setting state Latitude to re re-render the Longitude Text
},
(error) => alert(error.message),
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
);
this.watchID = navigator.geolocation.watchPosition((position) => {
//Will give you the location on location change
console.log(position);
const currentLongitude = JSON.stringify(position.coords.longitude);
//getting the Longitude from the location json
const currentLatitude = JSON.stringify(position.coords.latitude);
//getting the Latitude from the location json
this.setState({ currentLongitude:currentLongitude });
//Setting state Longitude to re re-render the Longitude Text
this.setState({ currentLatitude:currentLatitude });
//Setting state Latitude to re re-render the Longitude Text
});
}
componentWillUnmount = () => {
navigator.geolocation.clearWatch(this.watchID);
}
render() {
return (
<View>
<Text>Longitude: {this.state.currentLongitude}</Text>
<Text>Latitude: {this.state.currentLatitude}</Text>
{this.state.error ? <Text>Error: {this.state.error}</Text> : null}
</View>
);
}
}
解决方案链接是
https://stackoverflow.com/questions/33865445/gps-location-provider-requires-access-fine-location-permission-for-android-6-0/33866959
答案 0 :(得分:1)
错误已完全清除,您需要获得访问位置的权限。
有两种类型的权限:
1-只可以通过向manifest.xml
添加权限来授予
2-个用户,他本人必须接受该权限。(这些通常显示在dialog
中,并要求用户授予应用访问权限...)。
位置许可是第二种类型。因此将该权限添加到Manifest.xml
不会执行任何操作。
您可以在https://facebook.github.io/react-native/docs/permissionsandroid
中查看权限列表请求用户许可的方式在以下链接中说明:
How do I request permission for Android Device Location in React Native at run-time?