Skip to content

Commit

Permalink
动态权限申请
Browse files Browse the repository at this point in the history
  • Loading branch information
ailiwean committed Jun 26, 2020
1 parent 7d9bbda commit 70d2dae
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "com.android.NBZxing"
minSdkVersion 26
targetSdkVersion 28
versionCode 3
versionName "1.2"
versionCode 4
versionName "1.3"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
Expand All @@ -28,7 +28,7 @@ android {
dependencies {
implementation project(path: ':module_camera')
api "androidx.appcompat:appcompat:1.1.0"
//implementation 'com.github.ailiwean:NBZxing:0.0.4'
// implementation 'com.github.ailiwean:NBZxing:0.0.7'
api 'com.google.zxing:core:3.3.3'

// implementation 'me.devilsen:czxing:1.0.11'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package com.google.android.cameraview

import android.Manifest
import android.app.Activity
import android.content.Context
import android.os.Handler
import android.content.pm.PackageManager
import android.os.Build
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import androidx.annotation.FloatRange
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.ailiwean.core.Config
import com.ailiwean.core.helper.ZoomHelper
import com.ailiwean.core.view.LifeOwner
import com.ailiwean.core.Utils
import com.ailiwean.core.WorkThreadServer
import com.ailiwean.core.helper.ZoomHelper
import com.ailiwean.core.view.LifeOwner

/**
* @Package: com.google.android.cameraview
Expand Down Expand Up @@ -87,8 +91,13 @@ abstract class BaseCameraView @JvmOverloads constructor(context: Context, attrib
appCompatActivity.lifecycle.addObserver(object : LifeOwner {
//在onCreate()中调用提升相机打开速度
override fun onCreate() {
WorkThreadServer.getInstance().bgHandle.post {
start()
if (checkPermission())
WorkThreadServer.getInstance().bgHandle.post {
start()
}
else {
isSingleLoad = true
requstPermission()
}
}

Expand All @@ -100,7 +109,8 @@ abstract class BaseCameraView @JvmOverloads constructor(context: Context, attrib
isSingleLoad = true
return
}
start()
if (checkPermission())
start()
}

override fun onPause() {
Expand Down Expand Up @@ -155,4 +165,18 @@ abstract class BaseCameraView @JvmOverloads constructor(context: Context, attrib
override fun onDestroy() {
}

fun checkPermission(): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
context.checkSelfPermission(
Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED
} else {
return true
}
}

fun requstPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
(context as? Activity)?.requestPermissions(arrayOf(Manifest.permission.CAMERA), 200)
}
}
}

0 comments on commit 70d2dae

Please sign in to comment.