Как показать сообщение об ошибке синхронизации
Я построил адаптер синхронизации контактов. Все работает нормально, но мне нужна еще одна вещь. Если по какой-либо причине синхронизация не завершается успешно, я хочу показать сообщение, как в аккаунте Google, при сбое синхронизации.
2 ответа
Решение
Решением было установить задержку для результата синхронизации. После этой задержки синхронизация будет возобновлена.
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;
}
Я думаю, что вы хотите, тосты
Простой тост:
Toast.makeText(context, text, duration).show();
text
это, как вы можете себе представить, текст, который вы хотите отобразить.duration
либо Toast.LENGTH_SHORT, либо Toast.LENGTH_LONG (зависит от того, как долго будет виден тост).
Более сложный подход с изображением в тосте: (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>
И в вашем коде:
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();