我正在使用此代码在android 11及更低版本的android 11中设置灯光状态栏,但不赞成弃用警告

问题描述

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Info {
    private String account;
    private String opportunity;
    private Integer value;
    private String desc;

    private Info(String account,String opportunity,Integer value,String desc) {
        super();
        this.account = account;
        this.opportunity = opportunity;
        this.value = value;
        this.desc = desc;
    }

    public static void main(String[] args) {
        List<Info> infos = new ArrayList<>();
        infos.add(new Info("12","absddd",4,"production"));
        infos.add(new Info("1234","abss",10,"vip"));
        infos.add(new Info("1234","abs","test"));
        infos.add(new Info("1234",5,"testing"));
        infos.add(new Info("123",8,"vip"));
        infos.add(new Info("12","absooo",2,"test"));
        Map<String,Map<String,List<Info>>> sortedResult = infos.stream().sorted(Info::compareByValueDesc)
                .collect(Collectors.groupingBy(Info::getAccount,LinkedHashMap::new,Collectors.groupingBy(r -> r.getOpportunity(),Collectors.toList())));
        sortedResult.forEach((key,value) -> System.out.println(key + value.toString()));
    }

    public static int compareByValueDesc(Info other1,Info other2) {
        return -other1.value.compareTo(other2.value);
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getOpportunity() {
        return opportunity;
    }

    public void setOpportunity(String opportunity) {
        this.opportunity = opportunity;
    }

    public String toString() {
        return this.value.toString();
    }
}

我正在使用此代码在android 11及更低版本的android 11中设置灯光状态栏。一切正常,只是有一个小问题,弃用警告不起作用。

解决方法

当您使用不赞成使用的代码而没有使用不赞成使用的代码时,会显示

不赞成使用警告-您有正确的if声明,导致在< 30上使用尚未弃用的方法,在30+上使用了新方法

,

我只是做了一件事,因为我的if语句很好,所以我只是通过使用禁止了警告 @SuppressWarnings(“弃用”)

我只是将此注释用于包含此代码的特定方法。

存在另一个也很好的选项: 只需添加

//noinspection deprecation

已弃用的代码行以上。这将允许检查整个功能中的其他警告。哪个很好,我会更喜欢。

与@SuppressWarnings(“ deprecation”)相比,//无检查弃用有什么优势?

这可以防止@SupressWarnings出现问题,因为它会忽略方法中的所有警告。因此,如果您不知道某些已弃用的内容,@ SupressWarnings会将其隐藏,并且不会收到警告。这就是// noinspection

的优势

测试并检查此帖子中的更多详细信息答案: How to suppress specific Lint warning for deprecated Android function?

,
if(fullScreen)
{
    getWindow().getDecorView().getWindowInsetsController().hide(WindowInsets.Type.statusBars());
    getWindow().getDecorView().getWindowInsetsController().setSystemBarsBehavior(BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
}
else
{
    getWindow().getDecorView().getWindowInsetsController().show(WindowInsets.Type.statusBars());
    getWindow().getDecorView().getWindowInsetsController().setSystemBarsBehavior(BEHAVIOR_SHOW_BARS_BY_SWIPE);
}

以上代码用于从 Android 11 开始全屏显示,设置 setSystemBarsBehavior(BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE) 很重要,否则在向下滑动状态栏时会显示并且在不重新创建活动之前不会消失。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...