예제를 시작하기 전에 싱글톤 디자인 패턴이 무엇인지 알아야 합니다. 싱글톤은 클래스의 인스턴스화를 하나의 인스턴스로만 제한하는 디자인 패턴입니다. 주목할만한 용도로는 동시성을 제어하고 애플리케이션이 데이터 저장소에 액세스할 수 있도록 중앙 액세스 지점을 만드는 것이 있습니다.
동기화됨은 한 번에 하나의 스레드만 액세스할 수 있음을 의미합니다.
이 예는 안드로이드에서 동기화를 사용하여 싱글톤 대화 상자를 사용하는 방법을 보여줍니다.
1단계 − Android Studio에서 새 프로젝트를 생성하고 파일 ⇒ 새 프로젝트로 이동하여 필요한 모든 세부 정보를 입력하여 새 프로젝트를 생성합니다.
2단계 − res/layout/activity_main.xml에 다음 코드를 추가합니다.
<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android"
xmlns:tools = "https://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity"
android:orientation = "vertical">
<Button
android:id = "@+id/start"
android:text = "start dialog in singleTone"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content" />
<Button
android:id = "@+id/end"
android:text = "end dialog singleTone"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content" />
</LinearLayout> 위의 코드에서는 두 개의 버튼을 사용했습니다. 사용자가 시작 버튼을 클릭하면 진행 대화 상자가 표시되고 종료 버튼은 진행 대화 상자를 닫는 데 사용됩니다.
3단계 − src/MainActivity.java
에 다음 코드 추가package com.example.andy.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button start;
singleTonExample singletonexample;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = findViewById(R.id.start);
singletonexample = singleTonExample.getInstance();
findViewById(R.id.end).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
singletonexample.dismiss();
}
});
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
singletonexample.show(MainActivity.this);
}
});
}
} 위의 코드에서는 singleTonExample 을 사용했습니다. 싱글톤 클래스이므로 singleTonExample.java로 호출을 생성합니다. 다음 코드를 추가하십시오-
package com.example.andy.myapplication;
import android.app.Dialog;
import android.content.Context;
import android.view.Window;
public class singleTonExample {
private Dialog dialog;
private static final singleTonExample ourInstance = new singleTonExample();
public static synchronized singleTonExample getInstance() {
return ourInstance;
}
private singleTonExample() { }
public void show(Context context) {
if (dialog != null && dialog.isShowing()) {
return;
}
dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.layout_progress_dialog);
dialog.setCancelable(true);
dialog.show();
}
public void dismiss() {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
}
} 위 코드에서 Dialog용 콘텐츠 보기를 추가하였으므로 res 폴더에 layout_progress_dialog.xml로 레이아웃을 생성하고 다음 콘텐츠를 추가합니다. –
<?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "match_parent" android:layout_height = "match_parent" android:gravity = "center"> <ProgressBar android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:id = "@+id/progress" /> </LinearLayout>
응용 프로그램을 실행해 보겠습니다. 실제 Android 모바일 장치를 컴퓨터에 연결했다고 가정합니다. Android 스튜디오에서 앱을 실행하려면 프로젝트의 활동 파일 중 하나를 열고 도구 모음에서 실행 아이콘을 클릭합니다. 모바일 장치를 옵션으로 선택한 다음 기본 화면을 표시할 모바일 장치를 확인하십시오 –

이제 싱글톤에서 대화상자 표시를 클릭하면 아래와 같이 싱글톤 클래스의 대화상자가 표시됩니다. –
