본문 바로가기

Project/Lifehelper

Branch : 05_checkPermission

728x90

commit message : Checking Permission : ACCESS_FINE_LOCATION

 

 

경로 : app/src/main/AndroidManifest.xml

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

 

 

경로 : app/src/main/java/com/example/lifehelper/PresentationLayer/Activity/MainActivity.kt

//권한 체크 : https://dunkey2615.tistory.com/100
class MainActivity : AppCompatActivity() {

    @SuppressLint("ObsoleteSdkInt")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<ImageButton>(R.id.Lifehelper_logo)
            .setOnClickListener {

                if(android.os.Build.VERSION.SDK_INT >= 23) {
                    ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), Constants.REQUEST_CODE)
                }
            }
    }
    private fun permissionCheck() {
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED){
            //권한이 거절된 상태

            if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)){
                // 1. 사용자가 승인 거절을 누른 상태
                Toast.makeText(this, "거절 누름 상태 ${Constants.REQUEST_CODE}", Toast.LENGTH_SHORT).show()

                //ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), Constants.REQUEST_CODE)
                //권한 요청 메소드 onRequestPermissionsResult()

            }else{
                // 2. 사용자가 승인 거절 + 다시 표시하지 않기를 누른 경우
                // 3. 아직 승인 요청을 한 적이 없는 경우
                Toast.makeText(this, "거절+다시 보기 X 상태 ${Constants.REQUEST_CODE}", Toast.LENGTH_SHORT).show()

                //ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), Constants.REQUEST_CODE)
                //권한 요청 메소드 onRequestPermissionsResult()

                CustomSnackBar.make(findViewById<ImageButton>(R.id.Lifehelper_logo), getString(R.string.request_permission)).show()
            }
        }else{
            // 4. 권한이 승인된 상태
            Toast.makeText(this, "권한 승인 상태 ${Constants.REQUEST_CODE}", Toast.LENGTH_SHORT).show()

            val intent = Intent(this, HomeActivity::class.java)
            startActivity(intent)
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray )
    {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == Constants.REQUEST_CODE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "권한 승인", Toast.LENGTH_SHORT).show()
                permissionCheck()
            }
            else {
                Toast.makeText(this, "권한 거절", Toast.LENGTH_SHORT).show()
                permissionCheck()
            }
        }
    }
}

 

 

경로 : app/src/main/res/layout/snackbar_custom.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/customSnackBar_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:paddingStart="16dp"
        android:paddingEnd="16dp">

        <TextView
            android:id="@+id/tvSample"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:text="Hello World!"
            android:textColor="@color/black"
            android:textSize="15dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <!--
          <ImageView
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:src="@mipmap/ic_launcher"
             app:layout_constraintBottom_toBottomOf="parent"
             app:layout_constraintEnd_toStartOf="@id/btnSample"
             app:layout_constraintTop_toTopOf="parent" />
        -->

        <Button
            android:id="@+id/btnSample"
            style="?attr/materialButtonOutlinedStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="10dp"
            android:layout_marginBottom="10dp"
            android:padding="5dp"
            android:text="GO"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView> 

 

 

경로 : app/src/main/java/com/example/lifehelper/PresentationLayer/custom/CustomSnackBar.kt

class CustomSnackBar(view: View, private val messeage: String) {

    companion object {

        fun make(view: View, message: String) = CustomSnackBar(view, message)
    }

    private val context = view.context
    private val snackbar = Snackbar.make(view, "", 5000)
    private val snackbarLayout = snackbar.view as Snackbar.SnackbarLayout

    private val inflater = LayoutInflater.from(context)
    private var snackbarBinding: SnackbarCustomBinding =
        SnackbarCustomBinding.inflate(inflater)

    init {
        initView()
        initData()
    }

    private fun initView() {
        with(snackbarLayout) {
            removeAllViews()
            setPadding(0, 0, 0, 0)
            setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent))
            addView(snackbarBinding.root, 0)
        }
    }

    private fun initData() {
        snackbarBinding.tvSample.text = messeage
        snackbarBinding.btnSample.setOnClickListener {
            // 앱 권한 설정화면으로 이동
            val intent = Intent(
                Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                Uri.parse("package:"+context.packageName))
            startActivity(context, intent, null)
        }
    }

    fun show() {
        snackbar.show()
    }
} 

 

 

Custom SnackBar 참조 : https://black-jin0427.tistory.com/294

 

[Android, Custom Snackbar] 커스텀 스낵바 만들기

안녕하세요. 블랙진입니다. 지난 포스팅에 이어 스낵바를 커스텀 마이징 하는 법을 확인해보겠습니다. Custom Dialog Fragment 만들기 Custom Toast 만들기 Custom SnackBar 만들기 (현재) 스낵바 토스트와 마

black-jin0427.tistory.com