1. MODE_WORLD_READABLE 模式廢棄
Caused by: java.lang.SecurityException: MODE_WORLD_READABLE no longer supported
MODE_WORLD_READABLE
模式換成 MODE_PRIVATE
2. 獲取通話記錄
Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.CallLogProvider from ProcessRecord{8c75d80 31624:com.ct.client/u0a122} (pid=31624, uid=10122) requires android.permission.READ_CALL_LOG or android.permission.WRITE_CALL_LOG
針對android.permission.READ_CALL_LOG
or android.permission.WRITE_CALL_LOG
做權限適配
3. 圖片選擇和裁剪
Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/com.ct.client/files/com.ct.client/camere/1547090088847.jpg exposed beyond app through ClipData.Item.getUri()
第一步:
在AndroidManifest.xml清單文件中註冊provider
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.ct.client.fileProvider"
android:grantUriPermissions="true"
android:exported="false">
<!--元數據-->
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
需要注意一下幾點:
- exported:必須爲false
- grantUriPermissions:true,表示授予 URI 臨時訪問權限。
- authorities 組件標識,都以包名開頭,避免和其它應用發生衝突。
第二步:
指定共享文件的目錄,需要在res文件夾中新建xml目錄,並且創建file_paths
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<paths>
<external-path path="" name="download"/>
</paths>
</resources>
path=”“,是有特殊意義的,它代表根目錄,也就是說你可以向其它的應用共享根目錄及其子目錄下任何一個文件了。
第三步:
使用FileProvider
- 根據版本號把Uri改成使用FiliProvider創建的Uri,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
cameraFileUri = FileProvider.getUriForFile(mContext, "com.ct.client.fileProvider", new File(saveCamerePath, saveCameraFileName));
} else {
cameraFileUri = Uri.fromFile(new File(saveCamerePath, saveCameraFileName));
}
- 添加intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)來對目標應用臨時授權該Uri所代表的文件
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//添加這一句表示對目標應用臨時授權該Uri所代表的文件
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
- 在設置裁剪要保存的
intent.putExtra(MediaStore.EXTRA_OUTPUT, outUri);
的時候,這個outUri是要使用Uri.fromFile(file)生成的,而不是使用FileProvider.getUriForFile。
4. 獲取以content開頭的文件拿不到正確路徑
java.lang.IllegalArgumentException: column '_data' does not exist
拿到uri之後進行版本判斷大於等於24(即Android7.0)用最新的獲取路徑方式
String str ="";
if (Build.VERSION.SDK_INT >= 24) {
str = getFilePathFromURI(this, uri);//新的方式
} else {
str = getPath(this, uri);你自己之前的獲取方法
}
public String getFilePathFromURI(Context context, Uri contentUri) {
File rootDataDir = context.getFilesDir();
String fileName = getFileName(contentUri);
if (!TextUtils.isEmpty(fileName)) {
File copyFile = new File(rootDataDir + File.separator + fileName);
copyFile(context, contentUri, copyFile);
return copyFile.getAbsolutePath();
}
return null;
}
public static String getFileName(Uri uri) {
if (uri == null) return null;
String fileName = null;
String path = uri.getPath();
int cut = path.lastIndexOf('/');
if (cut != -1) {
fileName = path.substring(cut + 1);
}
return fileName;
}
public void copyFile(Context context, Uri srcUri, File dstFile) {
try {
InputStream inputStream = context.getContentResolver().openInputStream(srcUri);
if (inputStream == null) return;
OutputStream outputStream = new FileOutputStream(dstFile);
copyStream(inputStream, outputStream);
inputStream.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public int copyStream(InputStream input, OutputStream output) throws Exception, IOException {
final int BUFFER_SIZE = 1024 * 2;
byte[] buffer = new byte[BUFFER_SIZE];
BufferedInputStream in = new BufferedInputStream(input, BUFFER_SIZE);
BufferedOutputStream out = new BufferedOutputStream(output, BUFFER_SIZE);
int count = 0, n = 0;
try {
while ((n = in.read(buffer, 0, BUFFER_SIZE)) != -1) {
out.write(buffer, 0, n);
count += n;
}
out.flush();
} finally {
try {
out.close();
} catch (IOException e) {
}
try {
in.close();
} catch (IOException e) {
}
}
return count;
}
5. 7.0的手機安裝沒問題,但是在8.0上安裝,app沒有反應,一閃而過
- 增加新權限
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
- 把
Intent intent = new Intent(Intent.ACTION_VIEW)
改爲
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
6. 解析包安裝失敗。
安裝時把intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
這句話放在intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
前面。
7. 通知欄不顯示
在Application中創建渠道
@Override
protected void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "chat";
String channelName = "聊天消息";
int importance = NotificationManager.IMPORTANCE_HIGH;
createNotificationChannel(channelId, channelName, importance);
channelId = "subscribe";
channelName = "訂閱消息";
importance = NotificationManager.IMPORTANCE_DEFAULT;
createNotificationChannel(channelId, channelName, importance);
}
}
@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel(String channelId, String channelName, int importance) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
NotificationManager notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}
發送消息
public class MainActivity extends AppCompatActivity {
...
public void sendChatMsg(View view) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this, "chat")
.setContentTitle("收到一條聊天消息")
.setContentText("今天中午吃什麼?")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.icon)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon))
.setAutoCancel(true)
.build();
manager.notify(1, notification);
}
public void sendSubscribeMsg(View view) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this, "subscribe")
.setContentTitle("收到一條訂閱消息")
.setContentText("地鐵沿線30萬商鋪搶購中!")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.icon)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon))
.setAutoCancel(true)
.build();
manager.notify(2, notification);
}
}
8. 懸浮窗適配
使用 SYSTEM_ALERT_WINDOW 權限的應用無法再使用以下窗口類型來在其他應用和系統窗口上方顯示提醒窗口:
- TYPE_PHONE
- TYPE_PRIORITY_PHONE
- TYPE_SYSTEM_ALERT
- TYPE_SYSTEM_OVERLAY
- TYPE_SYSTEM_ERROR
相反,應用必須使用名爲 TYPE_APPLICATION_OVERLAY 的新窗口類型。
也就是說需要在之前的基礎上判斷一下:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mWindowParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
}else {
mWindowParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
}
需要新增權限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
9. 隱式廣播無法接收
隱式廣播例外
ACTION_LOCKED_BOOT_COMPLETED, ACTION_BOOT_COMPLETED
免除,因爲這些廣播僅在首次啓動時發送一次,並且許多應用需要接收此廣播以安排作業,警報等。
注意:WorkManager是一個新的API,目前採用alpha格式,允許您安排需要保證完成的後臺任務(無論應用程序進程是否存在)。WorkManager爲API 14+設備提供類似JobScheduler的功能,即使是那些沒有Google Play服務的設備。WorkManager是可查詢的(可觀察的),對工作圖表和流暢的API有很強的支持。如果您使用的是JobScheduler,FireBaseJobScheduler和/或AlarmManager以及BroadcastReceivers,則應考慮使用WorkManager。
10. BroadcastReceiver無法接收廣播
intent.setComponent(new ComponentName(mContext, AppWidgetProvider.class)); // 8.0以上版本必須寫