新手入门:目前为止最透彻的的Netty高性能原理和框架架构解析
分类:android
Android 判断网络连接是否可用
/** * 判断网络是否已连接 */ public static boolean isConnected(Context context) { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = cm.getActiveNetworkInfo(); if (info != null) { return info.isConnected(); } return false; }
/** * 使用ping判断网络连接 */ public static boolean ping() { try { Process process = Runtime.getRuntime().exec("ping -c 1 www.baidu.com"); int exitCode = process.waitFor(); return (exitCode == 0); } catch (Exception e) { e.printStackTrace(); } return false; }
Android Gradle 相关文章
ProductFlavors 简单使用
概述
我们在开发过程中,会经常遇到,同样的业务逻辑,需要配置不同的资源的情况。有时是不同的渠道,有时是不同的语言环境,各种不同。
如果我们在业务逻辑中,去实现这些差异化,会导致两个问题:
- 代码量很大,分支路径很多。而很多路径在实际使用中是用不到的,造成代码质量差。
- 用业务逻辑去区分情况加载资源,会导致包内需要包含大量的资源,造成包体积过大。
所以,我们需要在编译阶段,就区分种种情况,给予相应的资源。
Android中build.gradle
的productFlavors为我们提供了这样的能力。
快速使用
在module的build.gradle中,我们通过flavorDimensions和productFlavors的配合,来实现多维度的区分:
android { compileSdkVersion 27 defaultConfig { applicationId "com.example.oceanlong.buildtestpro" minSdkVersion 21 targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } ... flavorDimensions "country" productFlavors { china { applicationId "com.example.oceanlong.buildtestfree" dimension "country" } usa { applicationId "com.example.oceanlong.buildtestpro" dimension "country" } } ... }
adb logcat 命令行用法
adb logcat -v time -s TAG
Android Studio Gradle 设置 SOCKS5 代理
修改项目中的 gradle.properties 文件可以开启项目代理。
修改 .gradle/gradle.properties 文件可以开启全局代理。
org.gradle.jvmargs=-DsocksProxyHost=127.0.0.1 -DsocksProxyPort=1080
Android离线语音识别
【转】RxJava应用场景:使用zip操作符等待多个网络请求完成
转载自:RxJava应用场景:使用zip操作符等待多个网络请求完成
假设这样一种场景,我们利用github api开发一个app,在user界面,我既要请求user基本信息,又要列举user下的event数据,为此,我准备使用Retrofit来做网络请求,首先写好interfaces
public interface GitHubUser { @GET("users/{user}") Observable<JsonObject> getUser(@Path("user") String user); } public interface GitHubEvents { @GET("users/{user}/events") Observable<JsonArray> listEvents(@Path("user") String user); }
然后定义好我们的两个Observable:
Retrofit repo = new Retrofit.Builder() .baseUrl("https://api.github.com") .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build(); Observable<JsonObject> userObservable = repo .create(GitHubUser.class) .getUser(loginName) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()); Observable<JsonArray> eventsObservable = repo .create(GitHubEvents.class) .listEvents(loginName) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread());
分别是userObservable和eventsObservable,很显然的,我们将会需要两次请求。两次就两次嘛,但是这里有个问题。
虽然在后台有两次请求,但是在前台,我们希望用户打开这个页面,然后等待加载,然后显示。用户只有一次等待加载的过程。所以说,我们需要等待这两个请求都返回结果了,再开始显示数据。
怎么办?自己写判断两个都加载已完成的代码吗?逻辑好像也不是很复杂,但是代码看起来就没有那么高大上了啊。
其实既然你都用过了还有,那么直觉上你应该意识到也许RxJava可以解决这个问题。没错,就是RxJava,使用zip操作符。
zip( ):使用一个函数组合多个Observable发射的数据集合,然后再发射这个结果
第一次知道这个操作符是在大头鬼翻译的这篇文章中:深入浅出RxJava四-在Android中使用响应式编程 。
“Retrofit对Observable的支持使得它可以很简单的将多个REST请求结合起来。比如我们有一个请求是获取照片的,还有一个请求是获取元数据的,我们就可以将这两个请求并发的发出,并且等待两个结果都返回之后再做处理:
Observable.zip( service.getUserPhoto(id), service.getPhotoMetadata(id), (photo, metadata) -> createPhotoWithData(photo, metadata)) .subscribe(photoWithData -> showPhoto(photoWithData));
Android 系统配置读写
1、增加配置信息:
adb shell setprop [key] [value] adb shell setprop persist.isilent.me 123456
2、查看配置信息:
adb shell getprop [key] adb shell getprop persist.isilent.me
注意:必须采用persist.开头的属性名才能永久保存
参考链接:
RecyclerView设置最大高度、宽度
当RecyclerView属性设置为wrap_content
+maxHeight
时,maxHeight没有效果。
<android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" ... android:layout_height="wrap_content" android:maxHeight="300dp" ... />
查看源码时发现,当RecyclerView的LayoutManager#isAutoMeasureEnabled()返回true时,RecyclerView高度取决于children view的布局高度,并非取决于RecyclerView自身的测量高度。
@Override protected void onMeasure(int widthSpec, int heightSpec) { ... if (mLayout.mAutoMeasure) { ... // now we can get the width and height from the children. mLayout.setMeasuredDimensionFromChildren(widthSpec, heightSpec); if (mLayout.shouldMeasureTwice()) { ... // now we can get the width and height from the children. mLayout.setMeasuredDimensionFromChildren(widthSpec, heightSpec); } } else { ... } }
解决办法
因此,我们只需要重写LayoutManager的
public void setMeasuredDimension(Rect childrenBounds, int wSpec, int hSpec)
方法即可为RecyclerView设置最大宽高。
override fun setMeasuredDimension(childrenBounds: Rect, wSpec: Int, hSpec: Int) { super.setMeasuredDimension(childrenBounds, wSpec, View.MeasureSpec.makeMeasureSpec(maxHeight, AT_MOST)) }