我已经使用同步适配器到我的应用程序来自动更新数据库内容,但它不起作用,即使我得到任何日志消息
这是我的同步适配器类
public class SynAdapter extends AbstractThreadedSyncAdapter {
private ContentResolver mResolver;
private Context mcontext;
public SynAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
// TODO Auto-generated constructor stub
this.mcontext = context;
}
@Override
public void onPerformSync(Account account, Bundle extras, String authority,
ContentProviderClient provider, SyncResult syncResult) {
// TODO Auto-generated method stub
Log.d("onperfor", "onPerfoem");
final SyncLoader mLoader = new SyncLoader(mcontext);
mResolver = mcontext.getContentResolver();
mLoader.onContentChanged();
}
这是我的Sync类,但遗憾的是我无法使用此类的oncreate方法,
public class SyncService extends Service {
private static SynAdapter sSyncAdapter = null;
// Object to use as a thread-safe lock
private static final Object sSyncAdapterLock = new Object();
private static final Context context = null;
public SyncService() {
super(null, null, null, null, null, null);
// TODO Auto-generated constructor stub
}
public void onCreate() {
/*
* Create the sync adapter as a singleton. Set the sync adapter as
* syncable Disallow parallel syncs
*/
Log.d("Sync service", "service");
Log.d("Sync service", "service");
sSyncAdapter = new SynAdapter(context, true);
}
public IBinder onBind(Intent intent) {
/*
* Get the object that allows external processes to call
* onPerformSync(). The object is created in the base class code when
* the SyncAdapter constructors call super()
*/
return sSyncAdapter.getSyncAdapterBinder();
}
这是我的加载器类来调用web url
public class SyncLoader extends AsyncTaskLoader<String> {
private Context context;
private String value;
public SyncLoader(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.context = context;
}
@Override
public String loadInBackground() {
// TODO Auto-generated method stub
JSONObject json = Getjsonurl.getJsonUrl(Utility.gBasepath
+ "getGocashNotifications/" + Session.getUserId(context),
context);
Log.d("json", "---->" + json);
value = String.valueOf(json);
Toast.makeText(context, String.valueOf(json), Toast.LENGTH_SHORT)
.show();
return value;
}
@Override
public void onContentChanged() {
// TODO Auto-generated method stub
super.onContentChanged();
loadInBackground();
}
我的清单
<service
android:name="com.example.app.SyncService"
android:exported="true" >
<intent-filter>
<action android:name="android.content.SyncAdapter" />
</intent-filter>
<meta-data
android:name="android.content.SyncAdapter"
android:resource="@xml/sync_myapp" />
</service>
我的Xml代码
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="com.android.example.datasync"
android:allowParallelSyncs="false"
android:contentAuthority="com.example.android.datasync.provider"
android:isAlwaysSyncable="true"
android:supportsUploading="false"
android:userVisible="false" />
请检查我的代码并提出建议以提前获得解决方案。
答案 0 :(得分:0)