BroadcastReceiver - 无法获取更改背景的按钮
BroadcastReceiver - Can't get button to change background
我有一个 Activity 和一个服务。
在我的 Activity 中,一个按钮与服务交互以 start/stop GPS 记录。
我的服务有 3 个状态指示器:一个用于连接到 Google Play 服务,一个用于主动记录 GPS,还有一个用于处理记录的内容。
当连接到 Google 播放服务时,服务流程是这样的:
就绪 -> 记录 -> 处理 -> 就绪
服务将按如下方式广播这些状态:
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.
接收到消息后在服务中发生的结构
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中没有改变?
经过太多小时的反复试验,我发现原因与线程有关。
我学到了什么:
- 我的服务正在执行其工作(处理)会挂起 UI 线程直到完成
- 这很简单,因为该服务 运行在 上 UI 线程
- Android 不 自动运行 在线程中与应用程序的其余部分分开的服务。
可以在不同的线程上运行您的服务。为此,请将以下内容添加到服务中的 AndroidManifest:
android:process=":WhateverNameYouLikeForYourThread"
请注意,这当然破坏了我所依赖的广播。然而,这很容易修复;结果是我不能再使用LocalBroadcastManager
举例 - 而不是
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
我现在用
sendBroadcast(intent);
相反。然而,这确实意味着广播不那么私密。
我有一个 Activity 和一个服务。
在我的 Activity 中,一个按钮与服务交互以 start/stop GPS 记录。
我的服务有 3 个状态指示器:一个用于连接到 Google Play 服务,一个用于主动记录 GPS,还有一个用于处理记录的内容。
当连接到 Google 播放服务时,服务流程是这样的:
就绪 -> 记录 -> 处理 -> 就绪
服务将按如下方式广播这些状态:
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.
接收到消息后在服务中发生的结构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中没有改变?
经过太多小时的反复试验,我发现原因与线程有关。
我学到了什么:
- 我的服务正在执行其工作(处理)会挂起 UI 线程直到完成
- 这很简单,因为该服务 运行在 上 UI 线程
- Android 不 自动运行 在线程中与应用程序的其余部分分开的服务。
可以在不同的线程上运行您的服务。为此,请将以下内容添加到服务中的 AndroidManifest:
android:process=":WhateverNameYouLikeForYourThread"
请注意,这当然破坏了我所依赖的广播。然而,这很容易修复;结果是我不能再使用LocalBroadcastManager
举例 - 而不是
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
我现在用
sendBroadcast(intent);
相反。然而,这确实意味着广播不那么私密。