android 学习中遇到的知识点(杂)

1. 用xml 合成图片

ic_launcher.xml 作用:将两个图片组合成一个图片,一个背景图,一个icon

<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
    <background android:drawable="@mipmap/ic_launcher_background"/>
    <foreground android:drawable="@mipmap/ic_launcher_foreground"/>
</adaptive-icon>

------生成了

 

2. Android中的app忽略电池优化

集成环信SDK时,进入页面,老是会弹出一个“要忽略电池优化吗?”的提示框,如果要去掉,把下列代码删除即可,代码如下:

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            String packageName = getPackageName();
            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            if (!pm.isIgnoringBatteryOptimizations(packageName)) {
                try {
                    //some device doesn't has activity to handle this intent
                    //so add try catch
                    Intent intent = new Intent();
                    intent.setAction(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                    intent.setData(Uri.parse("package:" + packageName));
                    startActivity(intent);
                } catch (Exception e) {
                }
            }
        }

这个功能在manifest文件中配置了权限:

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

3. android9.0新特性:

1)启动前台服务

Android在8.0限制了后台服务这些,启动后台服务需要设置通知栏,使服务变成前台服务。但是在9.0上,就会出现Permission Denial: startForeground requires android.permission.FOREGROUND_SERVICE

解决办法是在AndroidManifest中添加

    <!--android 9.0上使用前台服务,需要添加权限-->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

之后启动service的时候也需要做处理,否则会抛异常java.lang.IllegalStateException: Not allowed to start service Intent

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(intent);
        } else {
            context.startService(intent);
        }

(2)Android 9.0强制使用https

Android 9.0强制使用https,会阻塞http请求,如果app使用的第三方sdk有http,将全部被阻塞。
出现UnknownServiceException: CLEARTEXT communication to localhost not permitted by network security policy或者IOException java.io.IOException: Cleartext HTTP traffic to * not permitted

解决方法:

AndroidManifest文件的application设置android:usesCleartextTraffic="true"

参考文献:Android App兼容8.0和9.0

 

4. 拨打电话:

跳转到拨号界面:

    public void callPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        getContext().startActivity(intent);
    }

直接拨打电话:

public void callAction(String telPhone) {
    Intent intent = new Intent(Intent.ACTION_CALL);
    Uri data = Uri.parse("tel:" + telPhone);
    intent.setData(data);
    getContext().startActivity(intent);
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值