========常用知识点===========
一,Activity相关
- 1,判断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; }
- 2,acitivity设置为singleTop或singleTask 模式时,相关文章跳本activity调整数据不改变问题
@Overrideprotected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); ARouter.getInstance().inject(this);}
二,View相关
- 1 判断当前view是否在前台展示,我们这里以recyclerView为例
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的时候可以做统计 */ } }); }
- 2,RecyclerView滚动到指定位置,并置顶
LinearSmoothScroller smoothScroller = new LinearSmoothScroller (ArticleDetailActivity.this) { @Override protected int getVerticalSnapPreference() { return LinearSmoothScroller.SNAP_TO_START; }//设置滑动1px所需时间@Overrideprotected 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; }
- 2,解决popupwindow在7.0以上机型设置居于view下方却盖在了view上面的问题
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编译相关
1, Error while Installing APK
解决方案:重新sync按钮编译下gradle就可以了
2,出现Execution failed for task类的错误
Error:Execution failed for task ‘:app:compileDebugJavaWithJavac’.
Compilation failed; see the compiler error output for details.
TaskExecutionException: Execution failed for task ‘:app:transformClassesWithAspectTransformForDebug’
解决方案:
首先找到Execution failed for task,然后取到后面的如上面红框里的信息
在命令行执行
./gradlew app:transformDexArchiveWithExternalLibsDexMergerForDebug –stacktrace –info
或者
./gradlew compileDebugJavaWithJavac –stackstrace
执行完成以后,搜索‘错误’就可以看到具体错误原因了
或者运行下面然后查看 Caused by的地方
./gradlew compileDebugSources –stacktrace -info
3 Could not find support-media-compat.aar
升级android studio到3.3版本,今天checkout到历史tag上运行android项目,死活报错
之前也有同事遇到过类似的问题: Could not find support-media-compat.aar
最后意外发现是google()仓库位置的问题
报错配置:
allprojects { repositories { flatDir { dirs 'libs' } jcenter() maven { url "https://jitpack.io" } maven { url "https://dl.bintray.com/thelasterstar/maven/" } maven { url "http://maven.aliyun.com/nexus/content/repositories/releases" } mavenCentral() google() } configurations.all { resolutionStrategy { force "com.android.support:appcompat-v7:$supportLibVersion" } }}
把google()放到第一位即可
allprojects { repositories { google() flatDir { dirs 'libs' } jcenter() maven { url "https://jitpack.io" } maven { url "https://dl.bintray.com/thelasterstar/maven/" } maven { url "http://maven.aliyun.com/nexus/content/repositories/releases" } mavenCentral() } configurations.all { resolutionStrategy { force "com.android.support:appcompat-v7:$supportLibVersion" } }}
4,could not find com.android.support:appconpat 如下图:
需要在project的build.gradle中allprojects 添加如下配置即可,添加下面代码到第一行。
maven { url “https://maven.google.com” }
5,报错:Annotation processors must be explicitly declared now.
在app的gradle文件中加入下面这句话:
android { ..... defaultConfig { ...... //在下面添加这句话,然后重新编译,就OK了。 javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } } }
持续更新中。。。
原著是一个有趣的人,若有侵权,请通知删除
评论前必须登录!
立即登录