实习笔记-1

px dp sp 的区别

px 其实就是像素单位,比如我们通常说的手机分辨列表800*400都是px的单位
sp 同dp相似,还会根据用户的字体大小偏好来缩放
dp 虚拟像素,在不同的像素密度的设备上会自动适配

隐藏状态栏任务栏

在api30之前

1
getWindow().getDecorView().setSystemUIVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN)
阅读更多

实习笔记-4

ImageView的参数

1
android:adjustViewBounds="true"

对应源码

1
2
3
4
5
6
7
@android.view.RemotableViewMethod
public void setAdjustViewBounds(boolean adjustViewBounds) {
mAdjustViewBounds = adjustViewBounds;
if (adjustViewBounds) {
setScaleType(ScaleType.FIT_CENTER);
}
}

应用场景

  • 当宽高有且仅有一个设置为wrapContent的时候是有用,指定一个宽or高,再根据drawable的比例确定另一个高/宽的值
阅读更多

实习笔记-5

git相关知识

新建代码库

1
2
3
4
5
6
7
8
## 当前目录 新建一个Git代码库
$ git init

## 新建一个目录,将其初始化为Git代码库
$ git init [project-name]

## 下载一个项目和它的整个代码历史
$ git clone [url]

配置

1
2
3
4
5
6
7
8
9
## 显示当前的Git配置
$ git config --list

## 编辑Git配置文件
$ git config -e [--global]

## 设置提交代码时的用户信息
$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"
阅读更多

实习笔记-7

FragmentActivity和Activity的具体区别在哪里

fragment是3.0以后的东西,为了在低版本中使用fragment就要用到android-support-v4.jar兼容包,fragmentActivity提供了操作fragment的一些方法,其功能跟3.0及以后的版本的Activity的功能一样。

  • 1、fragmentactivity 继承自activity,用来解决android3.0 之前没有fragment的api,所以在使用的时候需要导入support包,同时继承fragmentActivity,这样在activity中就能嵌入fragment来实现你想要的布局效果。
  • 2、当然3.0之后你就可以直接继承自Activity,并且在其中嵌入使用fragment了。
  • 3、获得Manager的方式也不同
    • 3.0以下:getSupportFragmentManager()
    • 3.0以上:getFragmentManager()(已弃用)

activity 转场动画

使用windowAnimation和ActivityAnimation

阅读更多

实习笔记-8

组件merge

类名.()的写法

1
2
3
4
5
6
7
8
9
inline fun AppCompatActivity.fragmentTransaction(block: FragmentTransaction.() -> Unit): Boolean {
kotlin.runCatching {
val t = supportFragmentManager.beginTransaction()
block.invoke(t)
t.commitNowAllowingStateLoss()
return true
}
return false
}
1
2
3
4
/* 新的协程job */
fun newWorkerThreadCoroutineJob(block: suspend CoroutineScope.() -> Unit): Job {
return GlobalScope.launch(context = Dispatchers.IO, block = block)
}

android-job庫

阅读更多

实习笔记-10

读取软件

  • 声明权限
1
2
3
4
5
6
7
8
9
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<!-- 对于安卓11开始 -->
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
<queries>
<intent>
<action android:name="android.intent.action.MAIN" />
</intent>
</queries>
1
2
3
4
5
val pm = context.applicationContext.packageManager
val installedApplications = pm.getInstalledApplications(0)
installedApplications.forEach {info ->
//handle info
}
  • 该操作比较耗时,在新线程或协程job中执行

获取应用Label(应用名) , 应用图标和应用安装时间

阅读更多

实习笔记-11

Intent Action相关

chooser

可自定义标题,弹出软件选择器

1
2
3
4
5
6
7
8
9
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
Intent intent2 = new Intent();
intent2.setAction(Intent.ACTION_CHOOSER);
intent2.putExtra(Intent.EXTRA_TITLE, "please selete a app");
//extra intent
intent2.putExtra(Intent.EXTRA_INTENT, intent);
startActivity(intent2);

方便起见,可以使用

1
Intent.createChooser(Intent,CharSequence)
阅读更多

实习笔记-12

PendingIntent认识

  • PendIntent其实是Intent的封装
  • 不是立刻执行某个行为,而是满足某些条件或触发某些事件后才执行指定的行为
  • 我们的 Activity 如果设置了 exported = false其他应用如果使用 Intent 就访问不到这个 Activity,但是使用 PendingIntent 是可以的。
  • 即:PendingIntent将某个动作的触发时机交给其他应用;让那个应用代表自己去执行那个动作(权限都给他)

获取PendingIntent

1
2
3
4
5
getActivity()
getActivities()
getBroadcast()
getService()
getForegroundService()
1
2
3
4
5
参数:
Context - 上下文对象
requestCode - 请求码
Intent - 请求意图用以指明启动类及数据传递
flags -关键标志位
flags
FLAG_CANCEL_CURRENT 先将当前已有的PendingIntent取消,然后重新生成一个PendingIntent对象。
FLAG_NO_CREATE 如果当前系统中不存在相同的PendingIntent对象,系统将返回null,否则返回已有对象
FLAG_ONE_SHOT 该PendingIntent只作用一次。在该PendingIntent对象通过send()方法触发过后,PendingIntent将自动调用cancel()进行销毁,那么如果你再调用send()方法的话,系统将会返回一个SendIntentException。
FLAG_UPDATE_CURRENT 更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras
FLAG_IMMUTABLE 创建的PendingIntent是不可变的,使用send方法发送的附加Intent会被忽略
阅读更多