본문 바로가기

프로그래밍/안드로이드

안드로이드 권한 확인 소스코드(다른 앱 위에 그리기, 시스템을 변경할 수 있는 앱, 접근성 설정)



자바 : 

@Override
protected void onResume() {
super.onResume();

BitSet bitSet = new BitSet();

if ( checkDrawOverlayPermission(getApplicationContext()) ){
bitSet.set(0);
overlayPermissionButton.setText("완료");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
overlayPermissionButton.setBackgroundTintList(getResources().getColorStateList(R.color.colorPrimaryDark));
}
}
if ( checkWriteSettingPermission() ){
bitSet.set(1);
writeSettingPermissiononButton.setText("완료");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
writeSettingPermissiononButton.setBackgroundTintList(getResources().getColorStateList(R.color.colorPrimaryDark));
}
}
if( checkAccessibilityService(getApplicationContext()) ) {
bitSet.set(2);
accessibilityPermissionButton.setText("완료");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
accessibilityPermissionButton.setBackgroundTintList(getResources().getColorStateList(R.color.colorPrimaryDark));
}
}

if(bitSet.get(0) && bitSet.get(1) && bitSet.get(2)){
startActivity(new Intent(getApplicationContext(), Activity_Main.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_ANIMATION)
);
finish();
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity__permission);

if(getSupportActionBar() != null){
getSupportActionBar().setTitle("권한");
}

overlayPermissionButton = findViewById(R.id.button_overlay_permission);
writeSettingPermissiononButton = findViewById(R.id.button_write_setting_permission);
accessibilityPermissionButton = findViewById(R.id.button_accessibility_permission);


overlayPermissionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( ! overlayPermissionButton.getText().equals("완료") ){
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
startActivity(
new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
);
}
}
}
});

writeSettingPermissiononButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( ! writeSettingPermissiononButton.getText().equals("완료") ) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
startActivity(
new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS)
.setData(Uri.parse("package:" + getPackageName()))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
);
}
}
}
});

accessibilityPermissionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(
new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
);
}
});
}

//오버레이 권한 확인
//마시멜로 이상부터만 가능
public boolean checkDrawOverlayPermission(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.canDrawOverlays(context)) {
return true;
} else {
return false;
}
}
else{
return true;
}
}

//시스템을 변경 할 수 있는 권한 으로 넘겨주는 부분
//마시멜로 이상부터만 가능
public boolean checkWriteSettingPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.System.canWrite(getApplicationContext())) {
return true;
}
else{
return false;
}
}
else {
return true;
}
}

// 접근성 권한이 있는지 없는지 확인하는 부분
private boolean checkAccessibilityService(Context mContext) {
int accessibilityEnabled = 0;
final String service = getPackageName() + "/" + Service_AppListener.class.getCanonicalName();
try {
accessibilityEnabled = Settings.Secure.getInt(
mContext.getApplicationContext().getContentResolver(),
android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
// Log.e(TAG, "Error finding setting, default accessibility to not found: " + e.getMessage());
}
TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');

if (accessibilityEnabled == 1) {
String settingValue = Settings.Secure.getString(
mContext.getApplicationContext().getContentResolver(),
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);

if (settingValue != null) {
mStringColonSplitter.setString(settingValue);

while (mStringColonSplitter.hasNext()) {
String accessibilityService = mStringColonSplitter.next();

if (accessibilityService.equalsIgnoreCase(service)) {
return true;
}
}
}
}

return false;
}


xml : 

<?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=".Activity_Permission"
tools:ignore="HardcodedText"
>

<TextView
android:id="@+id/textView13"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text='1. "권한 허용하기" 버튼을 눌러 다른 앱 위에 표시되는 앱 권한을 허용해주쉐요 : )'
android:textColor="@color/textColorBlack"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>

<Button
android:id="@+id/button_overlay_permission"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="권한 허용하기"
android:textColor="#fff"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView13"
android:textStyle="bold"
/>

<TextView
android:id="@+id/textView15"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text='2. "권한 허용하기" 버튼을 눌러 시스템을 변경할 수 있는 앱 권한을 허용해주세요 : )'
android:textColor="@color/textColorBlack"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_overlay_permission" />

<Button
android:id="@+id/button_write_setting_permission"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="권한 허용하기"
android:textStyle="bold"
android:textColor="#fff"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView15" />

<TextView
android:id="@+id/textView16"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text='3. "접근성 설정" 버튼을 누르고 - "네비 자동실행"을 찾아서 클릭 후 - 권한을 활성화 시켜주세요 : )'
android:textColor="@color/textColorBlack"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_write_setting_permission" />

<Button
android:id="@+id/button_accessibility_permission"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="접근성 설정"
android:textColor="#fff"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView16" />
</android.support.constraint.ConstraintLayout>