Android应用代码结构与运行流程详解(Java版)
一、Android应用代码结构
1.1 项目整体结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| MyApp/ ├── app/ │ ├── manifests/ # Android清单文件 │ │ └── AndroidManifest.xml │ ├── java/ # Java源代码 │ │ └── com/example/myapp/ │ │ ├── MainActivity.java │ │ ├── models/ # 数据模型类 │ │ ├── adapters/ # 适配器类 │ │ ├── utils/ # 工具类 │ │ └── ... │ ├── res/ # 资源文件 │ │ ├── layout/ # 布局文件 │ │ ├── values/ # 值资源(字符串、颜色等) │ │ ├── drawable/ # 图片资源 │ │ ├── mipmap/ # 应用图标 │ │ └── ... │ └── build.gradle # 模块构建配置 ├── gradle/ └── build.gradle # 项目构建配置
|
1.2 核心文件详解
AndroidManifest.xml - 应用配置文件
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
| <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapp"> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MyService" /> <receiver android:name=".MyReceiver" /> </application> </manifest>
|
MainActivity.java - 主Activity示例
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
| package com.example.myapp;
import android.os.Bundle; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView = findViewById(R.id.text_view); textView.setText("Hello Android!"); findViewById(R.id.my_button).setOnClickListener(view -> { }); } @Override protected void onStart() { super.onStart(); } @Override protected void onResume() { super.onResume(); } @Override protected void onPause() { super.onPause(); } @Override protected void onStop() { super.onStop(); } @Override protected void onDestroy() { super.onDestroy(); } }
|
activity_main.xml - 布局文件示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <Button android:id="@+id/my_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me" /> </LinearLayout>
|
二、Android应用启动与运行流程
2.1 应用启动流程
1 2 3 4 5 6 7 8 9 10 11 12
| 1. 用户点击应用图标 2. Launcher向AMS发送启动Intent 3. AMS检查应用进程是否存在 ↓ 4. 如果不存在:Zygote fork新进程 → 创建Application对象 ↓ 5. 创建主Activity实例 ↓ 6. 调用Activity生命周期方法: onCreate() → onStart() → onResume() ↓ 7. Activity进入运行状态
|
2.2 详细启动时序
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
| public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); } }
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState != null) { } setContentView(R.layout.activity_main); } @Override protected void onStart() { super.onStart(); } @Override protected void onResume() { super.onResume(); } @Override protected void onPause() { super.onPause(); } @Override protected void onStop() { super.onStop(); } @Override protected void onRestart() { super.onRestart(); } @Override protected void onDestroy() { super.onDestroy(); } }
|
三、重要注意事项
3.1 线程管理要点
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
| public void loadData() { }
public void loadData() { new Thread(() -> { String result = fetchDataFromNetwork(); runOnUiThread(() -> { updateUI(result); }); }).start(); }
private class MyAsyncTask extends AsyncTask<Void, Void, String> { @Override protected String doInBackground(Void... voids) { return fetchDataFromNetwork(); } @Override protected void onPostExecute(String result) { updateUI(result); } }
|
3.2 内存管理注意事项
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
| public class MainActivity extends AppCompatActivity { private static Context sContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); sContext = this; Context appContext = getApplicationContext(); new Thread(new Runnable() { @Override public void run() { } }).start(); new Thread(new MyRunnable()).start(); } private static class MyRunnable implements Runnable { @Override public void run() { } } }
|
3.3 生命周期管理最佳实践
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
| public class MainActivity extends AppCompatActivity { private TextView mTextView; private MyDataLoader mDataLoader; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = findViewById(R.id.text_view); mDataLoader = new MyDataLoader(); } @Override protected void onResume() { super.onResume(); mDataLoader.startLoading(); } @Override protected void onPause() { super.onPause(); mDataLoader.pauseLoading(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("KEY_TEXT", mTextView.getText().toString()); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); String savedText = savedInstanceState.getString("KEY_TEXT"); mTextView.setText(savedText); } }
|
3.4 资源访问注意事项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String appName = getString(R.string.app_name); int primaryColor = getResources().getColor(R.color.primary); float textSize = getResources().getDimension(R.dimen.text_size); String title = getString(R.string.app_title); } }
|
四、常见问题与解决方案
4.1 应用崩溃常见原因
- 空指针异常:使用对象前检查是否为null
- 主线程阻塞:耗时操作放入子线程
- 内存溢出:及时释放资源,使用合适的数据结构
- 权限问题:运行时请求必要权限
4.2 调试技巧
1 2 3 4 5 6 7 8 9
| Log.d("MainActivity", "当前状态: " + state); Log.e("MainActivity", "错误信息", exception);
Toast.makeText(this, "操作完成", Toast.LENGTH_SHORT).show();
|
4.3 性能优化建议
- 布局优化:减少嵌套层级,使用ConstraintLayout
- 内存优化:及时回收Bitmap,使用内存缓存
- 网络优化:合理使用缓存,合并网络请求
- 电池优化:避免不必要的后台服务,使用JobScheduler
五、学习资源推荐
- 官方文档:developer.android.com
- Android Studio:熟练使用IDE的调试工具
- Git:学习版本控制管理代码
- 设计模式:学习MVC/MVP/MVVM架构模式