Android:同步适配器未在某些设备上运行

时间:2017-01-30 10:37:09

标签: android synchronization android-contentprovider android-syncadapter

在我的android项目中,我有一个同步适配器,可以将数据从Android设备同步到服务器并且工作正常,但最近我注意到onPerfomSync没有调用我的moto g3安卓手机,我不知道知道是否有任何其他手机存在同样的问题,我已经在其他一些具有不同android api级别的三星手机上进行了测试,但没有发现任何问题。我已经找到成功的用户登录时配置了同步。这种行为的原因是什么。如果我做错了,请更正我。 以下是我的代码。

        private void finishLogin(Intent intent) {
    String accountName = intent.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
    String accountPassword = intent.getStringExtra(PARAM_USER_PASS);
    final Account account = new Account(accountName, intent.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE));

    if (getIntent().getBooleanExtra(ARG_IS_ADDING_ACCOUNT, false)) {
        String authtoken = intent.getStringExtra(AccountManager.KEY_AUTHTOKEN);
        String authtokenType = authTokenType;
        String refreshToken = intent.getStringExtra(KEY_REFRESH_TOKEN);
        Bundle userData = new Bundle();
        userData.putString(KEY_REFRESH_TOKEN, refreshToken);
        accountManager.addAccountExplicitly(account, accountPassword, userData);
        accountManager.setAuthToken(account, authtokenType, authtoken);
        accountManager.setUserData(account,KEY_REFRESH_TOKEN,refreshToken);

        ContentResolver.setMasterSyncAutomatically(true); 
        ContentResolver.setSyncAutomatically(account, "com.myexample.mysampleproject", true);
        configurePeriodicSync(account, SYNC_INTERVAL, SYNC_FLEXTIME);

        try {



        } catch (Exception e) {
            e.printStackTrace();
        }



    } else {
        System.out.println("condition else getIntent().getBooleanExtra(ARG_IS_ADDING_ACCOUNT, false)");
        accountManager.setPassword(account, accountPassword);
    }
    setAccountAuthenticatorResult(intent.getExtras());
    setResult(RESULT_OK, intent);
    finish();
}

public void configurePeriodicSync(Account account, int syncInterval, int flexTime) {
    Log.d(TAG, "PERIODC SYNC CONFIGURED");
    Bundle mySyncExtras = new Bundle();
    mySyncExtras.putBoolean("MY_SYNC", true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        System.out.println("if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)");
        SyncRequest request = new SyncRequest.Builder().
                syncPeriodic(syncInterval, flexTime).
                setSyncAdapter(account, "com.myexample.mysampleproject").setExtras(mySyncExtras).build();
        ContentResolver.requestSync(request);
    } else {
        System.out.println("else (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)");
        ContentResolver.addPeriodicSync(account, "com.myexample.mysampleproject", mySyncExtras, syncInterval);
    }

}

SyncService.java:

    public class SyncService extends Service {

    private static SyncAdapter syncAdapter = null;

    private static final Object syncAdapterLock = new Object();

    @Override
    public void onCreate() {
        synchronized (syncAdapterLock) {
            if (syncAdapter == null) {
                syncAdapter = new SyncAdapter(getApplicationContext(), true);
            }
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return syncAdapter.getSyncAdapterBinder();
    }
}

AndroidManifest.xml:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myexample.mysampleproject" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>

<application
    android:largeHeap="true"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".view.MainActivity"
        android:configChanges="orientation|screenSize"
        android:theme="@style/AppTheme"
        android:label="@string/app_name" >
    </activity>
    <activity android:name=".view.LoginActivity"
              android:configChanges="orientation|screenSize"
              android:theme="@style/AppTheme"
              android:label="@string/login_label"/>

    ........
    ........

    <provider
            android:name=".db.MyContentProvider"
            android:authorities="com.myexample.mysampleproject"
            android:exported="false"
            android:syncable="true" />
    <service
            android:name=".sync.SyncService"
            android:exported="true">
        <intent-filter>
            <action android:name="android.content.SyncAdapter" />
        </intent-filter>

        <meta-data
                android:name="android.content.SyncAdapter"
                android:resource="@xml/sync_adapter" />
    </service>
    </application>

</manifest>

sync_adapter.xml:

    <?xml version="1.0" encoding="utf-8"?>
<sync-adapter
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="com.myexample.mysampleproject"
    android:accountType="com.myexample.mysampleproject"
    android:userVisible="true"
    android:supportsUploading="true"
    android:allowParallelSyncs="true"
    android:isAlwaysSyncable="true" />

SyncAdapter.java:

    public class SyncAdapter extends AbstractThreadedSyncAdapter {

    ContentResolver contentResolver;

    Context context;
    String authToken;

    public SyncAdapter(Context context, boolean autoInitialize) {
        super(context, autoInitialize);
        this.context = context;
        contentResolver = context.getContentResolver();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public SyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) {
        super(context, autoInitialize, allowParallelSyncs);
        contentResolver = context.getContentResolver();
    }

    @Override
    public void onPerformSync(Account account, final Bundle extras, String authority,
                            ContentProviderClient provider, SyncResult syncResult) {
        try {
            System.out.println("start sync ");
            AccountManager manager = AccountManager.get(context);
            authToken = manager.peekAuthToken(account, AccountGeneral.AUTHTOKEN_TYPE_FULL_ACCESS);
            try {
                if(authToken != null) {
                    if (isOnline()) {
                        // perform sync....

                    } else {
                        Log.d("ATTEMPT SYNC ERROR", "NO NETWORK CONNECTION");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

0 个答案:

没有答案