티스토리 뷰

android

EasyPermissions

구름나드리 2019. 1. 5. 19:03


EasyPermissions


implementation 'pub.devrel:easypermissions:1.3.+'





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



<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/btn_permission"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="request Permission"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>




public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {

public static final int REQUEST_CODE = 123;
Button btnOpenCamera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnOpenCamera = findViewById(R.id.btn_permission);
btnOpenCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openCamera();
}
});

}

@AfterPermissionGranted(REQUEST_CODE)
private void openCamera() {
String[] perms = {Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE};
if (EasyPermissions.hasPermissions(this, perms)) {
Toast.makeText(this, "Open Camera ", Toast.LENGTH_SHORT).show();
} else {
EasyPermissions.requestPermissions(this, "we need permission ", REQUEST_CODE, perms);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}

@Override
public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
Log.d("TAG", "why not call ");
}

@Override
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
if (EasyPermissions.somePermissionDenied(this, String.valueOf(perms))) {
new AppSettingsDialog.Builder(this).build().show();
Log.d("TAG", "onPermissionDenied if in ~");
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
Log.d("TAG", "onActivityResult if in method ~");
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {
Log.d("TAG", "onActivityResult if in ~");
}
}
}




'android' 카테고리의 다른 글

RecyclerView add,remove,delete  (0) 2018.12.03
imeOptions 사용  (0) 2018.10.25
HashMap 사용방법  (0) 2018.10.25
Custom AutoCompleteTextView  (0) 2018.10.23
spannable string  (0) 2018.10.23
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함