Dangerous类权限:需要运行时去申请
easypermission
1.添加依赖项
compile 'pub.devrel:easypermissions:0.2.0'
2.Splash页面 进行权限检测(核心代码)
package com.thomas.dagger_retrofit_rxjava.view.activity;
import android.Manifest;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.View;
import com.orhanobut.logger.Logger;
import com.thomas.dagger_retrofit_rxjava.R;
import com.thomas.dagger_retrofit_rxjava.app.BaseApplication;
import com.thomas.dagger_retrofit_rxjava.di.components.DaggerSplashComponent;
import com.thomas.dagger_retrofit_rxjava.di.modules.SplashModule;
import com.thomas.dagger_retrofit_rxjava.presenter.SplashContract;
import com.thomas.dagger_retrofit_rxjava.presenter.SplashPresenter;
import com.thomas.dagger_retrofit_rxjava.util.AppUtil;
import com.thomas.dagger_retrofit_rxjava.util.FileUtil;
import com.thomas.dagger_retrofit_rxjava.util.PreferenceUtils;
import com.thomas.dagger_retrofit_rxjava.view.MainActivity;
import com.thomas.dagger_retrofit_rxjava.widget.FixedImageView;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Random;
import javax.inject.Inject;
import pub.devrel.easypermissions.EasyPermissions;
/**
* @author 涂文远
* @version $Rev$
* @time 2016/12/5 15:19
* @des ${TODO}
* @updateAuthor $Author$
* @updateDate $Date$
* @updateDes ${TODO}
*/
public class SplashActivity extends BaseActivity implements SplashContract.View,EasyPermissions.PermissionCallbacks {
FixedImageView splashImg;
@Inject
SplashPresenter presenter;
private static final int PERMISSON_REQUESTCODE = 1;
/**
* 需要进行检测的权限数组
*/
protected String[] needPermissions = {
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.READ_PHONE_STATE
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DaggerSplashComponent.builder()
.netComponent(BaseApplication.get(this).getNetComponent())
.splashModule(new SplashModule(this))
.build().inject(this);
//presenter = new SplashPresenter(this);
initStatus();
}
private void initStatus() {
if (Build.VERSION.SDK_INT >= 21) {
View decorView = getWindow().getDecorView();
int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(option);
getWindow().setStatusBarColor(Color.TRANSPARENT);
}
}
@Override
protected void onStart() {
super.onStart();
requestCodePermissions();
}
/**
* 申请权限结果的回调方法
*/
@Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions, int[] paramArrayOfInt) {
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, paramArrayOfInt, this);
}
private void requestCodePermissions() {
if (!EasyPermissions.hasPermissions(this, needPermissions)) {
EasyPermissions.requestPermissions(this, "应用需要这些权限", PERMISSON_REQUESTCODE, needPermissions);
} else {
setContentView(R.layout.activity_splash);
initView();
delaySplash();
String deviceId = AppUtil.getDeviceId(this);
presenter.getSplash(deviceId);
}
}
private void initView() {
splashImg = (FixedImageView) findViewById(R.id.splash_img);
}
private void showMissingPermissionDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示");
builder.setMessage("当前应用缺少必要权限。请点击\"设置\"-\"权限\"-打开所需权限。");
// 拒绝, 退出应用
builder.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.setPositiveButton("设置",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startAppSettings();
}
});
builder.setCancelable(false);
builder.show();
}
/**
* 启动应用的设置
*/
private void startAppSettings() {
Intent intent = new Intent(
Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
}
@Override
public void onPermissionsGranted(int requestCode, List perms) {
recreate();
}
@Override
public void onPermissionsDenied(int requestCode, List perms) {
showMissingPermissionDialog();
}
private void delaySplash() {
List picList = FileUtil.getAllAD();
if (picList.size() > 0) {
Random random = new Random();
int index = random.nextInt(picList.size());
int imgIndex = PreferenceUtils.getPrefInt(this, "splash_img_index", 0);
Logger.i("当前的imgIndex=" + imgIndex);
if (index == imgIndex) {
if (index >= picList.size()) {
index--;
} else if (imgIndex == 0) {
if (index + 1 < picList.size()) {
index++;
}
}
}
PreferenceUtils.setPrefInt(this, "splash_img_index", index);
Logger.i("当前的picList.size=" + picList.size() + ",index = " + index);
File file = new File(picList.get(index));
try {
InputStream fis = new FileInputStream(file);
splashImg.setImageDrawable(InputStream2Drawable(fis));
animWelcomeImage();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
}
} else {
try {
AssetManager assetManager = this.getAssets();
InputStream in = assetManager.open("welcome_default.jpg");
splashImg.setImageDrawable(InputStream2Drawable(in));
animWelcomeImage();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public Drawable InputStream2Drawable(InputStream is) {
Drawable drawable = BitmapDrawable.createFromStream(is, "splashImg");
return drawable;
}
private void animWelcomeImage() {
ObjectAnimator animator = ObjectAnimator.ofFloat(splashImg, "translationX", -100F);
animator.setDuration(1500L).start();
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
}