Domanda

I have used Sync Adapter to my app to automatically update the db content,but its not working, even i dint get any log messages

This is my sync adapter class

 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();

    }

And this is my Sync class but unfortunately i cant ovveride the oncreate methods of this class,

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();
    }

Here is my loader class to call the 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();
    }

My manifest

<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>

And my Xml code

<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" />

please check my code and give suggestion to get the solution thanks in advance.

È stato utile?

Soluzione

The sync adapter framework requires each sync adapter to have an account type. http://developer.android.com/training/sync-adapters/creating-sync-adapter.html#CreateAccountTypeAccount

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top