アクティビティとサービスがあります。
私のアクティビティでは、ボタンがサービスとやり取りして、GPS ログの開始/停止を行います。
My Service には 3 つの状態インジケーターがあります。1 つは Google Play Services に接続されているため、1 つはアクティブに GPS をログに記録するため、もう 1 つはログに記録された内容を処理するためです。
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);
}
My 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()
以下に投稿された では、ボタンのテキストと背景を変更して、サービスが現在行っていることを反映します。これは、1 番目と 3 番目のケースでうまく機能します。ただし、正しいブール値を受け取ったにもかかわらず、2 番目の背景が描画されることはありません。
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);
}
}
問題をデバッグするために、次のことを確認しました。
- テキストと背景リソースが正しく定義されている (つまり、最初と 3 番目のケースの代わりにそれを使用しようとしている)
- if-else 条件は期待どおりに実行されます (つまり、"else if" 条件は、期待どおりに実際に実行されます。ブレークポイントによって検証されます)。
- このプロセスでは、他の if-else 条件は使用されません。(つまり、正しい条件のみが実行されます。)
関連する可能性がある他のコード:
これは、アクティビティが 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");
}
}
これは、アクティビティからメッセージを受信した後にサービスで何が起こるかの構造です。
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 でボタンの背景が変わらないのはなぜですか?