25
25
26
26
import java .util .ArrayList ;
27
27
import java .util .List ;
28
+ import java .util .concurrent .Executor ;
28
29
import java .util .concurrent .ExecutorService ;
29
30
import java .util .concurrent .Executors ;
30
31
@@ -44,7 +45,8 @@ static boolean run(Context context, Intent intent) {
44
45
// Execute in the next second.
45
46
Bundle extra = new Bundle (1 );
46
47
extra .putParcelable (INTENT_KEY , intent );
47
- int did = scheduler .schedule (new JobInfo .Builder (JOB_SERVICE_ID , new ComponentName (context , PushServiceApi26 .class ))
48
+ ComponentName component = new ComponentName (context , PushServiceApi26 .class );
49
+ int did = scheduler .schedule (new JobInfo .Builder (JOB_SERVICE_ID , component )
48
50
.setMinimumLatency (1L )
49
51
.setOverrideDeadline (1000L )
50
52
.setRequiresCharging (false )
@@ -58,19 +60,13 @@ static boolean run(Context context, Intent intent) {
58
60
// We delegate the intent to a PushHandler running in a streamlined executor.
59
61
private ExecutorService executor ;
60
62
private PushHandler handler ;
63
+ private int jobsCount ;
61
64
62
65
// Our manifest file is OK.
63
66
static boolean isSupported () {
64
67
return true ;
65
68
}
66
69
67
- @ Override
68
- public void onCreate () {
69
- super .onCreate ();
70
- executor = Executors .newSingleThreadExecutor ();
71
- handler = PushServiceUtils .createPushHandler ();
72
- }
73
-
74
70
@ Override
75
71
public boolean onStartJob (final JobParameters jobParameters ) {
76
72
if (ParsePlugins .Android .get () == null ) {
@@ -85,13 +81,18 @@ public boolean onStartJob(final JobParameters jobParameters) {
85
81
86
82
final Bundle params = jobParameters .getTransientExtras ();
87
83
final Intent intent = params .getParcelable (INTENT_KEY );
88
- executor .execute (new Runnable () {
84
+ jobsCount ++;
85
+ getExecutor ().execute (new Runnable () {
89
86
@ Override
90
87
public void run () {
91
88
try {
92
- handler .handlePush (intent );
89
+ getHandler () .handlePush (intent );
93
90
} finally {
94
91
jobFinished (jobParameters , false );
92
+ jobsCount --;
93
+ if (jobsCount == 0 ) {
94
+ tearDown ();
95
+ }
95
96
}
96
97
}
97
98
});
@@ -104,13 +105,19 @@ public boolean onStopJob(JobParameters jobParameters) {
104
105
return true ;
105
106
}
106
107
107
- @ Override
108
- public void onDestroy () {
109
- if (executor != null ) {
110
- executor .shutdown ();
111
- executor = null ;
112
- handler = null ;
113
- }
114
- super .onDestroy ();
108
+ private Executor getExecutor () {
109
+ if (executor == null ) executor = Executors .newSingleThreadExecutor ();
110
+ return executor ;
111
+ }
112
+
113
+ private PushHandler getHandler () {
114
+ if (handler == null ) handler = PushServiceUtils .createPushHandler ();
115
+ return handler ;
116
+ }
117
+
118
+ private void tearDown () {
119
+ if (executor != null ) executor .shutdown ();
120
+ executor = null ;
121
+ handler = null ;
115
122
}
116
123
}
0 commit comments