因應(yīng)用市場的要求,需要將targetSdkVersion的版本改變?yōu)?6+,所以才有了本篇文章的由來
一.權(quán)限問題
毋庸置疑,以前targetSdkVersion是22就是懶得處理權(quán)限導(dǎo)致的,應(yīng)了一句話,欠下的總要還的.
在這里插入圖片描述
舉個(gè)栗子,以前你想使用系統(tǒng)相機(jī)進(jìn)行拍照.代碼會是這樣的.
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);
升級之后的代碼應(yīng)該是這樣的.
/*
* 如果是6.0以上才去判斷是否需要判斷運(yùn)行時(shí)權(quán)限,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{
}
申請權(quán)限的回調(diào)
@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) {
//申請成功之后,跳轉(zhuǎn)拍照界面
}
}
break;
default:
break;
}
}
二. 應(yīng)用間共享文件問題.
Android 7.0 行為變更 通過FileProvider在應(yīng)用間共享文件
所以如果你還是使用剛才那個(gè)打開相機(jī)的代碼,你會發(fā)現(xiàn),就算有了權(quán)限,照樣crash.所以你還需要以下操作.
1. 如下圖所示.在res文件目錄下新建一個(gè)xml文件目錄,并且創(chuàng)建一個(gè)xml文件,文件內(nèi)容是自定義需要共享的文件夾.
image.png
2. AndroidManifest.xml 中配置如下代碼
<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>
3. 打開相機(jī)的代碼也要做出如下改變.
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);
}
三. 打開服務(wù)的問題.
1. Android8.0之后不能悄悄的打開后臺服務(wù)嘍,不然你的應(yīng)用同樣會crash,所以你需要添加以下代碼
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);
}
2. Android 8.0 還對特定函數(shù)做出了以下變更:
- 如果針對 Android 8.0 的應(yīng)用嘗試在不允許其創(chuàng)建后臺服務(wù)的情況下使用 startService() 函數(shù),則該函數(shù)將引發(fā)一個(gè) IllegalStateException。
- 新的 Context.startForegroundService() 函數(shù)將啟動一個(gè)前臺服務(wù)。現(xiàn)在,即使應(yīng)用在后臺運(yùn)行,系統(tǒng)也允許其調(diào)用 Context.startForegroundService()惨险。不過,應(yīng)用必須在創(chuàng)建服務(wù)后的五秒內(nèi)調(diào)用該服務(wù)的 startForeground() 函數(shù)脊髓。
四. 發(fā)送廣播問題.
你會發(fā)現(xiàn)在8.0上發(fā)送廣播接收不到了,尷尬不,意外不.<< Android8.0 靜態(tài)receiver接收不到隱式廣播>>
加上一行如下代碼就沒事了
intent.setPackage(getPackageName());