anroid development part.1

44

Upload: rank-liu

Post on 13-May-2015

3.313 views

Category:

Education


9 download

DESCRIPTION

在公司内部讲解的一个andoid开发的ppt.

TRANSCRIPT

Page 2: Anroid development part.1
Page 3: Anroid development part.1

不仅仅是一部手机• 出色的操控性的用户体验• 华丽的界面• 出色的上网体验 safari

• 娱乐 - 游戏,音乐• market

• 非常多的应用程序• 相对成熟的操作系统• 相对成熟的商业体系

代表智能机的一种趋势

Page 4: Anroid development part.1

华丽的挣扎

• 历史悠久,使用人群基数非常大• 软件也是比较多• 在现在手机系统更新速度越来越快的今天,界面和操控无疑有点 out

• 即将退出历史舞台

Page 5: Anroid development part.1

• 优势在于手机上的 Office 协同软件• 更新速度不是一个慢字了得• 期待还在产房里的 Windows Phone 7

温室的花朵

Page 6: Anroid development part.1

• Google’s 开源的操作系统• 丰富的网络应用功能 Google map, youtube, search

• Google adsense for mobile

• Market 已经快超万• 系统更新速度快,目前 2.1

Page 7: Anroid development part.1

Scan barcode Goggles

Page 8: Anroid development part.1

• Architecture

• Application

• Activity

•Interaction

Page 9: Anroid development part.1
Page 10: Anroid development part.1
Page 11: Anroid development part.1

AndroidManifest.xml 是系统的控制文件,它告诉系统如何处理你所创建的所有 顶层组件 ( 尤其是 Activities, 服务 ,Intent 接收器和 ContentProvider) 。

Page 12: Anroid development part.1

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.myAndroidApp"

android:versionCode="1"

android:versionName="1.0">

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.RECORD_AUDIO"/>

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name=".myActivity"

android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<activity android:name="SettingActivity"></activity>

<activity android:name="SensorActivity"></activity>

</application>

<uses-sdk android:minSdkVersion="3" />

</manifest>

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.myAndroidApp"

android:versionCode="1"

android:versionName="1.0">

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.RECORD_AUDIO"/>

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name=".myActivity"

android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<activity android:name="SettingActivity"></activity>

<activity android:name="SensorActivity"></activity>

</application>

<uses-sdk android:minSdkVersion="3" />

</manifest>

Code Snippet

Page 13: Anroid development part.1

Drawable : png, .9.png, .jpg files 这些类型的文件被编译进下列这些图表资源列表Layouts :可编译成屏幕布局的 XML 文件,编译时, Android 产生一个叫 R 的类,它指向你程序中所有的资源。Values :可编译成多种类型资源的 XML 文件。例如多语言问题的解决(字符串),数字常量。Assets : Raw 格式文件。这里的任何文件都将直接被复制到设备上。编译产品时,这些数据不会被编译,它们被直接加入到程序包里。

Page 14: Anroid development part.1

• Activity 是最基本的 Andorid 应用程序组件,应用程序中,一个活动通常就是一个单独的屏幕。• 同时只可能有一个页面是活动 Activity

• 每个 Activity 之间可以通过 Intent 通迅• 每个 Activity 都有自己的生命周期

Page 15: Anroid development part.1

Lifecycle of an Activity

Page 16: Anroid development part.1
Page 17: Anroid development part.1
Page 18: Anroid development part.1

startActivity(new Intent("com.myAndroidApp.action.SETTING"));startActivity(new Intent("com.myAndroidApp.action.SETTING"));

Code Snippet

<activity android:name=".SettingActivity">

<intent-filter>

<action android:name="com.myAndroidApp.action.SETTING"/>

<category android:name="android.intent.category.DEFAULT"/>

</intent-filter>

</activity>

<activity android:name=".SettingActivity">

<intent-filter>

<action android:name="com.myAndroidApp.action.SETTING"/>

<category android:name="android.intent.category.DEFAULT"/>

</intent-filter>

</activity>

Intent intent = new Intent(Boy.class, Girl.class);startActivity(intent);Intent intent = new Intent(Boy.class, Girl.class);startActivity(intent);

一、

二、

Page 19: Anroid development part.1

关于 Intent—— 组件之间的联系Intent - 执行某操作的一个抽象描述。

Page 20: Anroid development part.1

显式 Intent 用于已有意向的情况例如从 Boy 跳转到 Girl 的 Activity

Intent intent = new Intent(Boy.class, Girl.class);startActivity(intent);Intent intent = new Intent(Boy.class, Girl.class);startActivity(intent);

方式一、非诚勿扰

BoyBoy GirlGirlIntentIntent

Page 21: Anroid development part.1

发布发布

存档存档

年 龄身 高体 重三 围

年 龄身 高体 重三 围

查找关键字查找关键字 合适合适 结婚结婚

方式二、征婚

Page 22: Anroid development part.1

发布发布

存档存档

Manifest 添加Intent-filterManifest 添加Intent-filter

The Intent-filter workflow

查找关键interfilte

r

查找关键interfilte

r

Intent找到最合适

Intent找到最合适

关联启动关联启动

隐式 Intent

Page 23: Anroid development part.1

<activity android:name=".SettingActivity"><intent-filter>

<action android:name="com.myAndroidApp.action.SETTING"/><category android:name="android.intent.category.DEFAULT"/>

</intent-filter></activity>

<activity android:name=".SettingActivity"><intent-filter>

<action android:name="com.myAndroidApp.action.SETTING"/><category android:name="android.intent.category.DEFAULT"/>

</intent-filter></activity>

Code Snippet

MainActivity.javaString SETTING_ACTION = "com.myAndroidApp.action.SETTING";Intent intent = new Intent();intent.setAction(SETTING_ACTION );startActivity(intent)

MainActivity.javaString SETTING_ACTION = "com.myAndroidApp.action.SETTING";Intent intent = new Intent();intent.setAction(SETTING_ACTION );startActivity(intent)

Page 24: Anroid development part.1

Intent-filter 匹配过程

Page 25: Anroid development part.1

Intent

Page 26: Anroid development part.1

• 一种以 KEY , VAL 方式存储的数据的类型,类似MAP 。

• 但不能传递复杂的数据类型,只能传递例如:字符串,数字型等类型。

在何时用?Intent 在各组件之间传递数据里就用 Bundle 类型来传递。

注:也可参见广播章里的代码。

Page 27: Anroid development part.1

视图 (Views) 可以将其自身绘制到屏幕上。Android 的用户界面由一系列的视图树 ( trees of views )构成。接口都是由一组以树的形式出现的视图组成的。开发者可 以通过创建一个新的视图的方法来使用自定义的图形处理技术 ( 比如开发游戏,或者是 使用了不常用的用户图形 (UI)窗口界面 (widget))

Layout 布局

视觉控件

Android UI

Page 28: Anroid development part.1

大概写了 20 个常用组件

Page 29: Anroid development part.1

• ContentProvider / ContentResolver / UriMatcher

• Broadcast / BroadcastReceiver / Notification

Page 30: Anroid development part.1

• Notification – 任务栏通知• Services – 后台服务• Broadcast – 广播• Broadcast Receiver – 广播接收器

注:不是所有的程序都必须有以下组件。

Page 31: Anroid development part.1

BroadcastBroadcast

Broadcast Receiver(get intent)Broadcast Receiver(get intent)

ServicesServicesActivitiesActivities

IntentIntent

BroadcastBroadcast

IntentIntent

组件 /

进程级的

Ob

server

广播

Page 32: Anroid development part.1

1. 添加 Intent-filter – manifest.xml

2. 添加广播数据 Bundle实例到 intent

3. 广播到某个组件 sendBroadcast(intent)

4. 组件接收广播 (该组件继承自 BroadcastReceiver)

onReceive(Context context, Intent intent)

5. 通知到任务栏 notification

Bundle —— 理解成 Hash Map ,但只允许存成基本的数据类型对,例如String , int 。

Page 33: Anroid development part.1

Manifest.xml<receiver android:name="Receiver">

<intent-filter> <!-- 和 Intent 中的 action 对应 --> <action android:name="com.myAndroidApp.action.Receiver"/> </intent-filter>

</receiver>

Manifest.xml<receiver android:name="Receiver">

<intent-filter> <!-- 和 Intent 中的 action 对应 --> <action android:name="com.myAndroidApp.action.Receiver"/> </intent-filter>

</receiver>

Code Snippet

MainActivity.javaIntent intent = new Intent();intent.setAction(RECEIVER_ACTION);Bundle buddle = new Bundle();buddle.putString("title", "Email");buddle.putString("content","ranklau");intent.putExtras(buddle);sendBroadcast(intent);

MainActivity.javaIntent intent = new Intent();intent.setAction(RECEIVER_ACTION);Bundle buddle = new Bundle();buddle.putString("title", "Email");buddle.putString("content","ranklau");intent.putExtras(buddle);sendBroadcast(intent);

Receiver.javapublic class Receiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); String title = bundle.getString("title"); String content = bundle.getString("content"); Notification notification = new Notification(R.drawable.icon1, "New notification", System.currentTimeMillis()); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0); notification.setLatestEventInfo(context, title, content, contentIntent); NotificationManager notificationManager = (NotificationManager) context.getSystemService(android.content.Context.NOTIFICATION_SERVICE); notificationManager.notify(NOTIFICATION_ID, notification); }}

Receiver.javapublic class Receiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); String title = bundle.getString("title"); String content = bundle.getString("content"); Notification notification = new Notification(R.drawable.icon1, "New notification", System.currentTimeMillis()); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0); notification.setLatestEventInfo(context, title, content, contentIntent); NotificationManager notificationManager = (NotificationManager) context.getSystemService(android.content.Context.NOTIFICATION_SERVICE); notificationManager.notify(NOTIFICATION_ID, notification); }}

Page 34: Anroid development part.1

Difference of startService and bindService

Page 35: Anroid development part.1
Page 36: Anroid development part.1

Query

Delete

Update

Insert

程序间 数据访问 操作?

Page 37: Anroid development part.1

例如使用内容管理器 (ContentProvider) 来访问联系人列表。你的应用程序也可以使用其它程序通过内容管理器提 供的数据,同时你也可以定义你自己的内容管理器来向其它应用提供数据访问服务。

注意,它是同步执行的。

Page 38: Anroid development part.1

A :标准前缀,用来说明一个 Content Provider 控制这些数据,无法改变的;B : URI 的标识,它定义了是哪个 Content Provider提供这些数据。对于第三方应用程序,为了保证URI标识的唯一性,它必须是一个完整的、小写的 类名。这个标识在 <provider> 元素的 authorities属性中说明:<provider name=”.TransportationProvider”

authorities=”com.example.transportationprovider” . . . >

C :路径, Content Provider 使用这些路径来确定当前需要生什么类型的数据, URI 中可能不包括路径,也可能包括多个;D :如果URI 中包含,表示需要获取的记录的 ID;如果没有 ID ,就表示返回全部;由于 URI 通常比较长,而且有时候容易出错,切难以理解。所以,在 Android当中定义了一些辅助类,并且定义了一些常量来代替这些长字符串,例如: People.CONTENT_URI

What is the URI?

Page 39: Anroid development part.1

• 访问一致性,即接口描述已经出现成的了, URI 已经描述。

• 天生具有权限控制(例如 FTP , HTTP , HTTPS 协议等等)

但是, URI传递不能数据类型就比较麻烦,所以。我想Android 通过 bundle 作参数来传递复杂参数

Page 40: Anroid development part.1

•content://media/internal/images

这个 URI 将返回设备上存储的所有图片•content://contacts/people/

这个 URI 将返回设备上的所有联系人信息•content://contacts/people/45

这个 URI返回单个结果(联系人信息中 ID 为 45 的联系人记录)

•Intent intent= new Intent(Intent.DIAL_ACTION,Uri.parse("tel:5551212"));

•startActivity(this.class,intent) //启动拨号程序。

Page 41: Anroid development part.1

•ContentProvider + UriMatcher

•ContentResolver ( Activity 方法)

Page 42: Anroid development part.1

好象是和 COM很相似…

Page 44: Anroid development part.1