Android addPeriodicSync不工作
使用Android addPeriodicSync不工作,android,android-syncadapter,Android,Android Syncadapter,使用SyncAdapter时出现问题。我在指南中创建了它,但当我尝试使用addPeriodicSync时,没有任何效果 我阅读了在堆栈溢出上找到的所有答案,但仍然不知道问题出在哪里。请寻求帮助 代码: Manifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ig
SyncAdapter
时出现问题。我在指南中创建了它,但当我尝试使用addPeriodicSync
时,没有任何效果
我阅读了在堆栈溢出上找到的所有答案,但仍然不知道问题出在哪里。请寻求帮助
代码:
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.igordotsenko.dotsenkorssreader" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_SYNC_STATS" />
<application
android:name="com.activeandroid.app.Application"
android:allowBackup="true"
android:icon="@mipmap/ic_rss"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ItemListActivity" >
</activity>
<activity android:name=".ItemContentActivity" >
</activity>
<service android:name=".RefreshService" />
<provider
android:name=".ReaderContentProvider"
android:authorities="com.igordotsenko.dotsenkorssreader"
android:enabled="true"
android:exported="true" >
</provider>
<service
android:name=".syncadapter.SyncService"
android:exported="true"
>
<!-- android:process=":sync" -->
<intent-filter>
<action android:name="android.content.SyncAdapter" />
</intent-filter>
<meta-data
android:name="android.content.SyncAdapter"
android:resource="@xml/syncadapter" />
</service>
<service
android:name=".syncadapter.AuthenticatorService"
android:exported="true"
>
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator"/>
</intent-filter>
<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator" />
</service>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
android:contentAuthority="com.igordotsenko.dotsenkorssreader"
android:accountType="dummy.com"
android:userVisible="false"
android:supportsUploading="false"
android:allowParallelSyncs="false"
android:isAlwaysSyncable="true"
/>
<?xml version="1.0" encoding="utf-8"?>
<account-authenticator
xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="dummy.com"
/>
同步服务
package com.igordotsenko.dotsenkorssreader.syncadapter;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import java.util.Objects;
public class SyncService extends Service {
private static final Object syncAdapterLock = new Object();
private static ReaderSyncAdapter syncAdapter;
public SyncService() {
synchronized (syncAdapterLock) {
Log.i(ReaderSyncAdapter.SA_LOG, "SyncService started");
if ( syncAdapter == null )
syncAdapter = new ReaderSyncAdapter(getApplicationContext(), true);
Log.i(ReaderSyncAdapter.SA_LOG, "SyncService finished");
}
}
@Override
public IBinder onBind(Intent intent) {
Log.i(ReaderSyncAdapter.SA_LOG, "SyncService onBind");
return syncAdapter.getSyncAdapterBinder();
}
}
package com.igordotsenko.dotsenkorssreader.syncadapter;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class AuthenticatorService extends Service {
private ReaderAccountAuthenticator readerAccountAuthenticator;
public AuthenticatorService() {
readerAccountAuthenticator = new ReaderAccountAuthenticator(this);
}
@Override
public IBinder onBind(Intent intent) {
return readerAccountAuthenticator.getIBinder();
}
}
syncadapter.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.igordotsenko.dotsenkorssreader" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_SYNC_STATS" />
<application
android:name="com.activeandroid.app.Application"
android:allowBackup="true"
android:icon="@mipmap/ic_rss"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ItemListActivity" >
</activity>
<activity android:name=".ItemContentActivity" >
</activity>
<service android:name=".RefreshService" />
<provider
android:name=".ReaderContentProvider"
android:authorities="com.igordotsenko.dotsenkorssreader"
android:enabled="true"
android:exported="true" >
</provider>
<service
android:name=".syncadapter.SyncService"
android:exported="true"
>
<!-- android:process=":sync" -->
<intent-filter>
<action android:name="android.content.SyncAdapter" />
</intent-filter>
<meta-data
android:name="android.content.SyncAdapter"
android:resource="@xml/syncadapter" />
</service>
<service
android:name=".syncadapter.AuthenticatorService"
android:exported="true"
>
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator"/>
</intent-filter>
<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator" />
</service>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
android:contentAuthority="com.igordotsenko.dotsenkorssreader"
android:accountType="dummy.com"
android:userVisible="false"
android:supportsUploading="false"
android:allowParallelSyncs="false"
android:isAlwaysSyncable="true"
/>
<?xml version="1.0" encoding="utf-8"?>
<account-authenticator
xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="dummy.com"
/>
ReaderAccountAuthenticator
只是一些基本的方法,没有OUD实现,因为我的帐户只是“虚拟的”
authenticator.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.igordotsenko.dotsenkorssreader" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_SYNC_STATS" />
<application
android:name="com.activeandroid.app.Application"
android:allowBackup="true"
android:icon="@mipmap/ic_rss"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ItemListActivity" >
</activity>
<activity android:name=".ItemContentActivity" >
</activity>
<service android:name=".RefreshService" />
<provider
android:name=".ReaderContentProvider"
android:authorities="com.igordotsenko.dotsenkorssreader"
android:enabled="true"
android:exported="true" >
</provider>
<service
android:name=".syncadapter.SyncService"
android:exported="true"
>
<!-- android:process=":sync" -->
<intent-filter>
<action android:name="android.content.SyncAdapter" />
</intent-filter>
<meta-data
android:name="android.content.SyncAdapter"
android:resource="@xml/syncadapter" />
</service>
<service
android:name=".syncadapter.AuthenticatorService"
android:exported="true"
>
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator"/>
</intent-filter>
<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator" />
</service>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
android:contentAuthority="com.igordotsenko.dotsenkorssreader"
android:accountType="dummy.com"
android:userVisible="false"
android:supportsUploading="false"
android:allowParallelSyncs="false"
android:isAlwaysSyncable="true"
/>
<?xml version="1.0" encoding="utf-8"?>
<account-authenticator
xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="dummy.com"
/>
我发现问题在于,周期性同步的最小间隔是1分钟,所以同步每分钟触发一次,但不是我设置的2秒
问题已解决。我发现问题在于,周期性同步的最小间隔是1分钟,所以同步每分钟触发一次,但不是我设置的2秒 问题已解决。addPeriodicSync()仅适用于在syncadapter.xml中作为true加载的supportsUploading。就这样
android:supportsUploading="true"
addPeriodicSync()仅在syncadapter.xml中与supportsUploading一起使用。就这样
android:supportsUploading="true"
@user1506104我的答案是Kit Kat版本的实际答案,我发现。现在它似乎被设置为至少1小时,这(请参见中的
pollFrequency
)@user1506104我的答案是Kit-Kat版本的实际答案,我发现。现在,它似乎被设置为至少1小时,这(请参见中的pollFrequency