Learn from me the first program in Android II

Video lesson: https://edu.csdn.net/course/play/7621

1: Four core components

Activity (activity)
     is the component responsible for interacting with the user in the Android application.
Service (service)
     is usually running in the background, it generally does not need to interact with the user, so the Service component does not have a graphical user interface
BroadcastReceiver (broadcast receiver)
     for the receiving system      The prerequisite for real-time data exchange between multiple applications of the broadcast
ContentProvider (content provider) sent by other components

Two: Project directory structure

Android stores different content in different directories according to purpose

The src folder
stores the source code written by the developer and
stores the imported third-party source code

gen folder

Store the code automatically generated by the system

assets folder

Store nonmedia files used in the program

bin folder

Store the compiled files and packaging files

libs folder

Store third-party jar packages

res folder

Store layout files, resource files, etc.

AndroidManifest.xml

Configuration file


The res folder is mainly used to store various resources

Store various picture files under drawable related files.
The layout file of the graphical user interface is stored under layout.
The layout file of the menu is stored under the menu.
Store various data resources under values, such as string resources.

         AndroidManifest.xml文件
                定义了应用程序的基本信息
               定义了应用程序包。
              定义应用程序组件。
             确定默认Activity。
             确定API使用权限。
            定义最小API级别。
           指定引用程序库。

           R.java文件


 每一种资源在R类中存在一个唯一的ID 
R类存在与gen目录中。 
一般情况下不需要修改这个类(自动生成) 。 

java源代码中可是引用资源                  Button btnLogin= (Button)findViewById(R.id.btnLogin);

XML文件中也可以引用资源                 @drawable/ic_launcher                  @string/app_name

assets下存放nonmedia文件

不会被编译成二进制,原样保存在apk包中。
不会被映射到R类中,需要使用AssetManager类进行访问。
可以创建子目录。


创建第一个Activity


Activity是一个应用程序组件:
包含了一个可以和用户交互的显示界面
比如:拍照、发邮件、浏览地图等
每个Activity都有一个窗口
这个窗口通常是充满屏幕的
也可以比屏幕小而浮于其他窗口之上
一个Android应用程序可以包含一个或多个Activity
这些Activity之间是相对独立的
当一个Activity启动时,之前的那个Activity就停止了

创建一个Activity通常有这样几个步骤: 

为这个Activity设计一个界面布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView android:id="@+id/textview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="我是文本控件" />


</LinearLayout>

需要创建一个Activity类的子类
必须实现onCreate()方法

public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}


Intent是在组件之间传递的消息

可以在同一个应用程序或者不同应用程序之间传递

这个消息是一个Intent类的对象,包含一组信息

Action——要执行的或者已发生的动作

ACTION_CALL(字符串常量)

ACTION_MAIN (字符串常量)

ACTION_SCREEN_ON (字符串常量)

Category——指明应该处理当前intent的组件种类

CATEGORY_HOME (字符串常量)

CATEGORY_LAUNCHER (字符串常量)

Intent过滤器用于指定组件“愿意”接收的Intent
可以包含Intent中包含的信息所对应的过滤条件
Action
Category
在配置Activity时通常通过Intent过滤器来给定Activity的执行条件

在AndroidManifest.xml文件中配置Activity

<manifest  …>
        <application …>
                <activity android:name=“com.csg.MainActivity” android:label="@string/app_name">
                        <intent-filter>
                                <action android:name=“android.intent.action.MAIN” />
                                <category android:name=“android.intent.category.LAUNCHER” />
                        </intent-filter>
                </activity>
        </application>
</manifest>

Android应用的构建过程如下:


Android应用的构建过程如下:
1、使用aapt工具生成R.java文件
2、使用javac工具将.java文件编译成.class文件
4、使用dx.bat批处理将众多.class文件转换成一个.dex文件
5、使用aapt工具打包资源文件(包括res、assets、AndroidManifest.xml等)
6、生成apk安装文件
SDK目录结构如下:


add-ons这里面保存着附加库
docs这里面是Android SDK API参考文档
platforms是每个平台的SDK真正的文件
tools目录里面包含了重要的编译工具
platform-tools保存着一些通用工具,比如adb、和aapt、aidl、dx等文件
samples是Android SDK自带的默认示例工程
tools作为SDK根目录下的tools文件夹,这里包含了重要的工具

SDK常用包如下:

android.os 提供基本的操作服务,消息传递和进程间通信IPC。

android.graphics 作为图形渲染包,提供图形渲染功能。

android.database 包含底层API处理数据库,方便操作数据库表和数据。

android.content 提供各种服务访问数据在手机设备上,程序安装到手机设备和其他相关资源,以及内容提供展示动态数据。

android.view  核心用户界面框架。

android.widget 提供标准用户界面元素,List(列表),Buttons(按钮),Layout manager(布局管理器)等,是组成我们界面的基本元素。

android.app 提供高层应用程序模型,实现使用Activity。

android.provider 提供方便调用系统提供的content providers的接口。

android.webikit 包含以系列工作在基于Web内容的API。

DDMS 的全称是Dalvik Debug Monitor Service

为测试设备截屏

查看特定进程的线程及堆信息

Logcat窗口

广播状态信息

模拟电话呼叫

收发SMS短信

虚拟地理坐标

Emulator Control可以用于模拟控制

模拟拨打和接听电话

模拟短信息收发

模拟地理坐标位置





Logcat窗体简介

Logcat窗体提供了应用运行中的日志输出与查询过滤 
可以输出设备的所有输出信息 
如果是真机,需要打开bebug模式 
支持日志级别 
支持过滤查询 


Android 中的日志工具类是Log( android.util.Log),这个类中提供了如下几个方法来供我们打印日志 
Log.v() 

打印那些最为琐碎的,意义最小的日志信息

Log.d() 

打印一些调试信息

Log.i() 

用于打印一些比较重要的数据

Log.w() 

用于打印一些警告信息

Log.e() 

用于打印程序中的错误信息

设置过滤器




Guess you like

Origin blog.51cto.com/2096101/2588826