本项目为「天气查询应用」,核心功能包括:
组件 | 版本说明 |
---|---|
Android SDK | API 33 (Android 13) |
Kotlin | 8.0 |
Gradle | 5.1 |
IDE | Android Studio Giraffe |
依赖管理 | Gradle Wrapper |
// build.gradle(app)核心配置 dependencies { implementation "androidx.core:core-ktx:1.10.1" implementation "androidx.appcompat:appcompat:1.6.1" implementation "com.google.android.material:material:1.9.0" // 网络请求 implementation "com.squareup.retrofit2:retrofit:2.9.0" implementation "com.squareup.retrofit2:converter-gson:2.9.0" // 图片加载 implementation "com.github.bumptech.glide:glide:4.15.1" annotationProcessor "com.github.bumptech.glide:compiler:4.15.1" // 数据库 implementation "androidx.room:room-runtime:2.5.1" kapt "androidx.room:room-compiler:2.5.1" // 生命周期管理 implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1" }
FusedLocationProviderClient
获取经纬度ConstraintLayout
+RecyclerView
实现天气列表LiveData
+ViewModel
架构// WeatherViewModel.kt class WeatherViewModel(application: Application) : AndroidViewModel(application) { private val _weatherLiveData = MutableLiveData<WeatherData>() val weatherLiveData: LiveData<WeatherData> = _weatherLiveData fun fetchWeather(lat: Double, lon: Double) { viewModelScope.launch { try { val response = RetrofitClient.apiService.getWeather(lat, lon) _weatherLiveData.postValue(response.data) } catch (e: Exception) { _weatherLiveData.postValue(null) } } } }
SearchView
实现城市模糊查询-CityEntity.kt @Entity(tableName = "city_table") data class CityEntity( @PrimaryKey val id: Int, val cityName: String, val latitude: Double, val longitude: Double )
WorkManager
周期性检查预警weather_alerts
NotificationCompat.BigTextStyle
显示预警详情技术场景 | 解决方案 |
---|---|
异形天气图标 | SVG转Vector Asset,支持不同屏幕密度适配 |
背景天气动画 | Lottiechapter实现雨雪动态效果 |
数据缓存策略 | OKHttp缓存+Room本地数据库双缓存机制 |
暗黑模式适配 | 使用UiModeManager 检测模式,资源文件分-light/-night后缀 |
单元测试覆盖 | Mock Retrofit接口进行网络层测试,使用Robolectric验证UI逻辑 |
功能分类 | 库名称 | 用途说明 |
---|---|---|
网络请求 | Retrofit2 + OkHttp | REST API调用与网络缓存 |
图片加载 | Glide | 天气图标/背景图加载 |
图表绘制 | MPAndroidChart | 温度折线图/风力雷达图 |
数据库 | Room + SQLite | 城市数据持久化存储 |
异步任务 | Coroutines + WorkManager | 后台数据同步/定时任务 |
buildTypes{release{...}}
Q1:应用启动时定位失败如何处理?
A1:需检查以下方面:
ACCESS_COARSE_LOCATION
/ACCESS_FINE_LOCATION
LocationServices.isLocationEnabled
)FusedLocationProviderClient
的timeout参数Q2:网络请求返回401错误如何解决?
A2:排查步骤:
appkey
参数是否正确拼接HttpException
并提示用户检查网络