我需要从我的移动应用程序每1分钟连接一次系统服务器以同步数据。为此,我在我的应用程序中使用了SyncAdapter类。对于具有api <23的手机(直到marshmallow),它可以正常工作。当我在具有api> 23的手机上测试我的应用程序时,同步适配器类不会触发。它仅在我第一次在设备上安装应用程序时触发。
我在我的应用程序中使用以下代码。有人能帮我解决这个问题吗?
我在我的应用程序中使用以下代码。有人能帮我解决这个问题吗?
public class MyServiceSyncAdapter extends AbstractThreadedSyncAdapter {
//TODO change this constant SYNC_INTERVAL to change the sync frequency
public static final int SYNC_INTERVAL = 20; //60 * 180; // 60 seconds (1 minute) * 180 = 3 hours
public static final int SYNC_FLEXTIME = SYNC_INTERVAL/3;
private static final int MOVIE_NOTIFICATION_ID = 3004;
public static DataBaseConnection mCon;
public MyServiceSyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
mCon = new DataBaseConnection(this.getContext());
}
@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
Log.i("MyServiceSyncAdapter", "onPerformSync");
//TODO get some data from the internet, api calls, etc.
//TODO save the data to database, sqlite, couchbase, etc
try{
//my code to perform the task
}catch (Exception e){
}
}
public static void configurePeriodicSync(Context context, int syncInterval, int flexTime) {
Account account = getSyncAccount(context);
String authority = context.getString(R.string.content_authority);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// we can enable inexact timers in our periodic sync
SyncRequest request = new SyncRequest.Builder()
.syncPeriodic(syncInterval, flexTime)
.setSyncAdapter(account, authority)
.setExtras(new Bundle()).build();
ContentResolver.requestSync(request);
} else {
ContentResolver.addPeriodicSync(account, authority, new Bundle(), syncInterval);
}
}
public static void syncImmediately(Context context) {
Log.i("MyServiceSyncAdapter", "syncImmediately");
Bundle bundle = new Bundle();
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
ContentResolver.requestSync(getSyncAccount(context), context.getString(R.string.content_authority), bundle);
}
public static Account getSyncAccount(Context context) {
AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE); // Get an instance of the Android account manager
Account newAccount = new Account(context.getString(R.string.app_name), context.getString(R.string.sync_account_type)); // Create the account type and default account
// If the password doesn't exist, the account doesn't exist
if (accountManager.getPassword(newAccount) == null) {
if (!accountManager.addAccountExplicitly(newAccount, "", null)) {
Log.e("MyServiceSyncAdapter", "getSyncAccount Failed to create new account.");
return null;
}
onAccountCreated(newAccount, context);
}
return newAccount;
}
private static void onAccountCreated(Account newAccount, Context context) {
Log.i("MyServiceSyncAdapter", "onAccountCreated");
MyServiceSyncAdapter.configurePeriodicSync(context, SYNC_INTERVAL, SYNC_FLEXTIME);
ContentResolver.setSyncAutomatically(newAccount, context.getString(R.string.content_authority), true);
syncImmediately(context);
}
public static void initializeSyncAdapter(Context context) {
Log.d("MyServiceSyncAdapter", "initializeSyncAdapter");
getSyncAccount(context);
}
}