【问题标题】:Make ConnectionResult to a parcelable or serializable object将 ConnectionResult 设置为可打包或可序列化的对象
【发布时间】:2014-03-06 02:36:30
【问题描述】:

我们可以将一个可打包或可序列化的对象附加到一个意图发送到其他活动,如下所示。

intent.putExtra(string, parceable);

我想发送ConnectionResult,但我看到 ConnectionResult 既不可解析也不可序列化。我不想使用任何静态字段。

有什么方法可以使这个 ConnectionResult 可打包或可序列化?

【问题讨论】:

    标签: android android-intent android-location


    【解决方案1】:

    ConnectionResult 不可打包,但它有一个构造函数,该构造函数接受一个 errorCode 和一个 pendingIntent。这两个都是可以打包的。

    所以,为您服务:

    private void notifyUiFailedConnection(ConnectionResult result) {
        Intent intent = new Intent(FIT_NOTIFY_INTENT);
        intent.putExtra(FIT_EXTRA_NOTIFY_FAILED_STATUS_CODE, result.getErrorCode());
        intent.putExtra(FIT_EXTRA_NOTIFY_FAILED_INTENT, result.getResolution());
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }
    

    在你的活动中:

    private BroadcastReceiver mFitStatusReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Get extra data included in the Intent
            if (intent.hasExtra(GoogleFitService.FIT_EXTRA_NOTIFY_FAILED_STATUS_CODE) &&
                    intent.hasExtra(GoogleFitService.FIT_EXTRA_NOTIFY_FAILED_STATUS_CODE)) {
                //Recreate the connection result
                int statusCode = intent.getIntExtra(GoogleFitService.FIT_EXTRA_NOTIFY_FAILED_STATUS_CODE, 0);
                PendingIntent pendingIntent = intent.getParcelableExtra(GoogleFitService.FIT_EXTRA_NOTIFY_FAILED_INTENT);
                ConnectionResult result = new ConnectionResult(statusCode, pendingIntent);
                fitHandleFailedConnection(result);
            }
        }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      • 2021-06-17
      • 1970-01-01
      • 2012-09-20
      • 2014-04-12
      • 1970-01-01
      • 2011-10-17
      相关资源
      最近更新 更多