因应用市场的要求,须要将targetSdkVersion的版本改变为26+,因此才有了本篇文章的由来android
毋庸置疑,之前targetSdkVersion是22就是懒得处理权限致使的,应了一句话,欠下的总要还的.bash
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);
复制代码
/*
* 若是是6.0以上才去判断是否须要判断运行时权限,6.0如下不考虑
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION);
return;
}
}else{
}
复制代码
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case CAMERA_PERMISSION:
if (permissions[0].equals(Manifest.permission.CAMERA)) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//申请成功以后,跳转拍照界面
}
}
break;
default:
break;
}
}
复制代码
因此若是你仍是使用刚才那个打开相机的代码,你会发现,就算有了权限,照样crash.因此你还须要如下操做.app
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
复制代码
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
/*
* 指定拍照存储路径
* 7.0 及其以上使用FileProvider替换'file://'访问
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//这里的BuildConfig,须要是程序包下BuildConfig。
intent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", photoFile));
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
} else {
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
}
startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);
}
复制代码
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int NOTIFICATION_ID = (int) (System.currentTimeMillis()%10000);
NotificationChannel channel = new NotificationChannel("hh","name", NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
Notification notification = new Notification.Builder(getApplicationContext(),"hh").build();
startForeground(NOTIFICATION_ID,notification);
}
复制代码
你会发如今8.0上发送广播接收不到了,尴尬不,意外不.<< Android8.0 静态receiver接收不到隐式广播>>ide
intent.setPackage(getPackageName());
复制代码
持续更新中...也许还有不少问题须要去发现和解决..函数