Android开发经验谈Android进阶之路Android技术知识

Android8.0适配——targetSdkVersion 2

2019-01-23  本文已影响8人  燊在锦官城
Android Oreo.jpg

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>

需要注意一下几点:

  1. exported:必须为false
  2. grantUriPermissions:true,表示授予 URI 临时访问权限。
  3. 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

        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));
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            //添加这一句表示对目标应用临时授权该Uri所代表的文件
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }

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 权限的应用无法再使用以下窗口类型来在其他应用和系统窗口上方显示提醒窗口:

也就是说需要在之前的基础上判断一下:

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以上版本必须写
上一篇下一篇

猜你喜欢

热点阅读