Android入门

来源:互联网 发布:淘宝电动车专卖店 编辑:程序博客网 时间:2024/06/05 06:30

Android

简介

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。
2003年10月,Andy Rubin等人创建Android公司,并组建Android团队。
2005年8月17日,Google低调收购了成立仅22个月的高科技企业Android及其团队。安迪鲁宾成为Google公司工程部副总裁,继续负责Android项目。
2007年11月5日,谷歌公司正式向外界展示了这款名为Android的操作系统。

系统框架

这里写图片描述

搭建Android开发环境

Eclipse环境

  1. Eclipse
  2. Android SDK
  3. ADT(Android Developer Tools)

Android Studio环境

  1. Android Studio
  2. Android SDK

问题

在用Eclipse时,出现“ADB server didn’t ACK * failed to start daemon ”的问题,下面给出解决办法。
1. adb.exe使用的端口号为5037,在cmd命令行中输入net -aon|findstr “5037”,找到占用该端口号的进程的进程号PID。
2. 根据该PID,找到该进程的名称,输入tasklist|findstr “PID”,找到该进程的名称name。
3. 结束该进程,输入taskkill /f /t /im name,重启就好了!

我的第一个Android程序

以Eclipse为例,创建HelloWorld项目
File->New->Android Application Project,创建Android项目,Application Name填入Hello World,即应用名称。Project Name代表项目名称,填入HelloWorld。Packet Name 代表包名,Android系统是通过包名区分不同的应用程序,因此包名一定要唯一,填入com.test.helloworld。后边默认配置,即创建好HelloWorld项目。
① AndroidManifest.xml

<activity    android:name="com.test.helloworld.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>

注意
1. 所有的活动都要在AndroidManifest.xml中注册才能生效。
2. intent-filter中的内容表示该活动为这个项目的主活动。
②MainActivity

public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.activity_main, menu);        return true;    } }
  1. Activity是Android系统提供的一个活动基类,项目中所有的活动都必须继承 Activity。
  2. onCreate()方法是一个活动被创建必须执行的方法。
    ③activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:text="@string/hello_world" /></RelativeLayout>

Android项目中的所有资源都方法res文件夹下面:
1. 在代码中通过R.string.hello_world可以获得该字符串的引用。
2. 在XML中通过@string/hello_world可以获得该字符串的引用。

日志工具

Android中的日志工具类Log(android.util.Log),这个类提供了以下几个方法供我们打印日志。
1. Log.v()
这个方法用于打印最为琐碎的,意义最小的日志信息。对应级别verbose。
2. Log.d()
这个方法用于打印一些调试信息,这些信息对于调试程序和分析问题是有很大帮助的。对应级别debug。
3. Log.i()
这个方法用于打印一些比较重要的数据,可以帮助分析用户行为的数据。对应级别info。
4. Log.w()
这个方法用于打印一些警告信息,提示程序在这个地方可能会有潜在的风险。对应级别warn。
5. Log.e()
这个方法用于打印程序中的错误信息。对应级别error。
注意:方法中传入两个参数,第一个参数tag,一般传入当前的类名就好,主要用于对打印信息的过滤。第二个参数msg,即想要打印的具体的内容。

1 0
原创粉丝点击