一,Activity相關(guān)
- 1,判斷activity是在前臺(tái)運(yùn)行,還是在后臺(tái)運(yùn)行
//當(dāng)前activity是否在前臺(tái)顯示
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", "前臺(tái)");
return true;
} else {
Log.i("qcl0403", "后臺(tái)");
}
}
return false;
}
- 2,acitivity設(shè)置為singleTop或singleTask 模式時(shí),相關(guān)文章跳本activity調(diào)整數(shù)據(jù)不改變問題
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
ARouter.getInstance().inject(this);
}
二,View相關(guān)
- 1 判斷當(dāng)前view是否在前臺(tái)展示,我們這里以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是我們窗口狀態(tài)改變時(shí)回傳回來的值
true: 我們的view在前臺(tái)展示
fasle: 熄屏,onpause,后臺(tái)時(shí)展示
我們?nèi)绻诿看蜗ㄆ恋搅疗烈菜阋淮纹毓獾脑挘沁@里為true的時(shí)候可以做統(tǒng)計(jì)
*/
}
});
}
- 2,RecyclerView滾動(dòng)到指定位置,并置頂
LinearSmoothScroller smoothScroller = new LinearSmoothScroller
(ArticleDetailActivity.this) {
@Override
protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}
//設(shè)置滑動(dòng)1px所需時(shí)間
@Override
protected float calculateSpeedPerPixel
(DisplayMetrics displayMetrics) {
//縮短每px的滑動(dòng)時(shí)間
float MILLISECONDS_PER_INCH = getResources().getDisplayMetrics()
.density * 0.03f;
return MILLISECONDS_PER_INCH / displayMetrics.density;
//返回滑動(dòng)一個(gè)pixel需要多少毫秒
}
};
smoothScroller.setTargetPosition(position);
virtualLayoutManager.startSmoothScroll(smoothScroller);
三,適配相關(guān)
-1,判斷手機(jī)是否有底部虛擬按鍵
/*
* 判斷手機(jī)是否有底部虛擬按鍵
* */
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以上機(jī)型設(shè)置居于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編譯相關(guān)
1, Error while Installing APK
解決方案:重新sync按鈕編譯下gradle就可以了
2,出現(xiàn)Execution failed for task類的錯(cuò)誤
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,然后取到后面的如上面紅框里的信息
在命令行執(zhí)行
./gradlew app:transformDexArchiveWithExternalLibsDexMergerForDebug --stacktrace --info
或者
./gradlew compileDebugJavaWithJavac --stackstrace
執(zhí)行完成以后,搜索‘錯(cuò)誤’就可以看到具體錯(cuò)誤原因了
或者運(yùn)行下面然后查看 Caused by的地方
./gradlew compileDebugSources --stacktrace -info
3 Could not find support-media-compat.aar
升級(jí)android studio到3.3版本,今天checkout到歷史tag上運(yùn)行android項(xiàng)目,死活報(bào)錯(cuò)
之前也有同事遇到過類似的問題: Could not find support-media-compat.aar
最后意外發(fā)現(xiàn)是google()倉庫位置的問題
報(bào)錯(cuò)配置:
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,報(bào)錯(cuò):Annotation processors must be explicitly declared now.
在app的gradle文件中加入下面這句話:
android {
.....
defaultConfig {
......
//在下面添加這句話,然后重新編譯,就OK了。
javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } }
}
持續(xù)更新中。。。
?
本文摘自 :https://blog.51cto.com/u