forked from vikeri/react-native-background-job
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.android.js
181 lines (173 loc) · 5.48 KB
/
index.android.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from "react";
import {
AppRegistry,
TouchableHighlight,
StyleSheet,
Text,
View
} from "react-native";
import BackgroundJob from "react-native-background-job";
const regularJobKey = "regularJobKey";
const exactJobKey = "exactJobKey";
const foregroundJobKey = "foregroundJobKey";
/**
* In Android SDK versions greater than 23, Doze is being used by apps by default,
* in order to optimize battery by temporarily turning off background tasks when
* the phone is left undisturbed for some hours.
*
* But, some apps may require background tasks to keep running, ignoring doze and
* not optimizing battery (this means battery needs to be traded off for performance
* as per required).
*
* Such jobs can be scheduled as everRunningJob is scheduled below.
* It may be scheduled as normal jobs are, but they wont behave as expected. Doze
* feature will disable the running background jobs if the phone remains undisturbed
* for some time.
*
* So everRunningJob scheduled below can be scheduled by checking if is ignoring
* optimizations.If true, schedule the job in the callback, else we notify the
* user to manually remove the app from the battery optimization list.
*/
const everRunningJobKey = "everRunningJobKey";
// This has to run outside of the component definition since the component is never
// instantiated when running in headless mode
BackgroundJob.register({
jobKey: regularJobKey,
job: () => console.log(`Background Job fired!. Key = ${regularJobKey}`)
});
BackgroundJob.register({
jobKey: exactJobKey,
job: () => {
console.log(`${new Date()}Exact Job fired!. Key = ${exactJobKey}`);
}
});
BackgroundJob.register({
jobKey: foregroundJobKey,
job: () => console.log(`Exact Job fired!. Key = ${foregroundJobKey}`)
});
BackgroundJob.register({
jobKey: everRunningJobKey,
job: () => console.log(`Ever Running Job fired! Key=${everRunningJobKey}`)
});
export default class backtest extends Component {
constructor(props) {
super(props);
this.state = { jobs: [] };
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>Testing BackgroundJob</Text>
<Text style={styles.instructions}>
Try connecting the device to the developer console, schedule an event
and then quit the app.
</Text>
<Text>
Scheduled jobs:
{this.state.jobs.map(({ jobKey }) => jobKey)}
</Text>
<TouchableHighlight
style={styles.button}
onPress={() => {
BackgroundJob.schedule({
jobKey: regularJobKey,
period: 15000
});
}}
>
<Text>Schedule regular job</Text>
</TouchableHighlight>
<TouchableHighlight
style={styles.button}
onPress={() => {
BackgroundJob.schedule({
jobKey: exactJobKey,
period: 1000,
exact: true
});
}}
>
<Text>Schedule exact job</Text>
</TouchableHighlight>
<TouchableHighlight
style={styles.button}
onPress={() => {
BackgroundJob.schedule({
jobKey: foregroundJobKey,
period: 1000,
exact: true,
allowExecutionInForeground: true
});
}}
>
<Text>Schedule exact foreground job</Text>
</TouchableHighlight>
<TouchableHighlight
style={styles.button}
onPress={() => {
BackgroundJob.isAppIgnoringBatteryOptimization(
(error, ignoringOptimization) => {
if (ignoringOptimization === true) {
BackgroundJob.schedule({
jobKey: everRunningJobKey,
period: 1000,
exact: true,
allowWhileIdle: true
});
} else {
console.log(
"To ensure app functions properly,please manually remove app from battery optimization menu."
);
//Dispay a toast or alert to user indicating that the app needs to be removed from battery optimization list, for the job to get fired regularly
}
}
);
}}
>
<Text>Schedule ever running job</Text>
</TouchableHighlight>
<TouchableHighlight
style={styles.button}
onPress={() => {
BackgroundJob.cancel({ jobKey: regularJobKey });
}}
>
<Text>Cancel regular job</Text>
</TouchableHighlight>
<TouchableHighlight
style={styles.button}
onPress={() => {
BackgroundJob.cancelAll();
}}
>
<Text>CancelAll</Text>
</TouchableHighlight>
</View>
);
}
componentDidMount() {
BackgroundJob.schedule({
jobKey: exactJobKey,
period: 1000,
timeout: 10000,
exact: true
});
}
}
const styles = StyleSheet.create({
button: { padding: 20, backgroundColor: "#ccc", marginBottom: 10 },
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#F5FCFF"
},
welcome: { fontSize: 20, textAlign: "center", margin: 10 },
instructions: { textAlign: "center", color: "#333333", marginBottom: 5 }
});
AppRegistry.registerComponent("backtest", () => backtest);