跳至主要內容

Android 阅读笔记

JI,XIAOYONG...大约 2 分钟

layout_weight

layout_weight 重要性,默认的是 0,0 等级最高,要显示,数字越大重要性越低。

例:a,b 的宽度为 0,layout_weight 分别为 1、2,则 a,b 宽度分别为父容器的 2/3、1/3。

PendingIntent

PendingIntent 是封装后的 intent,有 intent 执行所需的 context,所以即使要执行 intent 的 activity 已经消失或者还没生成,其他 activity 依然可以通过 PendingIntent 执行 intent。

PendingIntent is a description of an Intent and target action to perform with it. Instances of this class are created with getActivity(Context, int, Intent, int), getActivities(Context, int, Intent[], int), getBroadcast(Context, int, Intent, int), and getService(Context, int, Intent, int); the returned object can be handed to other applications so that they can perform the action you described on your behalf at a later time.

也就是把自己要执行的 intent 和执行所需的 context 封装后给别人,请别人在适当的时候执行。

android 模拟器访问电脑 localhost

电脑localhost或者127.0.0.1访问本地网址。

模拟器访问localhost会默认访问手机的本地网址,要访问电脑的本地网址则需要访问10.0.2.2:8080,记得加上对应的端口。

获取屏幕画面

View decor = MainActivity.this.getWindow().getDecorView();
decor.setDrawingCacheEnabled(true);
imageView.setImageBitmap(decor.getDrawingCache());

获取网络信息,请求网络

需要请求权限

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

java 代码如下:

    private void chackNetWork(Context context) {
        boolean isNetAvailable = false;
        ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        if (manager.getActiveNetworkInfo() != null) {
            isNetAvailable = manager.getActiveNetworkInfo().isAvailable();
        }
        if (!isNetAvailable) {
            Toast.makeText(this, "open network", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_NETWORK_OPERATOR_SETTINGS);
            context.startActivity(intent);
        }
    }

LiveData

LiveData可以在数据有变化的时候调用订阅者并执行指定方法。

Transformations有两个转化LiveData的方法:map()switchMap()

map()可以将一个LiveData经过处理转化为另外一个LiveData

switchMap()则可以根据不同的需要切换不同的LiveData

val live = MutableLiveData<String>()
val d : LiveData<String> = map(live){
  "this is $it"
}
val e = switchMap(live) {
    return@switchMap when (it) {
        "a" -> MutableLiveData<String>("a")
        "b" -> MutableLiveData("b")
        else -> MutableLiveData("else")
    }
}

此外,LiveData的子类MediatorLiveData可以添加多个监听项,每个项目改变都会回调对应的onChange()方法。

val mediatorLiveData = MediatorLiveData<String>()

mediatorLiveData.addSource(d) {
    update(it, e.value)
}

mediatorLiveData.addSource(e) {
    update(d.value, it)
}

fun update(a: String? = "", b: String? = "") {
    //do sth
}

参考资料

【译】LiveData 使用详解open in new window

文章标题:《Android 阅读笔记》
本文作者: JI,XIAOYONG
发布时间: 2018/02/22 19:29:48 UTC+8
更新时间: 2023/12/30 16:17:02 UTC+8
written by human, not by AI
本文地址: https://jixiaoyong.github.io/blog/posts/aacd75ab.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 许可协议。转载请注明出处!
你认为这篇文章怎么样?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.8