Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Android 10 (Q, API 29) 부터는 신체 활동 감지 권한이 필요하게 변경됐습니다. 앱에서 PedometerLight feature 를 활성화할 때 권한을 얻어야 합니다.

https://developer.android.com/about/versions/10/privacy/changes#physical-activity-recognition

(참고: Android Developer Guide)

1. showPop 을 호출하기 전에 requestActivityRecognitionPermissionIfNeeded 함수를 통해 신체 활동 감지 권한이 있는 지 확인하고 없으면 권한을 요청

Code Block
private boolean requestActivityRecognitionPermissionIfNeeded(Activity activity) {
    if (activity != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACTIVITY_RECOGNITION) != PackageManager.PERMISSION_GRANTED) {
            requestActivityRecognitionPermission(activity);
            return true;
        }
    }
    return false;
}

@TargetApi(Build.VERSION_CODES.Q)
private void requestActivityRecognitionPermission(Activity activity) {
    if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.ACTIVITY_RECOGNITION)) {
        if (!activity.isFinishing()) {
            new AlertDialog.Builder(activity)
                    .setTitle(R.string.pop_pedometer_dialog_permission_title)
                    .setMessage(R.string.pop_pedometer_dialog_permission_description)
                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            requestPermissions(new String[]{Manifest.permission.ACTIVITY_RECOGNITION}, ACTIVATION_REQUEST_CODE);
                        }
                    }).show();
        }
    } else {
        requestPermissions(new String[]{Manifest.permission.ACTIVITY_RECOGNITION}, ACTIVATION_REQUEST_CODE);
    }
}

2. 권한이 획득 되면 onRequestPermissionsResult 를 통해서 결과를 받고 다음 step(overlay permission 획득) 진행

Code Block
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case ACTIVATION_REQUEST_CODE: {
            if ((grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                showPopOrRequestOverlayPermissionIfNeeded();
            } else {
                pedometerPopSwitch.setChecked(false);
            }
        }
    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

3. Overlay 권한 획득

Code Block
private void showPopOrRequestOverlayPermissionIfNeeded() {
    if (BuzzAdPop.hasPermission(MainActivity.this) || Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        showPop();
    } else {
        BuzzAdPop.requestPermissionWithDialog(MainActivity.this,
                new PopOverlayPermissionConfig.Builder(R.string.pop_name)
                        .settingsIntent(OverlayPermission.createIntentToRequestOverlayPermission(MainActivity.this))
                        .requestCode(REQUEST_CODE_SHOW_POP)
                        .build()
        );
    }
}

4. 위 스탭들을 통해서 신체 감지 권한 및 OverlayPermission 권한을 획득했다면 Activity.onCreate 에 아래 코드를 넣어 주면 activatePedometerPop 을 자동으로 호출

Code Block
// Activity.onCreate
if (getIntent().getBooleanExtra(KEY_SETTINGS_RESULT, false)
        && getIntent().getIntExtra(KEY_SETTINGS_REQUEST_CODE, 0) == REQUEST_CODE_SHOW_POP) {
    if (BuzzAdPop.hasPermission(this)) {
        buzzAdPop.showTutorialPopup(this);
        // overlay permission granted
        // collect event here if necessary
        activatePedometerPop();
    }
}

private void activatePedometerPop() {
    BuzzAdPopPedometer.activate();
}

Step 5. Final Step

App 실행 → PedometerPop 활성화 → 신체 감지 권한 획득 → OverlayPermissoin 획득 → BuzzAdPopPedometer.activate() → showPop → PedometerPop 기능을 사용할 수 있습니다.