我需要每10秒触发一次syncAdapter类中的performSync,但它甚至一次也不会触发。为此,使用contentResolver的addPeriodicSync函数。我还具有触发contentResolver的requestSync的按钮。单击我的按钮performSync被执行后,这个效果很好,但是为什么periodicSync功能不起作用。
这是我的主要活动代码。
package com.stalnobcrs.syncadapdemo;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
//...
// Constants
public static String AUTHORITY="";
public static final String ACCOUNT = "default_account";
// An account type, in the form of a domain name
public static String ACCOUNT_TYPE="";
private static final String TAG=MainActivity.class.getName();
// Global variables
ContentResolver mResolver;
Button sync;
//...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//...
// Get the content resolver object for your app
AUTHORITY=getApplicationContext().getString(R.string.content_authority);
ACCOUNT_TYPE=getApplicationContext().getString(R.string.sync_account_type);
mResolver = getContentResolver();
mAccount=CreateSyncAccount(this);
setContentView(R.layout.activity_main);
ContentResolver.setIsSyncable(mAccount,AUTHORITY, 1);
// Inform the system whether this account is eligible for auto sync when the network
// is up. In your case should be false
ContentResolver.setSyncAutomatically(mAccount, AUTHORITY, false);
// Add a schedule for automatic synchronisation. The system may call the sync function
// a few seconds early or late for battery and network optimisation.
ContentResolver.addPeriodicSync(mAccount, AUTHORITY, Bundle.EMPTY,
10);
}
//...
// Constants
// Instance fields
Account mAccount;
// ...
/**
* Create a new dummy account for the sync adapter
*
* @param context The application context
*/
public static Account CreateSyncAccount(Context context) {
// Create the account type and default account
Log.i(TAG,ACCOUNT+" "+ACCOUNT_TYPE);
Account newAccount = new Account(ACCOUNT,ACCOUNT_TYPE);
// Get an instance of the Android account manager
AccountManager accountManager =
(AccountManager) context.getSystemService(
ACCOUNT_SERVICE);
/*
* Add the account and account type, no password or user data
* If successful, return the Account object, otherwise report an error.
*/
assert accountManager != null;
if (accountManager.addAccountExplicitly(newAccount, "", null)) {
/*
* If you don't set android:syncable="true" in
* in your <provider> element in the manifest,
* then call context.setIsSyncable(account, AUTHORITY, 1)
* here.
*/
Log.i(TAG, "CreateSyncAccount Successful");
return newAccount;
} else {
/*
* The account exists or some other error occurred. Log this, report it,
* or handle it internally.
*/
Log.i(TAG,"Already account exist or some error occurred in CreateSyncAccount");
return newAccount;
}
}
public void onRefreshButtonClick(View v) {
//...
// Pass the settings flags by inserting them in a bundle
Bundle settingsBundle = new Bundle();
settingsBundle.putBoolean(
ContentResolver.SYNC_EXTRAS_MANUAL, true);
settingsBundle.putBoolean(
ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
/*
* Request the sync for the default account, authority, and
* manual sync settings
*/
Log.i(TAG,"Button clicked");
Log.i(TAG,AUTHORITY+" "+mAccount);
ContentResolver.requestSync(mAccount, AUTHORITY, settingsBundle);
}
}
这是我的清单文件。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stalnobcrs.syncadapdemo">
<uses-permission
android:name="android.permission.INTERNET"/>
<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"
android:maxSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
<service
android:name="com.stalnobcrs.syncadapdemo.SyncService"
android:exported="true">
<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="com.stalnobcrs.syncadapdemo.AuthenticatorService">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator"/>
</intent-filter>
<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator" />
</service>
<provider
android:name="com.stalnobcrs.syncadapdemo.StubProvider"
android:authorities="com.stalnobcrs.syncadapdemo"
android:exported="false"
android:syncable="true"/>
</application>
这是我的syncAdapter xml
<?xml version="1.0" encoding="utf-8"?>
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
android:contentAuthority="@string/content_authority"
android:accountType="@string/sync_account_type"
android:userVisible="false"
android:supportsUploading="false"
android:allowParallelSyncs="false"
android:isAlwaysSyncable="false"/>
有人可以帮我吗?