适配Android 8.0新特性权限(广播,下拉通知中心无法查看下载进度条,无法自动安装新apk)

本人开发更新新版本时候发现阿里上传compileSdkVersion要大于26,也就是安卓8.0才可以上传应用apk。于是将compileSdkVersion原来的25给成了26。但是测试过程中发现8.0以上的手机无法自动更新,下拉通知中心栏也没有进度条。广播也不能回调进度条。于是经过排查发现8.0意思的手机需要动态添加权限。

广播

8.0以下需要在AndroidManifest文件中进行静态的注册。8.0手机是接收不到的,需要动态注册。也就是说,不能在AndroidManifest文件对有些广播进行静态注册。

8.0以下的发送广播(需要在AndroidManifes添加以下代码)

 
            
                
            
Intent intent = new Intent("teprinciple.update");
intent.putExtra("progress", progress);
intent.putExtra("title", serverVersionName);
context.sendBroadcast(intent);

8.0发送的广播

Intent intent = new Intent("teprinciple.update");
ComponentName componentName = new ComponentName("", "");//参数1-包名 参数2-广播接收者所在的路径名
intent.putExtra("progress", progress);
intent.putExtra("title", serverVersionName);
intent.setComponent(componentName);
context.sendBroadcast(intent);

广播发送完整代码

 
            
                
            
    private static void send(Context context, int progress, String serverVersionName) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            Intent intent = new Intent("teprinciple.update");
            ComponentName componentName = new ComponentName("", "");//参数1-包名 参数2-广播接收者所在的路径名
            intent.putExtra("progress", progress);
            intent.putExtra("title", serverVersionName);
            intent.setComponent(componentName);
            context.sendBroadcast(intent);
        } else {
            Intent intent = new Intent("teprinciple.update");
            intent.putExtra("progress", progress);
            intent.putExtra("title", serverVersionName);
            context.sendBroadcast(intent);
        }

    }
获取是否有安装未知来源应用的权限
boolean haveInstallPermission = context.getPackageManager().canRequestPackageInstalls();

广播接收,apk自动安装权限,下拉通知栏完整代码

public class UpdateAppReceiver extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {

        int notifyId = 1;
        int progress = intent.getIntExtra("progress", 0);
        String title = intent.getStringExtra("title");

        NotificationManager nm = null;
        Notification.Builder builder = null;

        //创建通知渠道
        CharSequence name = HzqUtils.getAppName(context);
        String description = "正在下载 " + name + title;
        String channelId = "app";//渠道id

       //8.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_DEFAULT;//重要性级别
            NotificationChannel mChannel = new NotificationChannel(channelId, name, importance);
            mChannel.setDescription(description);//渠道描述
            mChannel.enableLights(true);//是否显示通知指示灯
            mChannel.enableVibration(false);//是否振动

            nm = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
            nm.createNotificationChannel(mChannel);//创建通知渠道
            builder = new Notification.Builder(context, channelId);
        } else {
            builder = new Notification.Builder(context);
        }

        builder.setContentTitle("正在下载 " + HzqUtils.getAppName(context) + title);
        builder.setSmallIcon(R.mipmap.app_logo);
        builder.setProgress(100, progress, false);

        nm = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        nm.notify(notifyId, builder.build());


        if (progress == 100) {
            if (nm != null)
                nm.cancel(notifyId);


            if (DownloadAppUtils.downloadUpdateApkFilePath != null) {
                //8.0
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    //先获取是否有安装未知来源应用的权限
                    boolean haveInstallPermission = context.getPackageManager().canRequestPackageInstalls();
                    Logger.d("onReceive---->:" + haveInstallPermission);


                    //有权限直接安装
                    if (haveInstallPermission) {
                        if (nm != null)
                            nm.cancel(1);
                        if (DownloadAppUtils.downloadUpdateApkFilePath != null) {
                            Intent i = new Intent(Intent.ACTION_VIEW);
                            File apkFile = new File(DownloadAppUtils.downloadUpdateApkFilePath);
                            if (UpdateAppUtils.needFitAndroidN && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                                i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                                Uri contentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", apkFile);
                                i.setDataAndType(contentUri, "application/vnd.android.package-archive");
                            } else {
                                i.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
                            }
                            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(i);
                        }

                    } else {
                        //没有权限跳转设置开启允许安装
                        Uri packageURI = Uri.parse("package:" + context.getPackageName());
                        Intent intent1 = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);
                        context.startActivity(intent1);
                    }

                } else {

                    Intent i = new Intent(Intent.ACTION_VIEW);
                    File apkFile = new File(DownloadAppUtils.downloadUpdateApkFilePath);
                    if (UpdateAppUtils.needFitAndroidN && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        Uri contentUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", apkFile);
                        i.setDataAndType(contentUri, "application/vnd.android.package-archive");
                    } else {
                        i.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
                    }
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(i);
                }
            }
        }
    }
}

 

你可能感兴趣的:(安卓,Java)