Есть ли альтернативный способ или способы запуска приложения Expo в фоновом режиме с помощью Android? Я искал пару дней в поисках ответа. Есть ответы, но для новичков это сложно. Я создаю приложение, которое отслеживает местоположение пользователя каждые 5 секунд и отправляет его на сервер с помощью узла. Он работает нормально, моя единственная проблема заключается в том, что когда в фоновом режиме мое приложение перестает отправлять данные на сервер.
App.js
import React, { Component } from 'react';
import {
Text,
View,
StyleSheet,
Dimensions
} from 'react-native';
import { BackgroundFetch, TaskManager } from 'expo';
import MapView from 'react-native-maps';
import {Marker} from 'react-native-maps'
export default class App extends Component {
state = {
mapRegion: null,
hasLocationPermissions: false,
locationResult: null,
marker: {
latitude: 0,
longitude: 0
},
latitude: 0,
longitude: 0,
location: null,
errorMessage: null
};
componentDidMount() {
//var handle = Interaction
this.getBackgroundStat()
this.watchCurLocation();
}
getBackgroundStat = async () =>{
const test = await BackgroundFetch.getStatusAsync({});
console.log(test)
}
watchCurLocation = () =>
setTimeout(() => {
this.watchCurLocation();
}, 5000);
}
getLocationAsync = async () => {
const { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== 'granted') {
this.setState({
errorMessage: 'Permission to access location was denied',
});
return;
}
const location = await Location.getCurrentPositionAsync({});
console.log(location)
this.setState({ location });
};
getCurrentLocation = () =>
navigator.geolocation.getCurrentPosition(
(position) => {
let currentUserPosition = position.coords;
//alert(JSON.stringify(currentUserPosition));
},
(error) => {
console.log(error);
},
{
enableHighAccuracy: true,
timeout: 2000,
maximumAge: 0,
distanceFilter: 1
}
);
_handleMapRegionChange = mapRegion => {
//console.log(mapRegion);
this.setState({ mapRegion });
};
_watchLocationAsync = async () =>{
let watchlocation = Location.watchPositionAsync({enableHighAccuracy:true,timeInterval:4000, distanceInterval:0}, (pos)=>{
console.log(pos)
return pos
});
return watchlocation
}
_getLocationAsync = async () => {
let { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== 'granted') {
this.setState({
locationResult: 'Permission to access location was denied',
});
} else {
this.setState({ hasLocationPermissions: true });
}
let location = await Location.getCurrentPositionAsync({});
let marker = Object.assign({}, this.state.marker)
marker.latitude = location.coords.latitude
marker.longitude = location.coords.longitude
this.setState({ locationResult: JSON.stringify(location) });
this.setState({marker})
// Center the map on the location we just fetched.
this.setState({ mapRegion: { latitude: location.coords.latitude, longitude: location.coords.longitude, latitudeDelta: 0.0922, longitudeDelta: 0.0421 } });
};
componentWillUnmount(){
navigator.geolocation.clearWatch(this.watchId);
}
render() {
return (
Pan, zoom, and tap on the map!
{
this.state.locationResult === null ?
Finding your current location... :
this.state.hasLocationPermissions === false ?
Location permissions are not granted. :
this.state.mapRegion === null ?
Map region doesn't exist. :
}
Location: {this.state.locationResult}
{"\n\n"}
MyOwnLocation: {this.state.latitude} {"\n\n"}
MyOwnLocation2: {this.state.longitude}
);
}
}
Подробнее здесь: https://stackoverflow.com/questions/541 ... background
Expo React-native с использованием Android: как запустить приложение в фоновом режиме? ⇐ Android
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Отправляйте SMS в фоновом режиме с помощью Expo React Native на Android
Anonymous » » в форуме Android - 0 Ответы
- 32 Просмотры
-
Последнее сообщение Anonymous
-