因應用市場的要求,須要將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());
複製代碼
持續更新中...也許還有不少問題須要去發現和解決..函數