第一次写自己的blog,第一次把android的学习的各个知识点总结并增加自己学习的DEMO
来源:互联网 发布:codecademy知乎 编辑:程序博客网 时间:2024/05/16 01:04
android知识点回顾总结(一)
视图(view)对象:是应用程序中负责生成用户界面的部分。也是在整个mvc架构中用户唯一可以看到的一层,接收用户的输入,显示处理结果。
android鼓励弱耦合和组件的重用,在android中mvc的具体体现如下:
1)视图层(view):一般采用xml文件进行界面的描述,使用的时候可以非常方便的引入,当然,如何你对android了解的比较的多了话,就一定 可以想到在android中也可以使用javascript+html等的方式作为view层,当然这里需要进行java和javascript之间的通 信,幸运的是,android提供了它们之间非常方便的通信实现。
2)控制层(controller):android的控制层的重 任通常落在了众多的acitvity的肩上,这句话也就暗含了不要在acitivity中写代码,要通过activity交割model业务逻辑层处理, 这样做的另外一个原因是android中的acitivity的响应时间是5s,如果耗时的操作放在这里,程序就很容易被回收掉。
3)模型层(model):对数据库的操作、对网络等的操作都应该在model里面处理,当然对业务计算等操作也是必须放在的该层的。
一 应用程序层(applications)
该层提供一些核心应用程序包,例如电子邮件、短信、日历、地图、浏览器和联系人管理等。同时,开发者可以利用Java语言设计和编写属于自己的应用程序,而这些程序与那些核心应用程序彼此平等、友好共处。
二 应用程序框架层(application Framework)
该层是Android应用开发的基础,开发人员大部分情况是在和她打交道。应用程序框架层包括活动管理器、窗口管理器、内容提供者、视图系统、包管理器、电话管理器、资源管理器、位置管理器、通知管理器和XMPP服务十个部分。在Android平台上,开发人员可以完全访问核心应用程序所使用的API框架。并且,任何一个应用程序都可以发布自身的功能模块,而其他应用程序则可以使用这些已发布的功能模块。基于这样的重用机制,用户就可以方便地替换平台本身的各种应用程序组件。
三 系统库和Android运行时
系统库包括九个子系统,分别是图层管理、媒体库、SQLite、OpenGLEState、FreeType、WebKit、SGL、SSL和libc。Android运行时包括核心库和Dalvik虚拟机,前者既兼容了大多数Java语言所需要调用的功能函数,又包括了Android的核心库,比如android.os、android.net、android.media等等。后者是一种基于寄存器的java虚拟机,Dalvik虚拟机主要是完成对生命周期的管理、堆栈的管理、线程的管理、安全和异常的管理以及垃圾回收等重要功能。
四 Linux内核(kernel)
Android核心系统服务依赖于Linux2.6内核,如安全性、内存管理、进程管理、网络协议栈和驱动模型。Linux内核也是作为硬件与软件栈的抽象层。驱动:显示驱动、摄像头驱动、键盘驱动、WiFi驱动、Audio驱动、flash内存驱动、Binder(IPC)驱动、电源管理等。
线性布局(LinearLayout):该标签下的所有子元素会根据它的orientation属性值来决定是按行还是按列来显示
示例代码如下,<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/name_text"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ok_text"
/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/canel_text"
/>
</LinearLayout>
显示结果为:
其中属性“xmlns:android”指定命名空间,顶级元素必须指定命名空间,而在该命名空间中的控件的属性如layout_width,要在属性前面加上”android:”做前缀。Layout_width指定了这个元素的宽度,Layout_height指定这个元素的高度。而属性”orientation”指定了子元素的排列方式,当指定为“vertical”时,是子元素垂直排列,每个子元素会站独立的一行,而为“horizontal”时,表示子元素要水平排列。
RelativeLayout(相对布局)
相对布局中的视图是按相互之间的相对位置来确定的,并不是线性布局中的必须按行或按列单个显示,相对布局文件如下:
<?xml version="1.0"encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/button2"/>
</RelativeLayout>
显示效果如图:
表格布局(TableLayout)
表格布局的风格和heml中表格布局的风格很像,只是采用的标签不一样,<TableLayout>是顶级元素,说明采用的是顶级元素,<TableRow>定义一个行,<TextView>定义一个单元格,演示代码如下:
其中”android:stretchColumns=“0,1,2,3””指定的是列
<?xml version="1.0"encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="0,1,2,3">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/gender"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/age"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/phone"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/namezs"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/genderzs"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/agezs"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/phonezs"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/namels"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/genderls"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/agels"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/phonels"
/>
</TableRow>
</TableLayout>
显示效果如下图:
1,post请求发送数据到服务器端,而且数据放在html header中一起发送到服务器url,数据对用户不可见,get请求是把参数值加到url的队列中,这在一定程度上,体现出post的安全性要比get高
2,get传送的数据量小,一般不能大于2kb,post传送的数据量大,一般默认为不受限制。
//定义保存所有Socket的ArrayList
public static ArrayList<Socket> socketList = new ArrayList<Socket>();
public static void main(String[] args){
try {
ServerSocket ss = new ServerSocket(8888);
while(true){
//阻塞一直在等待客户端的链接
Socket s = ss.accept();
socketList.add(s);
//成功加入这个链接的时候启动一个线程
new Thread(new ServerThread(s)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class ServerThread implements Runnable {
//定义线程所处理的Socket
Socket s = null;
//处理该线程Socket所对应的输入流
BufferedReader br = null;
public ServerThread(Socket s) {
this.s = s;
//初始化该Socket对于的输入流
try {
br = new BufferedReader(new InputStreamReader(s.getInputStream(),"utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
String content = null;
//采用循环不断从Socket中读取客户端发过来数据
while((content = readFormClient())!=null){
//遍历socketList中的每一个socket
for(Socket s :SimpleServer.socketList){
//将读到的内容向每个Socket发送一次
OutputStream os = s.getOutputStream();
os.write((content+"\n").getBytes("utf-8"));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private String readFormClient() {
try {
return br.readLine();
} catch (IOException e) {
//捕捉到异常的时候说明客户端已经关闭,删除Socket
SimpleServer.socketList.remove(s);
}
return null;
}
}
EditText input , show;
Button send;
Handler handler;
OutputStream os;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
show = (EditText) findViewById(R.id.show);
input = (EditText) findViewById(R.id.input);
send = (Button) findViewById(R.id.send);
Socket socket ;
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
//如果消息来自子线程
if(msg.what == 0x123)
{
//将读取的内容追加显示在文本框中
show.append("\n"+msg.obj.toString());
}
}
};
try {
socket = new Socket("115.204.126.206", 8888);
//客户端启动线程不断读取来自服务器的数据
new Thread(new ClientThread(socket,handler)).start();
} catch (IOException e) {
e.printStackTrace();
}
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {// TODO Auto-generated method stub
try{
//将用户在文本框中写入的数据写入网络
os.write((input.getText().toString() +"\r\n").getBytes("utf-8"));
//清空文本框
input.setText("");
}catch(Exception e)
{
e.printStackTrace();
}
}
});
}
}
//该线程负责处理的Socket
Socket socket;
Handler handler;
//该线程所处理的Socket的输入流
BufferedReader br = null;
public ClientThread(Socket socket, Handler handler)throws IOException {
this.socket = socket;
this.handler = handler;
br = new BufferedReader(new InputStreamReader( socket.getInputStream()));
}
@Override
public void run() {
try{
String content = null;
//不断读取Socket输入流的内容
while((content = br.readLine())!= null){
//每当读到来自服务器的数据之后,发送消息到通知程序界面显示该数据
Message msg = new Message();
msg.what = 0x123;
msg.obj = content;
handler.sendMessage(msg);
}
}catch(IOException e )
{
e.printStackTrace();
}
}
}
- 第一次写自己的blog,第一次把android的学习的各个知识点总结并增加自己学习的DEMO
- 第一次拥有自己的blog
- 第一次有了自己的blog
- 第一次上自己的BLOG
- 第一次发表自己的Blog
- 第一次有自己的blog
- 第一次拥有自己的Blog
- 第一次拥有自己的blog
- 第一次拥有自己的BLOG
- 第一次开始自己的blog
- 第一次写自己的心得
- 第一次自己写的Tab
- 第一次写自己的博客
- 第一次触电Blog,希望总结一下自己最近的学习成果
- 第一次写我自己的BLOG。记录一下
- 第一次写自己的blog耶!好开心...
- 今天第一次使用MIcrosoft Bloger来写自己的blog
- 第一次写的blog
- (转载)Instrumentation 框架简介
- 张朝霞博客,先谈下个人的一些事情,总结自己
- 解决ORA-12560: TNS: protocol adapter error 的步骤
- securecrt 光标不显示,光标丢失
- 数据结构第一章
- 第一次写自己的blog,第一次把android的学习的各个知识点总结并增加自己学习的DEMO
- http请求状态码大全详解
- RHEL 7 / CentOS 7 : Disable Firewalld and use iptables
- 总结自己的2个工作,感觉还是学习重要。
- 快速排序求第K小
- 20141916
- Largest Rectangle in Histogram
- 再议访问者模式 - Visitor vs Acyclic Visitor
- css一些简写优化技巧