Andrews review .Part1

Andrews Chart

Here Insert Picture Description

Android application components

Android application includes four kinds of components:

  1. The Activity (Activity): visual user interface;
  2. Service (activity): execution duration, time-consuming and user interface without operator interaction;
  3. Broadcast Receiver (Broadcast): Global listener receives a broadcast from the system and applications;
  4. Content Provider (content provider): shared persistent data storage mechanism;

Activity

Activity for providing a visual user interface components that can interact with the user to complete a task.

Common method

method Functional Description
setContentView(int layoutResID) Activity settings interface layout
onCreate(Bundle savedInstanceState) Call when you first create Activity, you must call in here setContentView()to define the layout of the Activity interface.
onStart() When you start calling Activity, after the call is completed Activity will enter the "Started" status, and visible to the user. This callback contains Activity enters the front desk for final preparations before the interaction with the user.
onStop () Stop calling Activity, Activity on when the user is no longer visible, the system will call onStop(). The reason for this may be the Activity is destroyed, the new Activity started or existing Activity is entering a "restored" state and covers stopped Activity. In all these cases, completely stopped Activity will no longer be visible. The next callback system calls will be onRestart()(if Activity re-user interaction) or onDestroy()(if Activity completely terminated).
onDestroy () 销毁Activity时调用,此回调是 Activity 接收的最后一个回调。通常,实现 onDestroy() 是为了确保在销毁 Activity 或包含该 Activity 的进程时释放该 Activity 的所有资源。
onRestart() Activity由停止状态恢复时调用,onRestart() 会从 Activity 停止时的状态恢复 Activity。此回调后面总是跟着 onStart()
onResume() Activity由暂停状态恢复时调用,此时,该 Activity 位于 Activity 堆栈的顶部,并会捕获所有用户输入。应用的大部分核心功能都是在 onResume() 方法中实现的。onResume() 回调后面总是跟着 onPause() 回调。
onPause() 当 Activity 失去焦点并进入“已暂停”状态时,系统就会调用 onPause()。当系统为您的 Activity 调用 onPause() 时,从技术上来说,这意味着您的 Activity 仍然部分可见,但大多数情况下,这表明用户正在离开该 Activity,该 Activity 很快将进入“已停止”或“已恢复”状态。如果用户希望界面继续更新,则处于“已暂停”状态的 Activity 也可以继续更新界面。onPause() 执行完毕后,下一个回调为 onStop()onResume(),具体取决于 Activity 进入“已暂停”状态后发生的情况。
setResult(int resultCode) 返回数据给上一个Activity
setResult(int resultCode, Intent data) 范湖即将数据给上一个Activity
startActivityForResult(Intent intent, int requestCode) 携带数据并跳转至指定Activity
startActivityForResult(Intent intent, int requestCode, Bundle options) 携带数据跳转至指定Activity
finish() 结束当前Activity

Activity状态转换

Here Insert Picture Description

Activity的声明

除了要在Java代码中新建一个继承于Activity或者其他Activity的类,还要在mainfest清单中添加Activity的声明。

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        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>
    </application>

XML资源文件

Android的资源可分为两大类:

  1. 原生资源:无法通过由R类进行索引的原生资源;
  2. 索引资源:通过R类进行自动索引的资源;

资源访问的方式:

  1. Java代码访问资源:
  2. 在XML文件中访问资源

目录结构

Here Insert Picture Description

访问res资源

Java代码访问res下的资源:

R.resourceType.resourceName;

通过Resources类提供的接口访问;

Here Insert Picture Description

访问assets原生资源

Java代码中通过AssetManager类访问assets原生资源;

通过Resources类提供的getAssets()接口获取AssetManager对象,在通过其接口来获取资源:

getResources().getAssets().open("xxxx");

在XML中使用资源

@[packageName:]resourceType/resourceName

Android常用的定义资源的XML文件有四种:

  1. strings.xml:用于定义文本内容的资源文件;
  2. colors.xml:用于定义颜色设置的资源文件;
  3. dimens.xml:用于定义尺寸的资源文件;
  4. styles.xml:用于定义主题和样式的资源文件;

文本资源文件

strings.xml文本资源文件

<resources>
    <string name="app_name">My Application</string>
</resources>

Java代码中访问字符串

String appName = getString(R.String.app_name);

XML文件中访问字符串资源

@string/app_name

颜色设置资源文件

colors.xml颜色资源文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
</resources>

颜色值的声明有以下四种方式:

  • #RGB
  • #ARGB
  • #RRGGBB
  • #AARRGGBB

语法

<color name="color_name">#color_value</color>

Java代码中访问颜色

int color = getColor(R.color.colorAccent);

XML文件中访问颜色资源文件

@color/color_name

AndroidMainfest.xml清单文件

清单文件中通常包含以下六项信息:

  1. 声明应用程序的包名;
  2. 描述应用程序组件;
  3. 确定宿主应用程序进程;
  4. 声明应用程序拥有的权限;
  5. 定义应用程序所支持API的最低等级;
  6. 列举应用程序必须链接的库;

设置清单文件的属性时,需要遵守几项规则:

  1. Elements: only and is necessary and can only occur once in all elements;
  2. Properties: Most attribute elements are optional, but there are a few attributes that must be set;
  3. The class name is defined: All element names which corresponds to the class name in the SDK;
  4. Multi numerical terms: If an element has more than one value must be a property of the described element having a plurality of numerical terms by repeated manner, and a plurality of values ​​of one-time can not be described in an attribute;
  5. Resource Item Description: When you need to refer to a resource, using the "@ [packageName:] type: name" format referenced;
  6. String Value: Similar to other languages;

Obtain system privileges:

<uses-permission android:name="android.permission.xxxx" />

Custom permission to use the element declaration:

<permission 
        android:label="权限名称"
        android:description="@string/descriptionType"
        android:protectionLevel="levelName"
        android:icon="@drawable/icoResName"
        android:name="PackageName"></permission>

Permission level Description:

  • normal-- low risk privileges
  • dangerous-- high risk privileges
  • signature-- signature authority
  • signatureOrSystem-- signature or system privileges

Android based on the current operating state of the application components and their assembly in accordance with the degree of importance of all the processes in descending divided into five priorities:

  1. Foreground process;
  2. Visible process;
  3. Service process;
  4. backstage process;
  5. Empty process;

Application

By extending the Application class, you can do three things:

  1. Responding to application-level time broadcast of Android runtime;
  2. Pass objects between application components;
  3. Resource management and maintain multiple application components used;

Application class creation and termination of the application, change the low available memory and processing time configuration provides interfaces:

  • onCreate()
  • onLowMemory()
  • onTrimMemory()
  • onConfigurationChanged()

Invasion delete images from the Internet ...

Published 24 original articles · won praise 8 · views 1844

Guess you like

Origin blog.csdn.net/qq_40462579/article/details/104503767