투케이2K

74. (AndroidStudio/android/java) LifecycleObserver 라이브러리 사용해서 액티비티 포그라운드 및 백그라운드 상태 확인 본문

Android

74. (AndroidStudio/android/java) LifecycleObserver 라이브러리 사용해서 액티비티 포그라운드 및 백그라운드 상태 확인

투케이2K 2021. 3. 1. 09:58
반응형

/* =========================== */

[ 개발 환경 설정 ]

개발 툴 : AndroidStudio

개발 언어 : java

/* =========================== */

/* =========================== */

[소스 코드]

 

//========== [build.gradle(Project) 파일] ==========
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.0.0"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

        //====라이프사이클 사용위함====
        maven { url "https://jitpack.io" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}





//========== [build.gradle(Module:app) 파일] ==========
apply plugin: 'com.android.application'

android {
    compileSdkVersion 29

    defaultConfig {
        applicationId "kr.co.2k.myapplication"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    //====라이프사이클 사용위함====
    implementation "androidx.lifecycle:lifecycle-runtime:2.0.0"
    implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
}






//========== [java - 초기 라이프 사이클 메소드 호출 : onCreate 부분] ==========
setupLifecycleObserver();





//========== [java - 라이프 사이클 메소드 작성] ==========
private void setupLifecycleObserver(){
	ProcessLifecycleOwner.get().getLifecycle().addObserver(lifecycleListener);
}





//========== [java - Toasty 사용 실시] ==========
package kr.co.2k.myapplication;

import android.content.Context;
import android.util.Log;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

public class C_CycleListener implements LifecycleObserver{

	/**
	 * [사용 하는 클래스]
	 * 1) A_Main 액티비티 생명주기를 실시간으로 확인해 포그라운드 및 백그라운드 상태를 확인한다
	 */

	//========= 전역 변수 선언 =========
	Context context;

	//========= 클래스 생성자 초기화 =========
	public C_CycleListener(Context context){
		this.context = context;
	}

	//========= 자동으로 애플리케이션 생명주기 상태 체크 수행 =========
	@OnLifecycleEvent(Lifecycle.Event.ON_START)
	public void onMoveToFoground(){
		/**
		 * Moving to Foground
		 */
		Log.d("---","---");
		Log.i("//===========//","================================================");
		Log.d("//A_Main//", "[C_CycleListener() 컨트롤러]"+" ["+"액티비티 포그라운드 상태 확인"+"]");
		Log.i("//===========//","================================================");
		Log.d("---","---");
	}
	@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
	public void onMoveToBackground() {
		/**
		 * Moving to background
		 */
		Log.d("---","---");
		Log.e("//===========//","================================================");
		Log.d("//A_Main//", "[C_CycleListener() 컨트롤러]"+" ["+"액티비티 백그라운드 상태 확인"+"]");
		Log.e("//===========//","================================================");
		Log.d("---","---");
	}

} //클래스 종료

/* =========================== */

반응형
Comments