3

I've build a contacts sync adapter. It's all working fine but I need one more thing. If for some reason the sync does not complete successfully, I want to show a message like Google account is showing when the sync fails

Screenshot

Goo
  • 1,318
  • 1
  • 13
  • 31
nikmin
  • 1,803
  • 3
  • 28
  • 46
  • So are you asking how to show this message, or which exceptions need to be catched in order to know the sync has failed? – Martin Grohmann Feb 12 '13 at 09:13
  • I have `catch` blocks in the `onPerformSync` that's catching the exceptions, and in those blocks I have `syncResult.stats.numParseExceptions++` and `syncResult.stats.numIoExceptions++`. I hope that this is the right way to catch the exceptions. If so, I need how to show the message – nikmin Feb 12 '13 at 09:15

2 Answers2

7

The solution was to set the delay on the sync result. After this delay the sync will be restarted.

try {
    DO THE SYNCHRONIZATION
} catch (AuthenticationException e) {
    Log.e(TAG, "AuthenticationException");
    syncResult.stats.numAuthExceptions++;
    syncResult.delayUntil = 180;
} catch (ParseException e) {
    Log.e(TAG, "ParseException");
    syncResult.stats.numParseExceptions++;
} catch (IOException e) {
    Log.e(TAG, "IOException");
    syncResult.stats.numIoExceptions++;
    syncResult.delayUntil = 180;
}
nikmin
  • 1,803
  • 3
  • 28
  • 46
  • It will show a message in the bottom of the screen like the one on the picture, and will retry the sync after the amount of seconds specified with `syncResult.delayUntil = numOfSeconds;` – nikmin Feb 12 '13 at 10:23
-3

I think what you want are Toasts

Simple Toast:

Toast.makeText(context, text, duration).show();

text is, as you can imagine, the text you want to be displayed. duration is either Toast.LENGTH_SHORT or Toast.LENGTH_LONG (depends in how long the Toast sahll be visible)

More complicated approach with picture in the toast: (sync_toast_lo.xml)

<?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/SynctoastLayout"
    android:background="@android:color/black">

  <ImageView
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:src="@drawable/your_logo"
    android:layout_toLeftOf="@+id/textView"
    android:layout_margin="5dip"
    android:id="@+id/syncLogo">
  </ImageView>

  <TextView
    android:id="@+id/syncFailedText"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:text="The sync has failed!"
    android:gravity="center"
    android:textColor="@android:color/white">
  </TextView>
</RelativeLayout>

And in your code:

LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.Sync_toast_lo,
                               (ViewGroup) findViewById(R.id.SynctoastLayout));

Toast toast = new Toast(this);
toast.setView(view);
toast.show();
Martin Grohmann
  • 437
  • 2
  • 17
  • NO, I don't want toast. I know very good what a toast is. I want to show a message in accounts and sync exactly as the Google account shows a message when the sync fails – nikmin Feb 12 '13 at 09:36
  • shouldn't have been downvoted... The only problem with a toast is that it's in a non-activity class in the SyncAdapter.java – Brandon Feb 17 '15 at 21:31
  • + sync is executed in the background and showing a toast from bg doesn't work. – User May 09 '17 at 11:39