第一次写自己的blog,第一次把android的学习的各个知识点总结并增加自己学习的DEMO

来源:互联网 发布:codecademy知乎 编辑:程序博客网 时间:2024/05/16 01:04

android知识点回顾总结(一)

入门(面试题):

1.android平台优势与不足:
优势:一.开源,开放性 二.挣脱运营商的束缚 三.不受任何限制的开发商 四.丰富的硬件选择 五.无缝结合Google的应用
不足:一.安全与隐私  二.首先开卖Android手机的不是最大运营商 三.运营商任然可以影响android手机 四、同类机型用户减少 五.过分依赖开发商
2.android的特点:
一.android拥有完善的应用程序框架,支持四大组件(activity,services,content provider 和 Broadcast)
二.开源的移动操作系统,研发成本低
三.支持轻量级的SQLite数据库
四.内置了核心的webkit浏览器
五.丰富的绘图功能
六.支持众多的硬件传感器,比如其他的一些硬件蓝牙、3G、Wifi、GPS等。

3.MVC模式:model (data)-view(UI)-controller(data&UI [connection])

模型(model)对象:是应用程序的主体部分,所有的业务逻辑都应该写在该层。

视图(view)对象:是应用程序中负责生成用户界面的部分。也是在整个mvc架构中用户唯一可以看到的一层,接收用户的输入,显示处理结果。

控制器(control)对象:是根据用户的输入,控制用户界面数据显示及更新model对象状态的部分,控制器更重要的一种导航功能,想用用户出发的相关事件,交给m哦得了处理。

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里面处理,当然对业务计算等操作也是必须放在的该层的。

4.Android 的系统架构:

一 应用程序层(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内存驱动、BinderIPC)驱动、电源管理等。


5.android 的开发环境的使用:
一.DDMS 和TraceView的区别,以及TraceView的使用方法?
DDMS 包含多个视图:File Explorer(用于浏览移动设备中的目录和文件)、Logcat(查看调试信息)、Emulator Control(用于控制模拟器)等等,所以说DDMS是一个集成调试、控制、浏览等操作一体的工具集,TraceView只是一个性能调优的图形化工具,开发人员可以根据这个工具查看程序中方法的执行效率。(.....)


6.如果adb的服务kill(死掉了)怎么办?
一.关闭模拟器,重新启动模拟器
二.可以在DDMS的Devices视图菜单点击“Reset adb”,启动adb服务
三.在任务管理器中结束“adb.exe”的进程,然后关闭模拟器,重新启动
四.这些都不行的话,只有关闭eclipse,重新启动


7.android基本组件:Activity和View 

Activity是Android中负责与用户交互的组件,view是所有UI控件的基类,继承Activity的基类,有自己的生命周期
设置Activity的窗口风格有一个setTheme(int resid);(不显示标题,以对话框的形式显示窗口)

Service一般不需要与用户交互,在后台运行(与Activity的区别),继承Service基类,也有自己的生命周期。
它的作用主要是:提供后台服务以及监控其他组件的运行状态

BroadcastReceiver:广播消息接收器,相当于一个监听器,与普通事件监听器不一样的是:这个监听的是android中其他应用的组件,很简单:

一.实现自己的BroadcastReceiver子类
二.重写onReceiver(Context context,Intent intent)方法

实现之后就需要注册:

一.Context.registReceiver()方法注册BroadcastReceiver
二.在清单文件中使用<receiver..//>元素完成注册

ContentProvider:实现多个应用程序之间进行数据交换

《意图》android的不同组件之间通信的载体。
Intent和IntentFilter

8.android的五大布局:LinearLayout(线性布局),TableLayout(表格布局),RelativeLayout(相对布局),FrameLayout(帧布局),AbsoluteLayout(绝对布局)

线性布局(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>

显示效果如下图:






9.重点总结的几个android的控件使用:列表组件、自定义组件、容器组件、对话框,菜单,信息提示。

一.列表组件(附加一个自己的Demo)

10.客户端与数据库之间的交互:android的网络应用
(使用的类或接口:java.net里面的
  ServerSocket (等待客户端连接的服务方套接字)/  socket(客户端的TCP套接字)
  DatagramSocket  (UDP套接字)/ DatagramPackage(数据包)
  IntetAddress Ip地址   UnkownHostException   位置主机异常
  URL   统一资源定位器    HttpURLConnection(管理Http连接)

通过http包工具进行通信,分get和post两种方式,两者的区别是: 
1,post请求发送数据到服务器端,而且数据放在html header中一起发送到服务器url,数据对用户不可见,get请求是把参数值加到url的队列中,这在一定程度上,体现出post的安全性要比get高 
2,get传送的数据量小,一般不能大于2kb,post传送的数据量大,一般默认为不受限制。 
一.基于TCP/Ip的网络连接(ServerSocket、Socket)、【 DatagramSocket  (UDP套接字)/ DatagramPackage(数据包)这个是UDP】

【DEMO简单实现一个c/s结构聊天室的功能】

先写服务器端:服务器端就是建立一个ServerSocket等待客户端的连接(一直处于阻塞状态)把成功加入的Socket连接都加入到ArrayList(定义一个保存所有的Socket),在服务器的线程主要的功能就是实现每个线程通信的线程类

public class SimpleServer {
//定义保存所有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;
}


}

客户端:第一文本输入,发送给服务器,然后用返回到客户端显示

public class MainActivity extends Activity {

    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();
}
}
});
}
}



客户端ClientThread

public class ClientThread implements Runnable {
//该线程负责处理的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();
}


}
}
0 0
原创粉丝点击