实习笔记-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(应用名) , 应用图标和应用安装时间

1
2
3
info.loadIcon(pm)
pm.getApplicationLabel(info)
pm.getPackageInfo(name, 0).firstInstallTime
阅读更多

实习笔记-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)
阅读更多

实习笔记-17

协调问题

  1. outsideLauncher

安卓中的单例模式

  • LayoutInflator也是一个SystemService
  • SYSTEM_SERVICE_FETCHERS是一个hashMap,保存ServiceName->ServiceFetcher的单例
  • ServiceFetctor是一个接口,定义了 T getService(ContextImpl ctx);

安卓中的Builder模式

  • 隔离getter,setter,在对象生成时对成员变量配置,生成后屏蔽
  • dialog
阅读更多

实习笔记-20

App真正的入口

ActivityThread 中的main方法,一个应用程序对应一个ActivityThread对象,Zygote孵化出一个进程后,就会执行main方法

  • 准备Looper和消息队列
  • thread.attach()方法绑定到ActivityManagerService中

attach方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
private void attach(boolean system, long startSeq) {
sCurrentActivityThread = this;
mConfigurationController = new ConfigurationController(this);
mSystemThread = system;
if (!system) {
android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
UserHandle.myUserId());
RuntimeInit.setApplicationObject(mAppThread.asBinder());
final IActivityManager mgr = ActivityManager.getService();
try {
mgr.attachApplication(mAppThread, startSeq);
//
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
// Watch for getting close to heap limit.
BinderInternal.addGcWatcher(new Runnable() {
@Override public void run() {
if (!mSomeActivitiesChanged) {
return;
}
Runtime runtime = Runtime.getRuntime();
long dalvikMax = runtime.maxMemory();
long dalvikUsed = runtime.totalMemory() - runtime.freeMemory();
if (dalvikUsed > ((3*dalvikMax)/4)) {
if (DEBUG_MEMORY_TRIM) Slog.d(TAG, "Dalvik max=" + (dalvikMax/1024)
+ " total=" + (runtime.totalMemory()/1024)
+ " used=" + (dalvikUsed/1024));
mSomeActivitiesChanged = false;
try {
ActivityTaskManager.getService().releaseSomeActivities(mAppThread);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
}
});
} else {
// Don't set application object here -- if the system crashes,
// we can't display an alert, we just want to die die die.
android.ddm.DdmHandleAppName.setAppName("system_process",
UserHandle.myUserId());
try {
mInstrumentation = new Instrumentation();
mInstrumentation.basicInit(this);
ContextImpl context = ContextImpl.createAppContext(
this, getSystemContext().mPackageInfo);
mInitialApplication = context.mPackageInfo.makeApplication(true, null);
mInitialApplication.onCreate();
} catch (Exception e) {
throw new RuntimeException(
"Unable to instantiate Application():" + e.toString(), e);
}
}

ViewRootImpl.ConfigChangedCallback configChangedCallback = (Configuration globalConfig) -> {
synchronized (mResourcesManager) {
// TODO (b/135719017): Temporary log for debugging IME service.
if (Build.IS_DEBUGGABLE && mHasImeComponent) {
Log.d(TAG, "ViewRootImpl.ConfigChangedCallback for IME, "
+ "config=" + globalConfig);
}

// We need to apply this change to the resources immediately, because upon returning
// the view hierarchy will be informed about it.
if (mResourcesManager.applyConfigurationToResources(globalConfig,
null /* compat */,
mInitialApplication.getResources().getDisplayAdjustments())) {
mConfigurationController.updateLocaleListFromAppContext(
mInitialApplication.getApplicationContext());

// This actually changed the resources! Tell everyone about it.
final Configuration updatedConfig =
mConfigurationController.updatePendingConfiguration(globalConfig);
if (updatedConfig != null) {
sendMessage(H.CONFIGURATION_CHANGED, globalConfig);
mPendingConfiguration = updatedConfig;
}
}
}
};
ViewRootImpl.addConfigCallback(configChangedCallback);
}

实习笔记-18

协调问题

  1. 需要SceneEvent的Observer(√)
  2. 跳转问题(√):
    1. battery错误跳转到boost,boost错误跳转到boost
    2. MainPageActivity在任务栈中时,点击通知按钮不跳转到功能页面,没有处理onNewIntent
  3. 在什么位置startService(√)

startActivity的过程

  1. 如果intent指明了Component,直接通过component找到ActivityInfo,否则
  2. 如果Intent指定了组件所在包名,通过包名获取ActivityInfo,否则
  3. 通过ActivityIntentResolver等类的queryIntentForPackage进行模糊匹配,如Action,Category