安卓开发常用知识点Android开发积累开发常见问题及解决方案

安卓开发常用知识点& 安卓开发常见问题及解决方案

2019-04-03  本文已影响8人  编程小石头666

========常用知识点===========

一,Activity相关

//当前activity是否在前台显示
    private boolean isAPPforeground(final Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            if (topActivity.getClassName().equals(context.getClass().getName())) {
                Log.i("qcl0403", "前台");
                return true;
            } else {
                Log.i("qcl0403", "后台");
            }
        }
        return false;
    }
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    ARouter.getInstance().inject(this);
}

二,View相关

ViewTreeObserver treeObserver = recyclerView.getViewTreeObserver();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            treeObserver.addOnWindowFocusChangeListener(new ViewTreeObserver.OnWindowFocusChangeListener() {
                @Override
                public void onWindowFocusChanged(boolean hasFocus) {
                    /*
                    hasFocus是我们窗口状态改变时回传回来的值
                    true: 我们的view在前台展示
                    fasle: 熄屏,onpause,后台时展示 
                    我们如果在每次熄屏到亮屏也算一次曝光的话,那这里为true的时候可以做统计
                    */

                }
            });
        }
LinearSmoothScroller smoothScroller = new LinearSmoothScroller
        (ArticleDetailActivity.this) {
    @Override
    protected int getVerticalSnapPreference() {
        return LinearSmoothScroller.SNAP_TO_START;
    }
//设置滑动1px所需时间
@Override
protected float calculateSpeedPerPixel
(DisplayMetrics displayMetrics) {
    //缩短每px的滑动时间
    float MILLISECONDS_PER_INCH = getResources().getDisplayMetrics()
            .density * 0.03f;
    return MILLISECONDS_PER_INCH / displayMetrics.density;
    //返回滑动一个pixel需要多少毫秒
}
};
smoothScroller.setTargetPosition(position);
virtualLayoutManager.startSmoothScroll(smoothScroller);

三,适配相关

-1,判断手机是否有底部虚拟按键

   /*
    * 判断手机是否有底部虚拟按键
    * */
    public boolean checkDeviceHasNavigationBar(Context context) {
        boolean hasNavigationBar = false;
        Resources rs = context.getResources();
        int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
        if (id > 0) {
            hasNavigationBar = rs.getBoolean(id);
        }
        try {
            Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
            Method m = systemPropertiesClass.getMethod("get", String.class);
            String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
            if ("1".equals(navBarOverride)) {
                hasNavigationBar = false;
            } else if ("0".equals(navBarOverride)) {
                hasNavigationBar = true;
            }
        } catch (Exception e) {
        }
        return hasNavigationBar;
    }
  if (Build.VERSION.SDK_INT < 24) {
                popupWindow.showAsDropDown(v);
            } else {
                int[] location = new int[2];
                v.getLocationOnScreen(location);
                int x = location[0];
                int y = location[1];
                popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, 0, y + v.getHeight());
            }

======常见问题==================

一,androidstudio编译相关

image.png
android {
    .....
    defaultConfig {
        ......
    //在下面添加这句话,然后重新编译,就OK了。
        javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } }
    }

持续更新中。。。

上一篇 下一篇

猜你喜欢

热点阅读