我创建了一个具有登录页面的简单应用程序,当用户单击登录按钮时,它将导航到另一个页面。每当我使用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
错误完全明显,您需要获得访问位置的许可。
有两种类型的权限:
1-仅通过向manifest.xml
2-用户,他本人必须接受该许可的
。(这些通常在dialog
中显示,并要求用户使应用程序访问...)。
位置权限为第二类。因此,添加对Manifest.xml
的许可将不会做任何事情。
您可以在https://facebook.github.io/react-native/docs/permissionsandroid
中查看权限列表在下面的链接中解释了询问用户权限的方法:
如何在运行时请求Android设备位置的Android设备位置?