android
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseandroid
Android
Purpose
用途
This skill equips the AI to handle core Android development tasks, including SDK setup, managing activity and fragment lifecycles, implementing the permissions model, applying Material Design principles, and preparing apps for Google Play Store distribution. It focuses on practical implementation for efficient app building.
该技能让AI能够处理核心Android开发任务,包括SDK配置、管理Activity和Fragment生命周期、实现权限模型、应用Material Design原则,以及为Google Play商店分发准备应用。它专注于高效构建应用的实践实现。
When to Use
使用场景
Use this skill for Android app development scenarios, such as initializing projects with SDK tools, debugging lifecycle events (e.g., onPause or onDestroy), requesting runtime permissions, designing responsive UIs with Material components, or automating Play Store uploads. Apply it when working on mobile apps in the "mobile" cluster, especially for Google ecosystem integrations.
在Android应用开发场景中使用该技能,例如使用SDK工具初始化项目、调试生命周期事件(如onPause或onDestroy)、申请运行时权限、使用Material组件设计响应式UI,或者自动化Play商店上传。在处理「移动」集群中的移动应用时使用,尤其是涉及Google生态系统集成的场景。
Key Capabilities
核心功能
- Set up Android SDK: Download and configure via SDK Manager with commands like to view packages.
sdkmanager --list - Manage activity/fragment lifecycles: Implement methods like onCreate() for activities or onViewCreated() for fragments to handle state changes.
- Handle permissions model: Use runtime checks with ContextCompat.checkSelfPermission() and request via ActivityCompat.requestPermissions().
- Apply Material Design: Integrate components like MaterialButton or BottomNavigationView from the Material library.
- Manage Play Store: Prepare app bundles and use Google Play Console API for uploads, requiring authentication with $GOOGLE_API_KEY.
- 配置Android SDK:通过SDK Manager下载并配置,使用等命令查看包。
sdkmanager --list - 管理Activity/Fragment生命周期:实现Activity的onCreate()或Fragment的onViewCreated()等方法来处理状态变化。
- 处理权限模型:使用ContextCompat.checkSelfPermission()进行运行时检查,并通过ActivityCompat.requestPermissions()申请权限。
- 应用Material Design:集成Material库中的MaterialButton或BottomNavigationView等组件。
- 管理Play商店:准备应用包,并使用Google Play Console API进行上传,需要使用$GOOGLE_API_KEY进行身份验证。
Usage Patterns
使用模式
To set up a new Android project:
- Use Android Studio or CLI: Run to create a basic project.
gradle init --type android-library - Add dependencies in build.gradle: e.g., implementation 'com.android.support:appcompat-v7:28.0.0'. For handling fragment lifecycles:
- Extend Fragment and override onCreateView() to inflate layouts.
- Use getActivity() in fragments to access activity context for permission requests. When requesting permissions:
- Check status first: If not granted, call requestPermissions() with a request code.
- Handle results in onRequestPermissionsResult() to proceed or show errors. For Material Design: Wrap layouts with CoordinatorLayout and add behaviors like app:layout_behavior="@string/appbar_scrolling_view_behavior".
要创建新的Android项目:
- 使用Android Studio或CLI:运行创建基础项目。
gradle init --type android-library - 在build.gradle中添加依赖:例如。 处理Fragment生命周期:
implementation 'com.android.support:appcompat-v7:28.0.0' - 继承Fragment并重写onCreateView()来加载布局。
- 在Fragment中使用getActivity()获取Activity上下文以申请权限。 申请权限时:
- 先检查状态:如果未授权,调用requestPermissions()并传入请求码。
- 在onRequestPermissionsResult()中处理结果,继续操作或显示错误。
对于Material Design:使用CoordinatorLayout包裹布局,并添加行为属性如。
app:layout_behavior="@string/appbar_scrolling_view_behavior"
Common Commands/API
常用命令/API
- CLI Commands: Use to list devices;
adb deviceswith flags likeadb logcatfor filtered logs;-s MyAppto install specific SDK components.sdkmanager --install "build-tools;30.0.3" - API Endpoints: For Play Store, use Google Play Developer API (e.g., POST to https://www.googleapis.com/androidpublisher/v3/applications/{packageName}/edits with $GOOGLE_API_KEY in headers).
- Code Snippets:
- Activity lifecycle example:
java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } - Permissions request:
java
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); } - Material Design component:
xml
<com.google.android.material.button.MaterialButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me" /> - Fragment lifecycle:
java
@Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); // Initialize views here }
- Activity lifecycle example:
- CLI命令:使用列出设备;使用带参数的
adb devices过滤日志;使用adb logcat -s MyApp安装特定SDK组件。sdkmanager --install "build-tools;30.0.3" - API端点:对于Play商店,使用Google Play开发者API(例如向https://www.googleapis.com/androidpublisher/v3/applications/{packageName}/edits发送POST请求,请求头中携带$GOOGLE_API_KEY)。
- 代码片段:
- Activity生命周期示例:
java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } - 权限申请:
java
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); } - Material Design组件:
xml
<com.google.android.material.button.MaterialButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me" /> - Fragment生命周期:
java
@Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); // Initialize views here }
- Activity生命周期示例:
Integration Notes
集成说明
Integrate Android SDK by setting environment variables: export ANDROID_HOME=/path/to/sdk and add to PATH. For Google services, include Google Play Services in build.gradle: implementation 'com.google.android.gms:play-services-auth:20.1.0' and use $GOOGLE_API_KEY for API calls (e.g., in HTTP requests: Authorization: Bearer $GOOGLE_API_KEY). When combining with other skills, ensure compatibility: For mobile cluster integrations, align with iOS skill for cross-platform configs; use JSON config files like {"apiKey": "$GOOGLE_API_KEY"} for Play Store automation scripts.
通过设置环境变量集成Android SDK:并添加到PATH中。对于Google服务,在build.gradle中引入Google Play Services:,并在API调用中使用$GOOGLE_API_KEY(例如在HTTP请求中:)。与其他技能结合时,确保兼容性:对于移动集群集成,与iOS技能对齐以实现跨平台配置;使用JSON配置文件如用于Play商店自动化脚本。
export ANDROID_HOME=/path/to/sdkimplementation 'com.google.android.gms:play-services-auth:20.1.0'Authorization: Bearer $GOOGLE_API_KEY{"apiKey": "$GOOGLE_API_KEY"}Error Handling
错误处理
Handle permission errors by checking results in onRequestPermissionsResult(): If request is denied, log with Log.e("Permission", "Denied") and prompt user via AlertDialog. For lifecycle issues (e.g., NullPointerException in onDestroy), use try-catch blocks around resource releases. Common SDK errors: If fails, verify internet connectivity or use --verbose flag for details. For Play Store API, catch HttpException for 401 errors by re-authenticating with $GOOGLE_API_KEY. Always wrap API calls in try { ... } catch (Exception e) { Log.d("Error", e.getMessage()); }.
sdkmanager通过在onRequestPermissionsResult()中检查结果来处理权限错误:如果请求被拒绝,使用记录日志,并通过AlertDialog提示用户。对于生命周期问题(例如onDestroy中的NullPointerException),在资源释放代码周围使用try-catch块。常见SDK错误:如果执行失败,检查网络连接或使用--verbose参数查看详细信息。对于Play商店API,捕获401错误的HttpException,通过重新验证$GOOGLE_API_KEY解决。始终将API调用包裹在中。
Log.e("Permission", "Denied")sdkmanagertry { ... } catch (Exception e) { Log.d("Error", e.getMessage()); }Concrete Usage Examples
实际使用示例
-
Implementing an activity with permission check: Create an activity to request camera access:java
public class CameraActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera); if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 100); } } }Then build and run:../gradlew assembleDebug && adb install app/build/outputs/apk/debug/app-debug.apk -
Using Material Design for a login screen: Design a fragment with Material components:xml
<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <com.google.android.material.textfield.TextInputEditText android:hint="Username" /> </com.google.android.material.textfield.TextInputLayout>In the fragment: Override onCreateView() to inflate and handle user input, ensuring compatibility with activity lifecycle.
-
实现带权限检查的Activity: 创建一个申请相机权限的Activity:java
public class CameraActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera); if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 100); } } }然后构建并运行:。./gradlew assembleDebug && adb install app/build/outputs/apk/debug/app-debug.apk -
使用Material Design设计登录界面: 用Material组件设计一个Fragment:xml
<com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <com.google.android.material.textfield.TextInputEditText android:hint="Username" /> </com.google.android.material.textfield.TextInputLayout>在Fragment中:重写onCreateView()来加载布局并处理用户输入,确保与Activity生命周期兼容。
Graph Relationships
关联关系
- Related to cluster: mobile (shares mobile development concepts).
- Connected to skills: ios (for cross-platform mobile strategies), google (via shared tags like "google" for ecosystem tools).
- Links: sdk (direct overlap in Android SDK usage), permissions (common with web skills for access control).
- 关联集群:移动(共享移动开发概念)。
- 关联技能:iOS(用于跨平台移动策略)、Google(通过共享标签如「google」关联生态系统工具)。
- 链接:SDK(与Android SDK使用直接重叠)、权限(与Web技能的访问控制有共性)。