android开发3:四大基本组件的介绍与生命周期
android开发3:四大基本组件的介绍与生命周期
Android四大基本组件分别是Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播接收器,。
生命周期
进程类别
组件介绍
Activity(表现层)
每个View对象控制着窗口内的一个矩形空间;
View是一种层次化结构,Parent View中的布局属性会被子View继承;
位于View层次关系最底层的子View对象所代表的矩形空间就是跟用户进行交互的地方
onCreate
onStart
onRestart
onResume
onPause
onStop
onDestroy
在 android 中,Activity 的生命周期交给系统统一管理。
模式启动模式,每次激活Activity时都会创建Activity,并放入任务栈中。
2. singleTop
如果在任务的栈顶正好存在该Activity的实例, 就重用该实例,否者就会创建新的实例并放入栈顶(即使栈中
已经存在该Activity实例,只要不在栈顶,都会创建实例)。
3. singleTask
如果在栈中已经有该Activity的实例,就重用该实例(会调用实例的onNewIntent())。重用时,会让该实例回到
栈顶,因此在它上面的实例将会被移除栈。如果栈中不存在该实例,将会创建新的实例放入栈中。
4. singleInstance
在一个新栈中创建该Activity实例,并让多个应用共享改栈中的该Activity实例。一旦改模式的Activity的实
例存在于某个栈中,任何应用再激活改Activity时都会重用该栈中的实例,其效果相当于多个应用程序共享一个应用,
不管谁激活该Activity都会进入同一个应用中。
自定义View在Activity中的显示步骤:
package com.example.lesson1_helloandroid;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
/**
* 这个是自定义的MyView.
* 至少需要重载构造方法和onDraw方法
* 对于自定义的View如果没有自己独特的属性,可以直接在xml文件中使用就可以了
* 如果含有自己独特的属性,那么就需要在构造函数中获取属性文件attrs.xml中自定义属性的名称
* 并根据需要设定默认值,放在在xml文件中没有定义。
* 如果使用自定义属性,那么在应用xml文件中需要加上新的schemas,
* 比如这里是xmlns:my="http://schemas.android.com/apk/res/demo.view.my"
* 其中xmlns后的“my”是自定义的属性的前缀,res后的是我们自定义View所在的包
* @author Administrator
*
*/
public class MyView extends View{
//设置画笔
Paint paint;
//构造函数
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
paint = new Paint();
//设置颜色 字体大小 等
paint.setColor(Color.WHITE);
paint.setTextSize(20);
paint.setAntiAlias(true);
} protected void onDraw(Canvas canvas){
//自定义界面绘制一个矩形框,在矩形框绘制一段文字
super.onDraw(canvas);
//定义画布背景颜色
canvas.drawColor(Color.BLUE);
//画布上绘制矩形框
canvas.drawRect(10,10,110,110, paint);
//在矩形框上通过画笔绘制了文字
canvas.drawText("你妹的屌丝", 60, 170, paint);
}
}
===================================================通过xml布局显示(未测试,不过思路大致是这样)======================================
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView">
<attr name="textColor" format="color"/>
<attr name="textSize" format="dimension"/>
</declare-styleable>
</resources>
在布局文件中使用:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:my="http://schemas.android.com/apk/res/demo.view.my"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
> <demo.view.my.MyView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
my:textColor="#FFFFFFFF"
my:textSize="22dp"
/>
</LinearLayout>
Activity类:
package com.example.lesson1_helloandroid;//表示的是这个包的名称 import android.os.Bundle;
import android.app.Activity;//是一个活动包,每一个android活动都需要继承Activity类
import android.view.Menu;// public class Lesson1_HelloAndroid extends Activity { @Override
//onCreate 是一个重载函数,在这个函数中实现应用程序创建的所执行的过程。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置当前的视图(View)
//设置的方法是使用一个文件,这个文件因此决定了视图中包含的内容。
//当前设置表示从res/layout/目录中使用activity_lesson1_hello_android.xml文件
//setContentView(R.layout.activity_lesson1__hello_android);
MyView myview = new MyView(this);
this.setContentView(myview);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.lesson1__hello_android, menu);
return true;
} }
AndroidManifest.xml(注:好像给不给权限都可以运行,默认就可以了,俺是因为一直报错就加了权限。。。。不过最终问题还是因为啃爹的录视频没有new Paint())
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="18" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.test.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>
运行结果:
Service服务(较长生命周期但没有用户界面的程序):
启动一个新的服务,或者向一个已有的服务传递新的指令,可以调用如下两种方法:
1.Context.startService()
2.Context.bindService()
Broadcast Receiver服务
1.Context.sendBroadcast()
2.Context.sendOrderBroadcast()
3.Context.sendStickBroadcast()
Intent一旦发出,Android都会准 确找到相匹配的一个或多个Activity、Service或BroadcastReceiver作响 应。所以,不同类型的Intent消息不会出现重叠,BroadcastIntent消息只会发送给BroadcastReceiver,而绝不可能发送 给Activity或Server。有startActivity()传递的消息也只可能发送给Activity,由startService()传递的 Intent只可能发送给Service。
android开发3:四大基本组件的介绍与生命周期的更多相关文章
- android开发艺术探索学习 之 结合Activity的生命周期了解Activity的LaunchMode
转载请标明出处: http://blog.csdn.net/lxk_1993/article/details/50749728 本文出自:[lxk_1993的博客]: 首先还是先介绍下Activity ...
- Android开发艺术探索读书笔记——01 Activity的生命周期
http://www.cnblogs.com/csonezp/p/5121142.html 新买了一本书,<Android开发艺术探索>.这本书算是一本进阶书籍,适合有一定安卓开发基础,做 ...
- Android开发艺术探索(一)——Activity的生命周期和启动模式
Activity的生命周期和启动模式 生命周期有? 1.典型情况下的生命周期—>指有用户参与的情况下,Activity所经过的生命周期改变 2.异常情况下的生命周期—>指Activity被 ...
- Android四大基本组件介绍与生命周期
Android四大基本组件介绍与生命周期 Android四大基本组件分别是Activity,Service服务,Content Provider内容提供者,BroadcastReceiver广播接收器 ...
- Android四大组件之——Activity的生命周期(图文详解)
转载请在文章开头处注明本博客网址:http://www.cnblogs.com/JohnTsai 联系方式:JohnTsai.Work@gmail.com [Andro ...
- Android系列之Fragment(二)----Fragment的生命周期和返回栈
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- Spring注解开发-全面解析常用注解使用方法之生命周期
本文github位置:https://github.com/WillVi/Spring-Annotation/ 往期文章:Spring注解开发-全面解析常用注解使用方法之组件注册 bean生命周期 ...
- React Native组件的结构和生命周期
React Native组件的结构和生命周期 一.组件的结构 1.导入引用 可以理解为C++编程中的头文件. 导入引用包括导入react native定义的组件.API,以及自定义的组件. 1.1 导 ...
- .Net组件程序设计之对象生命周期
.Net组件程序设计之对象生命周期 .NET 垃圾回收 IDisposable() Using语句 .NET 垃圾回收 是CLR管理着垃圾回收器,垃圾回收器监控着托管堆,而我们使用的对象以及系统启动是 ...
随机推荐
- COJ 0244 HDNOIP201404最短路径
HDNOIP201404最短路径 难度级别: A: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 a.b.c是3个互不相等的1 ...
- App项目升级Xcode7&iOS9(续) - This bundle is invalid. The bundle identifier contains disallowed characters
金田 iOS 9发布已经有2月有余,现在Xcode已经有升级到Xcode7.1,开发环境安装等一系列相关的流程,以及Xcode 7 & iOS 9升级相关的一些部分,在这里就不再多加赘述(详见 ...
- vi查找
/pattern<Enter> :向下查找pattern匹配字符串 ?pattern<Enter>:向上查找pattern匹配字符串 使用了查找命令之后,使用如下两个键快速查找 ...
- Redis哨兵模式
Redis-Sentinel redis的哨兵模式 Redis Sentinel 模式简介 Redis-Sentinel是官方推荐的高可用解决方案,当redis在做master-slave的高可用方案 ...
- SQL Server 启用 xp_cmdshell 与bcp 使用
启用 xp_cmdshell 1: sp_configure 'show advanced options',1 2: reconfigure 3: GO 4: 5: sp_configure 'xp ...
- 添加链接服务器 SQL SERVER
使用sql语句: exec sp_addlinkedserver @server='serverontest',@provider='sqloledb',@srvproduct='',@datasrc ...
- Android 中文 API (40) —— RatingBar
Android 中文 API (40) —— RatingBar 前言 本章内容是 android.widget.RatingBar,译为"评分条",版本为Android 2.2 ...
- MySQL加密的性能测试
这是对MySQL进行加密性能测试的两篇文章系列之二.在第一篇中,我专门使用MySQL的内置的对SSL的支持来 做压力测试,产生了一些令人惊讶的结果. AD:WOT2015 互联网运维与开发者大会 热销 ...
- Linux学习笔记01:Linux下的drwxr-xr-x
1. drwxr-xr-x 第1字母:表示文件类型 d ------- 表示文件目录(directory) - ------- 表示二进制文件 l ------ ...
- linux命令之seq
seq命令简述 seq命令比较常用,在需要做循环的时候用于产生一个序列是再合适不过的工具了,常用方法也比较简单: Usage: seq [OPTION]... LAST seq [ ...