Android高仿網易雲音樂-啟動介面實現和動態許可權處理

效果

實現分析

基本上沒有什麼難點,就是布局,然後顯示用戶協議對話框,動態處理許可權,判斷是否顯示引導介面,是否顯示廣告介面等。

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="//schemas.android.com/apk/res/android"
    xmlns:tools="//schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".component.splash.activity.SplashActivity">

    <!--banner-->
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="120dp"
        android:src="@drawable/splash_banner" />

    <!--logo-->
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/copyright"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"
        android:src="@drawable/splash_logo" />

    <!--版權-->
    <TextView
        android:id="@+id/copyright"
        android:textColor="@color/black80"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="30dp"
        android:text="@string/copyright" />
</RelativeLayout>

是否顯示用戶協議對話框

if (DefaultPreferenceUtil.getInstance(getHostActivity()).isAcceptTermsServiceAgreement()) {
    //已經同意了用戶協議

    checkPermission();
} else {
    showTermsServiceAgreementDialog();
}

動態處理許可權

現在做Android必須要動態處理許可權,這裡使用第三方框架,推薦再用到的時候才動態請求許可權,寫法和現在一樣。

private void checkPermission() {
    //讓動態框架檢查是否授權了

    //如果不使用框架就使用系統提供的API檢查
    //它內部也是使用系統API檢查
    //只是使用框架就更簡單了
    SplashActivityPermissionsDispatcher.onPermissionGrantedWithPermissionCheck(this);
}

/**
 * 許可權授權了就會調用該方法
 * 請求相機許可權目的是掃描二維碼,拍照
 */
@NeedsPermission({
        Manifest.permission.CAMERA,
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.ACCESS_COARSE_LOCATION,
        Manifest.permission.ACCESS_FINE_LOCATION
})
void onPermissionGranted() {
    //如果有許可權就進入下一步
    prepareNext();
}

/**
 * 顯示許可權授權對話框
 * 目的是提示用戶
 */
@OnShowRationale({
        Manifest.permission.CAMERA,
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.ACCESS_COARSE_LOCATION,
        Manifest.permission.ACCESS_FINE_LOCATION
})
void showRequestPermission(PermissionRequest request) {
    new AlertDialog.Builder(getHostActivity())
            .setMessage(R.string.permission_hint)
            .setPositiveButton(R.string.allow, (dialog, which) -> request.proceed())
            .setNegativeButton(R.string.deny, (dialog, which) -> request.cancel()).show();
}

/**
 * 拒絕了許可權調用
 */
@OnPermissionDenied({
        Manifest.permission.CAMERA,
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.ACCESS_COARSE_LOCATION,
        Manifest.permission.ACCESS_FINE_LOCATION
})
void showDenied() {
    //退出應用
    finish();
}

/**
 * 再次獲取許可權的提示
 */
@OnNeverAskAgain({
        Manifest.permission.CAMERA,
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.ACCESS_COARSE_LOCATION,
        Manifest.permission.ACCESS_FINE_LOCATION
})
void showNeverAsk() {
    //繼續請求許可權
    checkPermission();
}


/**
 * 授權後回調
 *
 * @param requestCode
 * @param permissions
 * @param grantResults
 */
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    //將授權結果傳遞到框架
    SplashActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
}

介面跳轉

private void postNext() {
    binding.copyright.postDelayed(new Runnable() {
        @Override
        public void run() {
            next();
        }
    }, Config.SPLASH_DEFAULT_DELAY_TIME);

}

private void next() {
    //獲取意圖的目的是,因為啟動該介面的時候,可能攜帶一些數據
    //需要在主介面處理
    Intent intent = new Intent();

    Intent oldIntent = getIntent();
    IntentUtil.cloneIntent(oldIntent, intent);

//        if (sp.isLogin()) {
//            intent.setClass(getHostActivity(), AdActivity.class);
//        } else {
    intent.setClass(getHostActivity(), MainActivity.class);
//        }

    startActivity(intent);

    //關閉當前介面
    finish();

    //禁用啟動動畫
    overridePendingTransition(0, 0);
}