0

我有一个活动和一个服务。

在我的活动中,一个按钮与服务交互以启动/停止 GPS 记录。

我的服务有 3 个状态指示器:一个用于连接到 Google Play 服务,一个用于主动记录 GPS,一个用于处理记录的内容。

当连接到 Google Play 服务时,服务流程是这样的:

就绪 -> 记录 -> 处理 -> 就绪

该服务将按如下方式广播这些状态:

private void UpdateStatusBroadcast() {
    //Save status variables to intent
    Intent intent = new Intent(this.getString(R.string.BroadcastStatusIntent));
    intent.putExtra(getString(R.string.BroadcastIsConnected), mIsConnected);
    intent.putExtra(getString(R.string.BroadcastIsTripActive), mIsTripActive);
    intent.putExtra(getString(R.string.BroadcastIsProcessing), mIsProcessing);

    //Send the broadcast
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

我的活动收到如下状态:

private class StatusReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        mIsConnected = intent.getBooleanExtra(getString(R.string.BroadcastIsConnected), false);
        mIsTripActive = intent.getBooleanExtra(getString(R.string.BroadcastIsTripActive), false);
        mIsProcessing = intent.getBooleanExtra(getString(R.string.BroadcastIsProcessing), false);

        HandleConnectionStatus();
        HandleTripStatus();
    }
}

然后是我的问题。在HandleTripStatus()下面发布的 中,我更改了按钮的文本和背景以反映服务当前正在执行的操作。这适用于第一种和第三种情况。尽管接收到正确的布尔值,但我从未看到绘制第二个背景。

private void HandleTripStatus() {
    Button tripButton = (Button) findViewById(R.id.TripButton);
    Button liveMapButton = (Button) findViewById(R.id.LiveMapButton);

    if (mIsTripActive) {
        tripButton.setText(R.string.TripButtonTitleStop);
        tripButton.setBackground(ContextCompat.getDrawable(mContext, R.drawable.trip_button_stop_shape));
        liveMapButton.setEnabled(true);
    } else if (mIsProcessing) {
        tripButton.setText(R.string.TripButtonTitleStopping);
        tripButton.setBackground(ContextCompat.getDrawable(mContext, R.drawable.trip_button_stopping_shape));
        liveMapButton.setEnabled(false);
    } else {
        tripButton.setText(R.string.TripButtonTitleStart);
        tripButton.setBackground(ContextCompat.getDrawable(mContext, R.drawable.trip_button_start_shape));
        liveMapButton.setEnabled(false);
    }
}

为了调试问题,我验证了以下内容:

  • 正确定义了文本和背景资源(即尝试使用它而不是第一种和第三种情况有效)
  • if-else 条件在预期时运行(即“else if”条件实际上在我期望的时候运行。通过断点验证。)
  • 在此过程中不使用其他 if-else 条件。(即,运行正确的条件。)

其他一些可能相关的代码:

这就是 Activity 请求停止 GPS 记录的方式(在完成之前导致处理步骤)

private void EndTrip() {
    //Create message to TripService with intent to run case for END_TRIP
    Message message = Message.obtain(null, TripService.END_TRIP, 0, 0);

    //Send the Message to the Service
    try {
        mMessenger.send(message);
        Toast.makeText(mContext, R.string.TripStopToast, Toast.LENGTH_SHORT).show();
    } catch (RemoteException e) {
        Log.e("Debug", "Failed to contact TripService");
    }
}

这是从 Activity 接收到消息后在 Service 中发生的结构。

private void EndTrip() {
    //Stop retrieving location updates

    //Broadcast the updated status and begin processing the trip
    mIsTripActive = false;
    mIsProcessing = true;
    UpdateStatusBroadcast();

    //Processing the collected data

    //Finish up
    mIsProcessing = false;
    UpdateStatusBroadcast();
    stopForeground(true);
}

我完全没有想法。原因可能是什么?为什么按钮背景在 else-if 中没有变化?

4

1 回答 1

0

经过太多小时的反复试验,我发现原因与线程有关。

我学到的是:

  • 我的服务做它的工作(处理)会挂起 UI 线程直到完成
  • 这很简单,因为服务UI 线程上运行
  • Android 不会自动在与应用程序的其余部分分开的线程中运行服务。
  • 可以在不同的线程运行您的服务。为此,请将以下内容添加到您的服务内的 AndroidManifest 中:

    android:process=":WhateverNameYouLikeForYourThread"
    

请注意,这当然破坏了我所依赖的广播。然而,这很容易解决;结果是我不能再使用了LocalBroadcastManager

举个例子 - 而不是

LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

我现在用

sendBroadcast(intent);

反而。然而,这确实意味着广播不那么私密。

于 2016-03-17T20:17:32.693 回答