Its seems like a weird question but still I want to call onPerformSync() or periodic sync when there is no internet connection on device. I have gone through some discussions here and found that onPerformSync will not be called when device is in offline mode. But, is there any way or work around to call onPerformSync when there is no internet connection.
Asked
Active
Viewed 264 times
0
-
How do you expect to get data from the internet if you are not connected to the internet? – K Neeraj Lal May 27 '16 at 07:03
-
No, I don't have to sync data with server when there is no internet, actually I am doing some other stuffs on periodic basic and calling them from onPerformSync......is there any way? – Abhimanyu Raj May 27 '16 at 07:08
-
Implement it in some other way using Threads or AlarmManager. – K Neeraj Lal May 27 '16 at 07:09
-
no, you cannot do that, use `AlarmManager` to make periodic actions – pskink May 27 '16 at 07:10
-
But in some custom android phones like, MIUI, when you clear the app from recent the app no longer exist in memory so your alarm manager or gcm kind of stuff not going to work there. – Abhimanyu Raj May 27 '16 at 07:12
-
Any one have any idea what whatsapp is doing to keep the app always in memory? – Abhimanyu Raj May 27 '16 at 07:17
-
did you use `AlarmManager`? did you try it? – pskink May 27 '16 at 07:22
-
Yes already tried these all before, to make it working I have to add the app in my device auto start app list which end user not going to do. Still if you have some code samples please share with me..... – Abhimanyu Raj May 27 '16 at 07:31
-
`AlarmManager` just works, post the code what you tried – pskink May 27 '16 at 07:50
1 Answers
0
Copying my answer from : syncadapter without internet connection
There is no way to achieve this without modifying AOSP or changing design as rAm answered.
My answer is for those open to modifying AOSP :
Google enforces this as follows :
Job is scheduled with Network constraint by SyncManager.java
final int networkType = syncOperation.isNotAllowedOnMetered() ?
JobInfo.NETWORK_TYPE_UNMETERED : JobInfo.NETWORK_TYPE_ANY;
JobInfo.Builder b = new JobInfo.Builder(syncOperation.jobId,
new ComponentName(mContext, SyncJobService.class))
.setExtras(syncOperation.toJobInfoExtras())
.setRequiredNetworkType(networkType)
.setPersisted(true)
.setPriority(priority);
But NETWORK_TYPE_ANY is mapped to CONSTRAINT_CONNECTIVITY in JobStatus.java
switch (job.getNetworkType()) {
case JobInfo.NETWORK_TYPE_NONE:
// No constraint.
break;
case JobInfo.NETWORK_TYPE_ANY:
requiredConstraints |= CONSTRAINT_CONNECTIVITY;
break;
There are two solutions :
1. First removes check for internet connectivity and just checks for regular connectivity
diff --git a/services/core/java/com/android/server/job/controllers/ConnectivityController.java b/services/core/java/com/android/server/job/controllers/ConnectivityController.java
index 4d5a920..6680c4e 100644
--- a/services/core/java/com/android/server/job/controllers/ConnectivityController.java
+++ b/services/core/java/com/android/server/job/controllers/ConnectivityController.java
@@ -118,7 +118,8 @@ public final class ConnectivityController extends StateController implements
&& !info.isRoaming();
boolean changed = false;
- changed |= jobStatus.setConnectivityConstraintSatisfied(connectionUsable);
+ //change to AOSP : use connected instead of connectionUsable - since connection maybe considered usable within enterprise without NET_CAPABILITY_VALIDATED
+ changed |= jobStatus.setConnectivityConstraintSatisfied(connected);
changed |= jobStatus.setMeteredConstraintSatisfied(metered);
changed |= jobStatus.setUnmeteredConstraintSatisfied(unmetered);
changed |= jobStatus.setNotRoamingConstraintSatisfied(notRoaming);
2. Second is you need to add an alternative condition conditionally to SyncManager.java I added charging condition since custom device I work on is always charging - this can be changed to any other condition - but one additional condition is necessary.
diff --git a/services/core/java/com/android/server/content/SyncManager.java b/services/core/java/com/android/server/content/SyncManager.java
index 205e828..81f9a8c 100644
--- a/services/core/java/com/android/server/content/SyncManager.java
+++ b/services/core/java/com/android/server/content/SyncManager.java
@@ -1539,6 +1539,13 @@ public class SyncManager {
.setRequiredNetworkType(networkType)
.setPersisted(true)
.setPriority(priority);
+
+ if (syncOperation.owningPackage.contains("com.example.yourcompany")) {
+ //Change to AOSP : Custom accounts need to sync wihout access to public internet
+ Slog.i(TAG, "set JobInfo.NETWORK_TYPE_NONE for "+syncOperation.target.toString());
+ b.setRequiredNetworkType(JobInfo.NETWORK_TYPE_NONE);
+ b.setRequiresCharging(true);
+ }
if (syncOperation.isPeriodic) {
b.setPeriodic(syncOperation.periodMillis, syncOperation.flexMillis);

RocketRandom
- 1,102
- 7
- 20