当设备上没有互联网连接时,有没有办法调用onPerformSync()

时间:2016-05-27 06:55:22

标签: android android-syncadapter

它似乎是一个奇怪的问题,但我仍然想在设备上没有互联网连接时调用 onPerformSync()定期同步。我在这里进行了一些讨论,发现当设备处于离线模式时,不会调用 onPerformSync 。但是,在没有互联网连接的情况下有任何方式或解决方法来调用onPerformSync

1 个答案:

答案 0 :(得分:0)

从以下位置复制我的答案:syncadapter without internet connection

如果不修改AOSP或按照rAm的回答更改设计,就无法实现这一目标。

我的答案是那些愿意修改AOSP的人:

Google强制执行以下操作:

作业是由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);

但是NETWORK_TYPE_ANY映射到JobStatus.java中的CONSTRAINT_CONNECTIVITY

switch (job.getNetworkType()) {
    case JobInfo.NETWORK_TYPE_NONE:
        // No constraint.
        break;
    case JobInfo.NETWORK_TYPE_ANY:
        requiredConstraints |= CONSTRAINT_CONNECTIVITY;
        break;

有两种解决方案:

1。 首先删除互联网连接检查,仅检查常规连接

  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。 其次,您需要有条件地向SyncManager.java添加替代条件 我添加了充电条件,因为正在使用的自定义设备始终处于充电状态-可以将其更改为其他任何条件-但还需要一个附加条件。

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);