我终于找到了解决问题的方法。这个想法是使用 ContentResolver 的getCurrentSyncs()或getCurrentSync()方法,无论哪个可用。以下方法将检查同步操作当前是否适用于帐户和权限。它需要 API 级别 8 (Froyo = Android 2.2)。
private static boolean isSyncActive(Account account, String authority)
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
return isSyncActiveHoneycomb(account, authority);
} else
{
SyncInfo currentSync = ContentResolver.getCurrentSync();
return currentSync != null && currentSync.account.equals(account) &&
currentSync.authority.equals(authority);
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private static boolean isSyncActiveHoneycomb(Account account, String authority)
{
for(SyncInfo syncInfo : ContentResolver.getCurrentSyncs())
{
if(syncInfo.account.equals(account) &&
syncInfo.authority.equals(authority))
{
return true;
}
}
return false;
}
然后 Activity 在 中注册更新onResume()
并在 中取消注册onDestroy()
。此外,必须手动更新状态onResume()
以赶上当前状态。
这是一个可以做到这一点的实现。子类应该自己定义
- 使用什么帐户(实施
getAccount()
)
- 使用什么权限(字段
CONTENT_AUTHORITY
)
- 如何显示同步状态(实现
updateState(boolean isSynchronizing)
)
我希望它会在未来对某人有所帮助。
import android.accounts.Account;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.SyncInfo;
import android.content.SyncStatusObserver;
import android.os.Build;
import android.os.Bundle;
public abstract class SyncActivity extends Activity
{
private static final String CONTENT_AUTHORITY = "com.example.authority";
private Object syncHandle;
private SyncStatusObserver observer;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
observer = new SyncStatusObserver()
{
@Override
public void onStatusChanged(int which)
{
runOnUiThread(new Runnable()
{
@Override
public void run()
{
Account account = getAccount();
boolean isSynchronizing =
isSyncActive(account, CONTENT_AUTHORITY);
updateState(isSynchronizing);
}
});
}
};
}
@Override
protected void onResume()
{
super.onResume();
// Refresh synchronization status
observer.onStatusChanged(0);
// Watch for synchronization status changes
final int mask = ContentResolver.SYNC_OBSERVER_TYPE_PENDING |
ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE;
syncHandle = ContentResolver.addStatusChangeListener(mask, observer);
}
@Override
protected void onPause()
{
super.onPause();
// Remove our synchronization listener if registered
if (syncHandle != null)
{
ContentResolver.removeStatusChangeListener(syncHandle);
syncHandle = null;
}
}
private static boolean isSyncActive(Account account, String authority)
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
return isSyncActiveHoneycomb(account, authority);
} else
{
SyncInfo currentSync = ContentResolver.getCurrentSync();
return currentSync != null && currentSync.account.equals(account)
&& currentSync.authority.equals(authority);
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private static boolean isSyncActiveHoneycomb(Account account,
String authority)
{
for(SyncInfo syncInfo : ContentResolver.getCurrentSyncs())
{
if(syncInfo.account.equals(account) &&
syncInfo.authority.equals(authority))
{
return true;
}
}
return false;
}
protected abstract Account getAccount();
protected abstract void updateState(boolean isSynchronizing);
}