【问题标题】:How to show sync failed message如何显示同步失败消息
【发布时间】:2013-01-27 13:11:38
【问题描述】:

我已经构建了一个联系人同步适配器。一切正常,但我还需要一件事。如果由于某种原因同步未成功完成,我想在同步失败时显示一条消息,例如 Google 帐户正在显示

【问题讨论】:

  • 那么您是在问如何显示此消息,或者需要捕获哪些异常才能知道同步失败?
  • 我在捕获异常的onPerformSync 中有catch 块,在这些块中我有syncResult.stats.numParseExceptions++syncResult.stats.numIoExceptions++。我希望这是捕获异常的正确方法。如果是这样,我需要如何显示消息

标签: android sync android-contacts contactscontract android-syncadapter


【解决方案1】:

解决方案是设置同步结果的延迟。在此延迟之后,同步将重新启动。

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

【讨论】:

  • 它会在屏幕底部显示一条消息,就像图片上的一样,并在syncResult.delayUntil = numOfSeconds;指定的秒数后重试同步
【解决方案2】:

我想你想要的是Toasts

简单吐司:

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

text 是您想显示的文本。 duration 是 Toast.LENGTH_SHORT 或 Toast.LENGTH_LONG(取决于 Toast 的可见时间)

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>

在你的代码中:

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

【讨论】:

  • 不,我不想干杯。我很清楚什么是吐司。我想在帐户中显示一条消息并完全按照同步失败时 Google 帐户显示一条消息的方式进行同步
  • 不应该被否决... toast 的唯一问题是它位于 SyncAdapter.java 中的非活动类中
  • + 同步在后台执行,显示来自 bg 的 toast 不起作用。
猜你喜欢
  • 2011-04-10
  • 2022-10-19
  • 2019-01-30
  • 2017-08-19
  • 2017-08-12
  • 2016-12-05
  • 2011-07-02
  • 1970-01-01
  • 2020-05-09
相关资源
最近更新 更多