gpt4 book ai didi

android - 无法执行同步(未调用 onPerformSync)

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:08:28 24 4
gpt4 key购买 nike

我关注这个标题Why does ContentResolver.requestSync not trigger a sync?但同步方法不会出现在设置屏幕的帐户和同步中。我认为 onPerformSync() 没有被调用。我尝试调试但没有找到错误日志。这是我的代码:

syncAdapter.xml

<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="@string/ACCOUNT_TYPE"
android:contentAuthority="@string/AUTHORITY"
android:userVisible="true"
android:isAlwaysSyncable="true"

/>

同步服务.java

public class CalendarSyncService extends Service{
private static SyncAdapterImpl sSyncAdapter = null;
private static String list_event_ids;
private static Requestor requestor;

public void onCreate(){
super.onCreate ();
if (sSyncAdapter == null)
sSyncAdapter = new SyncAdapterImpl(this);
}

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return sSyncAdapter.getSyncAdapterBinder();
}

public class SyncAdapterImpl extends AbstractThreadedSyncAdapter {

Context context;
public SyncAdapterImpl(Context context) {
super(context, true);
// TODO Auto-generated constructor stub
this.context = context;
}

@Override
public void onPerformSync(Account arg0, Bundle arg1, String arg2,
ContentProviderClient arg3, SyncResult arg4) {
// TODO Auto-generated method stub
Log.e("thuutai", "Perform Sync Call");

try {
CalendarSyncService.performSync(context);
} catch (DOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

我在这里调用它

public void onClick(View arg0) {
Bundle params = new Bundle();
params.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false);
params.putBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false);
params.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);
ContentResolver.addPeriodicSync(selectedAccount, getString(R.string.AUTHORITY), params, 3600);
ContentResolver.setSyncAutomatically(selectedAccount, getString(R.string.AUTHORITY), true);

ContentResolver.requestSync(selectedAccount, getString(R.string.AUTHORITY), params);}

list .xml

 <service android:name=".service.CalendarSyncService" 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>

我在 Nexus S Android 4.0 中测试过。感谢您的阅读!

最佳答案

有时由于正在进行类似的同步,此方法不会被调用。您可以使用以下代码检查并取消:

if (ContentResolver.isSyncPending(yourAccount, yourAuthority)  ||
ContentResolver.isSyncActive(yourAccount, yourAuthority)) {
Log.i("ContentResolver", "SyncPending, canceling");
ContentResolver.cancelSync(yourAccount, yourAuthority);
}

关于android - 无法执行同步(未调用 onPerformSync),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13132865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com