Android 零碎


//反射获取状态栏ID
private void initStatusBar() {
        if (statusBarView == null) {
            int identifier = getResources().getIdentifier("statusBarBackground", "id", "android");
            statusBarView = getWindow().findViewById(identifier);
        }
        if (statusBarView != null) {
            statusBarView.setBackgroundResource(R.drawable.home_toolbar_bg);
        }
    }


    private boolean hasStatusBar() {
        return true;
    }

1.语言切换

Resources resources = getResources();
Configuration configuration = resources.getConfiguration();
if (lang.equals("zh")) {
    configuration.setLocale(Locale.CHINESE);
    PreferenceUtils.setLang(this, "zh");
} else {
    configuration.setLocale(Locale.ENGLISH);
    PreferenceUtils.setLang(this, "en");
}
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
this.recreate();

2.版本versionName大小

 public static boolean isNewestApp(String localVersion, String netVersion) {
        if (localVersion.equals(netVersion)) {
            return true;
        }
        if (netVersion.equals("")) {
            return true;
        }
        String[] newVersion = netVersion.split("\\.");
        String[] oldVersion = localVersion.split("\\.");
        int length = oldVersion.length < newVersion.length ? newVersion.length : oldVersion.length;

        for (int i = 0; i < length; i++) {
            if (Integer.parseInt(newVersion[i]) > Integer.parseInt(oldVersion[i])) {
                return false;
            } else if (Integer.parseInt(newVersion[i]) < Integer.parseInt(oldVersion[i])) {
                return true;
            }
            // 相等 比较下一组值
        }
        return false;
    }

3.保存文件

public static String decryptedFile(Context context, String name) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        File file = null;
        File files = null;
        try {
            File dir = new File(AppConfig.getRootPath(context) + "temp/");
            if (!dir.exists()) {
                dir.mkdirs();
            }
            if (!TextUtils.isEmpty(name)) {
                file = new File(AppConfig.getRootPath(context), name);
                files = new File(dir, name);
                if (files.exists()) {
                    files.delete();
                    files.createNewFile();
                } else {
                    files.createNewFile();
                }
            }

            fis = new FileInputStream(file);
            byte[] buf = new byte[2048];
            int len = 0;
            int total = fis.available();

            fos = new FileOutputStream(files);
            int i = 0;
            while ((len = fis.read(buf)) != -1) {
                if (i == 0) {
                    fos.write(buf, 8, len - 8);
                } else {
                    fos.write(buf, 0, len);
                }
                i++;
            }
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) fis.close();
                if (fos != null) fos.close();
                return files.getAbsolutePath();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

4.TabLayout

tabIndicatorColor//选中下划线颜色
tabTextColor//字体颜色
tabSelectedTextColor//选中时文字颜色

5.状态栏透明

 if (Build.VERSION.SDK_INT >= 21) {
            View decorView = activity.getWindow().getDecorView();
            //SYSTEM_UI_FLAG_HIDE_NAVIGATION隐藏底部导航栏
            //SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 全屏
            //SYSTEM_UI_FLAG_LAYOUT_STABLE  让应用主题内容占用系统状态栏的空间
            //View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            int options = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(options);//设置系统UI的可见性
            //设置状态栏和导航栏颜色为透明
            activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
适配5.0以下
<item name="android:windowBackground">@drawable/tpv_window_background</item>

6.状态栏渐变色

 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
            @Override
            public boolean queueIdle() {
                if (hasStatusBar()) {
                    initStatusBar();
                    getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                        @Override
                        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                            initStatusBar();
                        }
                    });
                }
                return false;
            }
        });

    }

//背景代码xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="0"
        android:endColor="@color/home_toolbar_bg_end"
        android:startColor="@color/home_toolbar_bg_start" />
</shape>

7.权限

权限组权限权限说明
CALENDARREAD_CALENDAR读取日历
WRITE_CALENDAR修改日历
CAMERACAMERA相机
CONTACTSREAD_CONTACTS读取手机联系人
WRITE_CONTACTS写入联系人
GET_ACCOUNTS允许访问Gmail列表
LOCATIONACCESS_FINE_LOCATION允许通过移动网络或WIFI定位
ACCESS_COARSE_LOCATION允许通过移动网络或WIFI定位
MICROPHONERECORD_AUDIO录音或录制视频
PHONEREAD_PHONE_STATE允许访问电话状态
CALL_PHONE允许程序拨打电话
READ_CALL_LOG读取通话记录
WRITE_CALL_LOG保存用户的联系人数据
ADD_VOICEMAIL允许一个应用程序添加语音邮件系统
USE_SIP允许使用SIP视频服务
PROCESS_OUTGOING_CALLS允许程序拦截,监视或修改播出电话
SENSORSBODY_SENSORS使用的传感器来测量身体变化(心跳)
SMSSEND_SMS发送短信
READ_SMS读取短信
RECEIVE_SMS允许程序接收短信
RECEIVE_WAP_PUSH接收WAP PUSH信息
RECEIVE_MMS接收彩信
STORAGEREAD_EXTERNAL_STORAGE读取外部存储空间(有写权限后无需添加)
WRITE_EXTERNAL_STORAGE程序写入外部存储,如SD卡上写文件

 

8.EditText自动弹出软件盘

etVCode.requestFocus();

9.support 升级 androidx Glide 报错

错误: 程序包android.support.annotation不存在

解决方案:

annotationProcessor 'androidx.annotation:annotation:1.0.2'

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值