Android适配8.0遇到的一些坑


96 
安卓的搬运工 
2018.05.10 17:11*  字数 733  阅读 918 评论 0

最近发现公司项目在华为应用市场直接提示不兼容8.0,想来8.0以后确实没有对项目进行响应适配升级,所以就大致整了下,首先必须的把SdkVersion提升到26把,然后google了一圈。

大致google+百度了一大圈,基本锁定会导致程序异常+功能失效的几个点。

①:通知(会收不到通知)

②:悬浮窗(7.0、8.0)(会崩溃)

③:自适应图标(图标终于有所关注)


一:通知渠道

Android O版本对通知做了规范性的控制,强制用户在发送通知的时候,对通知进行系统性的管理,新增了channel渠道功能,貌似Android P版本对与这一点也做了强调,使用户能够更好的管理通知,做到有目的性的屏蔽通知。下面上相关性的代码

NotificationChannel mChannel =new NotificationChannel("channel_01","消息推送", NotificationManager.IMPORTANCE_DEFAULT);

notificationManager.createNotificationChannel(mChannel);//创建一个通知渠道

其他和原设置一样即可。

二:悬浮窗

在一些广播中需要弹出对话框的应用场景,没有依附的activity,这个时候就可以用悬浮窗,相关适配以及源码有注释自行查看。

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)

private void showConflictDialog(final Context context,final String text) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;//Android O版本适配

    }else {

LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;

    }

View view = View.inflate(context, R.layout.item_post, null);

    view.setFocusableInTouchMode(true);

    view.measure(View.MeasureSpec.makeMeasureSpec(0,

            View.MeasureSpec.UNSPECIFIED), View.MeasureSpec

.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

    Button button = view.findViewById(R.id.main_dialog);

    button.setFocusable(true);

    TextView textView = view.findViewById(R.id.main_tv_jpush);

    textView.setText(text);

    params =new WindowManager.LayoutParams(

WindowManager.LayoutParams.WRAP_CONTENT,

            WindowManager.LayoutParams.WRAP_CONTENT,

            LAYOUT_FLAG,

            //WindowManager.LayoutParams控制悬浮窗特性,比如点击窗体外面是否取消悬浮窗

            WindowManager.LayoutParams.FLAG_TOUCHABLE_WHEN_WAKING | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LOCAL_FOCUS_MODE,

            PixelFormat.TRANSLUCENT);

    AlertDialog.Builder dialogBuilder =new AlertDialog.Builder(context);

    dialogBuilder.setTitle("提示");

    dialogBuilder.setView(view);

    dialogBuilder.setCancelable(false);

    final AlertDialog alertDialog = dialogBuilder.create();

    alertDialog.getWindow().setAttributes(params);

    if (!alertDialog.isShowing()){

alertDialog.show();

    }

button.setOnClickListener(new View.OnClickListener() {

@Override

        public void onClick(View v) {

if (alertDialog !=null &&alertDialog.isShowing()){

alertDialog.dismiss();

            }

isConflictDialogShow =false;

            conflictBuilder =null;

            Intent intent =new Intent(context, LoginActivity.class);

            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

            intent.putExtra("type", "exit");

            context.startActivity(intent);

        }

});

    alertDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {

        @Override

        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

                   if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() ==0){

                   if (alertDialog.isShowing()){

                       alertDialog.dismiss();

                     isConflictDialogShow =false;

                    conflictBuilder =null;

                    Intent intent =new Intent(context, LoginActivity.class);

                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

                    intent.putExtra("type", "exit");

                    context.startActivity(intent);

                }

}

return false;

        }

});

}

三:自适应图标 

Android O版本对图标做了修改,总体上来讲使Android APP从桌面就开始高大上起来了,加入了一些列动画,详细的可以看这篇博文。

Android O自适应图标

        另外:设计到一些需要访问清单文件,或者是pakageManager类的时候,适配8.0需要加入新的权限。

        <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

猜你喜欢

转载自blog.csdn.net/oneblue123/article/details/80842389