【问题标题】:BroadcastReceiver - Can't get button to change backgroundBroadcastReceiver - 无法获得更改背景的按钮
【发布时间】:2016-03-17 17:15:25
【问题描述】:

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

在我的 Activity 中,一个按钮与服务交互以启动/停止 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);
}

我的 Activity 接收状态如下:

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中的按钮背景没有变化?

【问题讨论】:

    标签: android broadcastreceiver localbroadcastmanager


    【解决方案1】:

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

    我学到了什么:

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

      android:process=":WhateverNameYouLikeForYourThread"
      

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

    举例 - 代替

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

    我现在用

    sendBroadcast(intent);
    

    相反。然而,这确实意味着广播的私密性较低。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多