이 예제에서는 안드로이드 모바일 기기가 동적 센서 발견(Dynamic Sensor Discovery)을 지원하는지 확인하는 방법을 알아봅니다.
1단계: 새 프로젝트 생성
Android Studio에서 새 프로젝트를 생성합니다. 상단 메뉴의 File → New Project로 이동한 후, 새 프로젝트 생성에 필요한 모든 세부 정보를 입력합니다.
2단계: 레이아웃 파일 작성
res/layout/activity_main.xml 파일에 아래 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/actionEvent"
android:textSize="40sp"
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>위 코드에서는 동적 센서 관련 정보를 화면에 표시하기 위해 TextView를 사용했습니다.
3단계: MainActivity 코드 작성
src/MainActivity.java 파일에 아래 코드를 추가합니다.
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.app.KeyguardManager;
import android.app.usage.UsageEvents;
import android.content.Context;
import android.hardware.SensorManager;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Build;
import android.os.Bundle;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
import android.support.annotation.RequiresApi;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.LogPrinter;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.logging.LogManager;
import javax.crypto.KeyGenerator;
public class MainActivity extends AppCompatActivity {
TextView textView;
private SensorManager sensorManager;
@SuppressLint({"RestrictedApi", "ClickableViewAccessibility"})
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView actionEvent = findViewById(R.id.actionEvent);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
actionEvent.setText(""+ sensorManager.isDynamicSensorDiscoverySupported());
}
}코드 설명
핵심 로직은 매우 간단합니다. onCreate() 메서드 내에서 시스템 서비스인 SensorManager 인스턴스를 가져온 후, isDynamicSensorDiscoverySupported() 메서드를 호출하여 해당 기기가 동적 센서 발견 기능을 지원하는지 여부를 boolean 값으로 반환받습니다. 그 결과값을 TextView에 표시하도록 설정했습니다.
참고로 이 API는 안드로이드 7.0 누가(API 24, Build.VERSION_CODES.N)부터 지원되므로, @RequiresApi(api = Build.VERSION_CODES.N) 어노테이션을 함께 사용했습니다.
앱 실행 및 결과 확인
이제 애플리케이션을 실행해 보겠습니다. 실제 안드로이드 모바일 기기가 컴퓨터와 연결되어 있다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 다음, 툴바에서 Run(실행) 아이콘을 클릭합니다. 옵션 목록에서 자신의 모바일 기기를 선택하면, 기기 화면에 아래와 같이 동적 센서 지원 여부가 표시됩니다.
