我正在开发一个Android应用程序,它与服务器执行后台同步(使用SyncAdapter和身份验证等).
启动前台应用程序(使用UI)时,可能正在进行后台同步,或者可选择通过UI按钮启动后台同步.
我想要一种"插入"正在进行的后台同步(无论是由系统启动,还是定期同步设置或UI)的方法,并显示它在前台活动中的进度.
ContentResolver文档(http://developer.android.com/reference/android/content/ContentResolver.html)提到了一个神秘的"SyncObserver",它没有链接到javadoc,也没有记录(我能找到).
还有一些其他页面提到它(http://www.chinaup.org/docs/migrating/m5-0.9/changes/android.content.ContentResolver.html)但我无法找到更多相关信息.
有人实施过这种野兽吗?
如果没有,是否有人有关于跟踪前台活动中后台同步进度的示例代码或建议?
user-interface android synchronization sync android-syncadapter
我正在编写一个Android同步适配器,基本上遇到了无限循环同步的问题.一旦同步完成,它就会重新开始.
谢谢,
问候,
阿克沙伊
@Override
public void onPerformSync(final Account account, final Bundle extras, final String authority, final ContentProviderClient provider, final SyncResult syncResult) {
Log.i("Sync result full sync = " + syncResult.fullSyncRequested);
Log.i("Sync result " + syncResult.toDebugString());
Log.i("Bundle " + extras.toString());
final CountDownLatch latch = new CountDownLatch(3);
final CachedDataReceiver globalStreamRefreshReciever = new CachedDataReceiver(null) {
@Override
protected void onComplete(int resultCode) {latch.countDown();}
@Override
protected void onError() {latch.countDown();}
};
final CachedDataReceiver newMessagesReciever = new CachedDataReceiver(null) {
@Override
protected void onComplete(int resultCode) {latch.countDown();}
@Override
protected …Run Code Online (Sandbox Code Playgroud) 我已经构建了一个Authenticator,我已经构建了一个SyncAdapter(两者都可以通过模拟器上的设置手动执行).
如果没有找到帐户,如何在应用程序启动时让我的应用程序启动登录屏幕(addAccount方法)?
这是我的Manifest.xml ...
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lateral.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="14"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>
<uses-permission android:name="android.permission.READ_SYNC_STATS" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<application android:icon="@drawable/icon" android:label="@string/app_name" android:name=".myappApplication">
<activity android:name=".ui.EventListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:name=".authenticator.AccountAuthenticatorService" android:exported="true" android:process=":auth">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator"/>
</intent-filter>
<meta-data android:name="android.accounts.AccountAuthenticator" android:resource="@xml/authenticator"/>
</service>
<service android:name="sync.EventsSyncAdapterService" android:exported="true" android:process=":events">
<intent-filter>
<action android:name="android.content.SyncAdapter" />
</intent-filter>
<meta-data android:name="android.content.SyncAdapter" android:resource="@xml/sync_events" />
</service>
<activity android:excludeFromRecents="true" android:name=".authenticator.myappAuthenticatorActivity">
<!--
No intent-filter …Run Code Online (Sandbox Code Playgroud) 我正在制作一个具有syncAdapter的应用.
我希望能够与一些将通过其捆绑发送的参数一起执行requestSync.原因是我希望选择syncAdapter实例会做什么.
由于某种原因,putSerializable和putIntArray都会导致ContentResolver抛出异常:
08-16 14:34:49.080: E/AndroidRuntime(10318): java.lang.IllegalArgumentException: unexpected value type: java.util.MiniEnumSet
08-16 14:34:49.080: E/AndroidRuntime(10318): at android.content.ContentResolver.validateSyncExtrasBundle(ContentResolver.java:1144)
08-16 14:34:49.080: E/AndroidRuntime(10318): at android.content.ContentResolver.requestSync(ContentResolver.java:1111)
08-16 14:34:49.080: E/AndroidRuntime(10318): at com.sciatis.syncer.syncing.SyncAdapter.requestSync(SyncAdapter.java:100)
08-16 14:34:49.080: E/AndroidRuntime(10318): at
...
Run Code Online (Sandbox Code Playgroud)
为什么会这样?有没有办法克服这个?把一个整数运行正常,但那些操作没有.
难道我做错了什么 ?有没有更好的方法来实现从活动向syncAdapter发送参数?
serialization android android-intent android-contentresolver android-syncadapter
我们的SyncAdapter实现与Android 4.4.4版不同步.适用于较低的API设备.
我们的应用程序使用AbstractThreadedSyncAdapter与服务器进行通信.我已经使用许多不同的设备和模型对其进行了测试,从API级别10开始运行android.我们最近购买了两台基于Android 4.4.4的设备(Moto E,Nexus 5),并开始遇到SyncAdapter问题.这是xml配置:
<sync-adapter
xmlns:android="http://schemas.android.com/apk/res/android"
android:contentAuthority="our.provider"
android:accountType="our.authenticator"
android:userVisible="false"
android:allowParallelSyncs="false"
android:isAlwaysSyncable="true"
android:supportsUploading="true" />
Run Code Online (Sandbox Code Playgroud)
即使我放了手动和加急标志,同步也不会触发:
Bundle bundle = new Bundle();
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
ContentResolver.requestSync(SyncAdapter.dummyAccount(context), SYNC_PROVIDER, bundle);
Run Code Online (Sandbox Code Playgroud)
我正在尝试通过WiFi和连接电源线来解除任何与电源使用相关的优化问题.目标SDK设置为最新版本:
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="20" />
Run Code Online (Sandbox Code Playgroud)
我拥有所有必要的权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.READ_SYNC_STATS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
Run Code Online (Sandbox Code Playgroud)
有一件事让我感到不同寻常.设备报告API级别= 19,但SDK说API 19等同于android 4.4.2,而不是android 4.4.4:

这可能与这个问题有关吗?
很抱歉没有更多的帮助或描述,但我真的没有关于如何继续调试此问题的线索.提前致谢.
进一步思考:
似乎与同步失败时使用的指数退避算法有关.在开始新请求并使用手动同步(忽略退避)之前,我最终取消了请求.我知道这不太理想,但在我的用例中,用户希望数据能够立即同步.确保给SyncManager一个:https://android.googlesource.com/platform/frameworks/base/+/master/services/core/java/com/android/server/content/SyncManager.java
卸载应用程序并再次运行它可以解决问题,因此可能与帐户的权限有关.请注意,我确实从清单中删除了一些未使用的服务,但没有任何与SyncAdapter或其关联类相关的服务.
我的应用程序有一个根据Android开发人员站点显示的结构创建的同步适配器.该onPerformSync()方法从Internet获取数据并使用批量插入将其插入数据库:
Vector<ContentValues> cVVector = new Vector<ContentValues>(rssItems.size());
for(RssItem rssItem : rssItems) {
ContentValues newsValues = new ContentValues();
// Get data from the remote server
// Fill all the values
newsValues.put(...);
// Add the values to a vector, at the end a BulkInsert will be called
cVVector.add(newsValues);
}
mContext.getContentResolver().bulkInsert(NewsEntry.CONTENT_URI, cvArray);
Run Code Online (Sandbox Code Playgroud)
在处理冲突时,数据库有一个IGNORE政治:
final String SQL_CREATE_NEWS_TABLE = "CREATE TABLE " + NewsEntry.TABLE_NAME + " (" +
NewsEntry._ID + " INTEGER PRIMARY KEY," +
NewsEntry.COLUMN_NEWS_TITTLE + " TEXT UNIQUE NOT NULL, …Run Code Online (Sandbox Code Playgroud) 我的应用数据库中存储的数字很少.我想在联系人应用中匹配这些数字并更新与mimetype的联系.基本上是Whatsapp的作用.他们同步所有联系人,他们的应用程序图标联系.
我已完成同步部分,如果我添加新的联系人,那么我可以在联系人中显示我的应用程序图标.但我的要求是只更新现有联系人.
这是我试图更新联系人,
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true))
.withValueBackReference(Data.RAW_CONTACT_ID,id)
.withValue(Data.MIMETYPE, MIMETYPE)
.withValue(Data.DATA1, "Username")
.withValue(Data.DATA2, "Ite's Me")
.build());
try {
context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
而且rawContactid我从中获取
Cursor cursor = context.getContentResolver().query(Data.CONTENT_URI, new String[] {Data.RAW_CONTACT_ID, Data.DISPLAY_NAME, Data.MIMETYPE, Data.CONTACT_ID },
ContactsContract.CommonDataKinds.Phone.NUMBER + "= ?",
new String[] {phonenumb}, null);
Run Code Online (Sandbox Code Playgroud)
我无法在该联系人中添加连接.我究竟做错了什么.有人可以帮我这个.
提前致谢.
android contactscontract android-syncadapter android-contacts rawcontactid
从应用程序切换器列表中刷出应用程序时,为什么SyncAdapter进程(:sync)被终止?我认为这里的全部意图是让他们脱钩.
编辑:
以下是使用的代码.mUploadTask是一个AsyncTask即时执行该读取(使用SQLite表信息getContext().getContentResolver())和相关的数据上传到后端(使用HttpPost).很直接.
另外,我只实现了一个,onSyncCanceled()因为我SyncAdapter不支持并行同步多个帐户.
public class SyncAdapter extends AbstractThreadedSyncAdapter implements UploadTaskListener {
private static final String TAG = SyncAdapter.class.getSimpleName();
private static volatile UploadTask mUploadTask;
/**
* Set up the sync adapter
*/
public SyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
}
/**
* Set up the sync adapter. This form of the
* constructor maintains compatibility with Android 3.0
* and later platform versions
*/
public SyncAdapter(
Context …Run Code Online (Sandbox Code Playgroud) 我正在开发一个应用程序,我正在使用Android联系人,但无法继续前进.在应用程序中,应用程序的需要是更新的联系人应该发送到服务器或被删除的联系人应该发送到服务器进行同步.
我使用联系服务:
public class ContactService extends Service {
private int mContactCount;
Cursor cursor = null;
static ContentResolver mContentResolver = null;
// Content provider authority
public static final String AUTHORITY = "com.android.contacts";
// Account typek
public static final String ACCOUNT_TYPE = "com.example.myapp.account";
// Account
public static final String ACCOUNT = "myApp";
// Instance fields
Account mAccount;
Bundle settingsBundle;
@Override
public void onCreate() {
super.onCreate();
// Get contact count at start of service
mContactCount = getContactCount();
}
@Override
public int onStartCommand(Intent intent, …Run Code Online (Sandbox Code Playgroud) android android-contentprovider android-syncadapter android-contacts
我正在编写一个使用同步适配器来同步数据的应用程序.
我已经阅读了文档,我很确定我理解它是如何工作的.我的大部分同步适配器都是按照Udi Cohen写的伟大指南来完成的.
但是,我确实有一个问题似乎无法解决,而且在安装我的应用程序时会自动启用同步.
当我的应用程序运行时,它会创建一个同步适配器和一个帐户,完成您希望它完成的所有工作,这很棒.但是,如果我转到"设置">"帐户">"我的应用程序",则同步已关闭.无论如何我可以让它自动启用吗?
设置同步适配器时,我的代码如下所示:
if(accountManager.addAccountExplicitly(account,null,null))
{
// Inform the system that this account supports sync
ContentResolver.setIsSyncable(account, CONTENT_AUTHORITY, 1);
// Inform the system that this account is eligible for auto sync
ContentResolver.setSyncAutomatically(account, CONTENT_AUTHORITY, true);
// Recommend a schedule for auto sync
ContentResolver.addPeriodicSync(account, CONTENT_AUTHORITY, new Bundle(), SYNC_FREQUENCY);
newAccount = true;
}
Run Code Online (Sandbox Code Playgroud)
通过阅读同步适配器,我认为这ContentResolver.setSyncAutomatically()是用于自动启用同步的正确方法.
我也试过设置,ContentResolver.setMasterSyncAutomatically(true);但似乎没有任何影响.
我android.permission.WRITE_SYNC_SETTINGS在AndroidManifest中声明了权限,所以这不是问题所在.我的项目中也有同步服务和xml文件.我在下面附上了这些代码.
还有其他人有过类似的问题吗?这可能是权限问题吗?如果有人有任何建议我会很乐意尝试.谢谢.
AndroidManifest.xml中
<service
android:name=".syncadapter.CalendarSyncService"
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/calendar_syncadapter"/>
</service>
Run Code Online (Sandbox Code Playgroud)
syncadapter.xml
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android" …Run Code Online (Sandbox Code Playgroud)