-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
102 lines (91 loc) · 3.31 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { StatusBar } from "expo-status-bar";
import { Switch, Text, View, StyleSheet } from "react-native";
import React from "react";
import BackgroundGeolocation, {
Location,
Subscription,
} from "react-native-background-geolocation";
export default function App() {
const [enabled, setEnabled] = React.useState(false);
const [location, setLocation] = React.useState("");
React.useEffect(() => {
/// 1. Subscribe to events.
const onLocation = BackgroundGeolocation.onLocation((location) => {
console.log("[onLocation]", location);
setLocation(JSON.stringify(location, null, 2));
});
const onMotionChange = BackgroundGeolocation.onMotionChange((event) => {
console.log("[onMotionChange]", event);
});
const onActivityChange = BackgroundGeolocation.onActivityChange((event) => {
console.log("[onActivityChange]", event);
});
const onProviderChange = BackgroundGeolocation.onProviderChange((event) => {
console.log("[onProviderChange]", event);
});
/// 2. ready the plugin.
BackgroundGeolocation.ready({
// Geolocation Config
desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
distanceFilter: 10,
// Activity Recognition
stopTimeout: 5,
// Application config
debug: true, // <-- enable this hear sounds for background-geolocation life-cycle.
logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
stopOnTerminate: false, // <-- Allow the background-service to continue tracking when user closes the app.
startOnBoot: true, // <-- Auto start tracking when device is powered-up.
// HTTP / SQLite config
// url: "http://yourserver.com/locations",
// batchSync: false, // <-- [Default: false] Set true to sync locations to server in a single HTTP request.
// autoSync: true, // <-- [Default: true] Set true to sync each location to server as it arrives.
// headers: {
// // <-- Optional HTTP headers
// "X-FOO": "bar",
// },
// params: {
// // <-- Optional HTTP params
// auth_token: "maybe_your_server_authenticates_via_token_YES?",
// },
}).then((state) => {
setEnabled(state.enabled);
console.log(
"- BackgroundGeolocation is configured and ready: ",
state.enabled
);
});
return () => {
// Remove BackgroundGeolocation event-subscribers when the View is removed or refreshed
// during development live-reload. Without this, event-listeners will accumulate with
// each refresh during live-reload.
onLocation.remove();
onMotionChange.remove();
onActivityChange.remove();
onProviderChange.remove();
};
}, []);
/// 3. start / stop BackgroundGeolocation
React.useEffect(() => {
if (enabled) {
BackgroundGeolocation.start();
} else {
BackgroundGeolocation.stop();
setLocation("");
}
}, [enabled]);
return (
<View style={styles.container}>
<Text>Click to enable BackgroundGeolocation</Text>
<Switch value={enabled} onValueChange={setEnabled} />
<Text style={{ fontFamily: "monospace", fontSize: 12 }}>{location}</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});