쨍쨍
#7. AI Smart Home_ModeActivity 본문
[ Mode 화면 및 기능 ]
[ activity_mode.xml ]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/Primary">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="모드 선택"
android:textColor="#ffffff"
android:textSize="30dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
</LinearLayout>
<Button
android:id="@+id/sleep_button"
android:layout_width="280dp"
android:layout_height="110dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#ffffff"
android:background="@color/Button"
android:text="수면 모드"
android:layout_marginTop="30dp"
android:layout_gravity="center"/>
<Button
android:id="@+id/out_button"
android:layout_width="280dp"
android:layout_height="110dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#ffffff"
android:background="@color/Button"
android:text="외출 모드"
android:layout_marginTop="30dp"
android:layout_gravity="center"/>
<Button
android:id="@+id/in_button"
android:layout_width="280dp"
android:layout_height="110dp"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#ffffff"
android:background="@color/Button"
android:text="귀가 모드"
android:layout_marginTop="30dp"
android:layout_gravity="center"/>
</LinearLayout>
[ ModeActivity.java ]
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.IOException;
public class ModeActivity extends AppCompatActivity implements SocketActivity.OnDataReceivedListener {
private Button inButton, outButton, sleepButton;
private static final String TAG = "ModeActivity";
private OkHttpClient httpClient;
private SocketActivity socketActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mode);
// OkHttpClient 초기화
httpClient = new OkHttpClient();
// 버튼 초기화
inButton = findViewById(R.id.in_button);
outButton = findViewById(R.id.out_button);
sleepButton = findViewById(R.id.sleep_button);
// SocketActivity 인스턴스를 가져와서 서버 연결 시작
socketActivity = SocketActivity.getInstance();
socketActivity.connectToServer(this);
// 버튼 이벤트 리스너 설정
inButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendLedSignal("LED_ON");
sendLedSignal("Recommend_ON");
Toast.makeText(ModeActivity.this, "LED_ON, Recommend_ON", Toast.LENGTH_SHORT).show();
}
});
outButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendLedSignal("LED_OFF");
sendLedSignal("AC_OFF");
sendLedSignal("HFAN_OFF");
Toast.makeText(ModeActivity.this, "LED_OFF, AC_OFF, HFAN_OFF", Toast.LENGTH_SHORT).show();
}
});
sleepButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendLedSignal("LED_OFF");
Toast.makeText(ModeActivity.this, "LED_OFF", Toast.LENGTH_SHORT).show();
}
});
}
// 서버에 신호를 전송하는 메서드
private void sendLedSignal(String signal) {
if (socketActivity != null) {
socketActivity.sendData(signal);
Log.d(TAG, "Sent LED signal: " + signal); // 신호 전송 시 로그 작성
} else {
Log.e(TAG, "SocketActivity is null, cannot send signal.");
}
}
// 서버에서 데이터 수신 시 호출되는 메서드
@Override
public void onDataReceived(String data) {
Log.d(TAG, "Received: " + data); // 수신 데이터 로그 출력
parseAndSetData(data);
if (data.equals("BTN_ON")) {
runOnUiThread(this::showAlertForBtnOn); // UI 스레드에서 팝업 실행
}
}
private void parseAndSetData(String data) {
}
// 비상버튼 팝업창
private void showAlertForBtnOn() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// 커스텀 레이아웃 인플레이션
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.emergency, null);
// AlertDialog에 커스텀 레이아웃 설정
builder.setView(dialogView)
.setTitle("알림")
.setPositiveButton("확인", (dialog, which) -> dialog.dismiss())
.show();
}
@Override
protected void onDestroy() {
super.onDestroy();
// 액티비티 종료 시 서버 연결 해제
if (socketActivity != null) {
socketActivity.disconnect();
}
}
}
'프로그래밍 코드 > Android Studio' 카테고리의 다른 글
#8. AI Smart Home_CctvActivity (1) | 2024.12.17 |
---|---|
#6. Al Smart Home_TipActivity (1) | 2024.12.17 |
#5. Al Smart Home_HomeActivty (3) | 2024.12.17 |
#4. AI Smart Home_SocketActivity (3) | 2024.12.17 |
AI 심리 분석 프로그램 도담이 (1) | 2024.07.22 |