|
| 1 | +#OSC-China源码学习——Splash界面 |
| 2 | + |
| 3 | +前段时间学习了git-osc的源码,今天开始学习OSC-China的源码,按照之前的流程进行学习。 |
| 4 | + |
| 5 | +##功能介绍: |
| 6 | + |
| 7 | +在应用启动的时候,出现一个启动的欢迎界面,在这个界面中完成的任务: |
| 8 | + |
| 9 | +> 1. Log日志的上传; |
| 10 | +> 2. 跳转到主页面 |
| 11 | +> 3. 动画——在动画结束的时候进行上述两项操作 |
| 12 | +
|
| 13 | +##集成指南: |
| 14 | + |
| 15 | +在自己开发应用的时候,Splash界面可以用来完成一些初始化工作,比如: |
| 16 | + |
| 17 | +> 1. 日志信息的上传; |
| 18 | +> 2. 资源的初始化(自己用过的经历——在Splash动画跳转的时候,将Assets文件夹中的内容拷贝到SD卡) |
| 19 | +
|
| 20 | +##详细介绍 |
| 21 | + |
| 22 | +1. AppStart.java —— 整个应用的入口 |
| 23 | +2. LogUploadService.java —— 在AppStart开启,完成上一次记录在本地的日志的上传 |
| 24 | +3. MainActivity.java —— 程序的主界面 |
| 25 | + |
| 26 | +--- |
| 27 | + |
| 28 | +###**AppStart分析** |
| 29 | + |
| 30 | +在AppStart中通过一个动画来控制Splash界面中的图片优雅的展示出来,在动画结束的时候,完成两个动作: |
| 31 | + |
| 32 | +> 1. 启动Service,上传日志; |
| 33 | +> 2. 跳转到程序主界面。 |
| 34 | +
|
| 35 | +###**LogUploadService分析**——启动Service |
| 36 | + |
| 37 | +通过如下代码启动Service,需要在Mainfest文件中进行配置 |
| 38 | + |
| 39 | +``` |
| 40 | + //在Mainfest文件中进行配置 |
| 41 | + <service android:name="net.oschina.app.LogUploadService" /> |
| 42 | + |
| 43 | + //启动Service |
| 44 | + Intent uploadLog = new Intent(this, LogUploadService.class); |
| 45 | + startService(uploadLog); |
| 46 | +``` |
| 47 | +**LogUploadService**在下一篇文章中详细学习 |
| 48 | + |
| 49 | +###**MainActivity分析**——展示主界面 |
| 50 | + |
| 51 | +通过如下代码启动主界面Activity,需要在Mainfest文件中进行配置 |
| 52 | + |
| 53 | +``` |
| 54 | +
|
| 55 | + //在主界面中的配置 |
| 56 | + <activity |
| 57 | + android:name=".ui.MainActivity" |
| 58 | + android:launchMode="singleTask" |
| 59 | + android:screenOrientation="portrait" > |
| 60 | + <intent-filter> |
| 61 | + <action android:name="android.intent.action.VIEW" /> |
| 62 | +
|
| 63 | + <category android:name="android.intent.category.BROWSABLE" /> |
| 64 | + <category android:name="android.intent.category.DEFAULT" /> |
| 65 | +
|
| 66 | + <data |
| 67 | + android:host="www.oschina.net" |
| 68 | + android:scheme="http" /> |
| 69 | + </intent-filter> |
| 70 | + <intent-filter> |
| 71 | + <action android:name="android.intent.action.VIEW" /> |
| 72 | +
|
| 73 | + <category android:name="android.intent.category.BROWSABLE" /> |
| 74 | + <category android:name="android.intent.category.DEFAULT" /> |
| 75 | +
|
| 76 | + <data |
| 77 | + android:host="www.oschina.net" |
| 78 | + android:scheme="https" /> |
| 79 | + </intent-filter> |
| 80 | + <intent-filter> |
| 81 | + <action android:name="android.intent.action.VIEW" /> |
| 82 | +
|
| 83 | + <category android:name="android.intent.category.BROWSABLE" /> |
| 84 | + <category android:name="android.intent.category.DEFAULT" /> |
| 85 | +
|
| 86 | + <data |
| 87 | + android:host="my.oschina.net" |
| 88 | + android:scheme="http" /> |
| 89 | + </intent-filter> |
| 90 | + <intent-filter> |
| 91 | + <action android:name="android.intent.action.VIEW" /> |
| 92 | +
|
| 93 | + <category android:name="android.intent.category.BROWSABLE" /> |
| 94 | + <category android:name="android.intent.category.DEFAULT" /> |
| 95 | +
|
| 96 | + <data |
| 97 | + android:host="my.oschina.net" |
| 98 | + android:scheme="https" /> |
| 99 | + </intent-filter> |
| 100 | + </activity> |
| 101 | + |
| 102 | + //启动主界面 |
| 103 | + Intent intent = new Intent(this, MainActivity.class); |
| 104 | + startActivity(intent); |
| 105 | +``` |
| 106 | + |
| 107 | +## 总结收获 |
| 108 | + |
| 109 | +> 1. 在Splash界面中启动服务,上传日志Log。 |
| 110 | +
|
| 111 | +## 目前为止,还不太明白的地方 |
| 112 | + |
| 113 | +* 在onCreate方法中有一个防止第三方跳转出现双实例的代码,还不太明白什么意思,代码如下: |
| 114 | + |
| 115 | +``` |
| 116 | + // 防止第三方跳转时出现双实例 |
| 117 | + Activity aty = AppManager.getActivity(MainActivity.class); |
| 118 | + if (aty != null && !aty.isFinishing()) { |
| 119 | + finish(); |
| 120 | + } |
| 121 | +``` |
| 122 | + |
| 123 | +* 在onResume中有一系列的动作,还不明白干嘛的: |
| 124 | + |
| 125 | +``` |
| 126 | + @Override |
| 127 | + protected void onResume() { |
| 128 | + super.onResume(); |
| 129 | + int cacheVersion = PreferenceHelper.readInt(this, "first_install", |
| 130 | + "first_install", -1); |
| 131 | + int currentVersion = TDevice.getVersionCode(); |
| 132 | + if (cacheVersion < currentVersion) { |
| 133 | + PreferenceHelper.write(this, "first_install", "first_install", |
| 134 | + currentVersion); |
| 135 | + cleanImageCache(); |
| 136 | + } |
| 137 | + } |
| 138 | +``` |
| 139 | + |
| 140 | + |
| 141 | + |
0 commit comments