您的位置:首页 > 其它

读安卓开发者文档笔记

2016-04-05 12:02 162 查看
1、每一个app生存在自己的沙盒中:安卓操作系统是一个多用户Linux系统,每一个app视作一个用户;默认,系统为每一个app分配一个userID,只有系统知道;默认,每个app有自己的Linux处理,在自己的VM(virtual machine)中,所以它们是相互独立的。Android is a privilege-separated operating system, in which each application runs with a distinct system identity (Linux
user ID and group ID).
2、四大组件:activity,service:在后台运行,没有UI,如网络连接,content-providers:管理app共享数据,管理app自己的file system或者手机的file,broadcast-receivers:系统/apps能发起广播,例如,系统发起低电关机广播,apps发起文件下载广播。通常,其没有UI,可能会有一个status bar做通知用。通常,其只是一个其他组件的网关。
3、安卓系统设计一大特点:任何app都能开启其他app的组件,如打开相册,打开短信等。安卓app没有一个简单的入口(没有main函数)。app必须向系统发送指定激活哪一个组件的intent,然后系统才会为app激活组件。
4、Intent可以激活activity、service和broadcast-receiver:
startActivity()  or startActivityForResult()

startActivityForResult():被调用的活动B会向其调用者A返回信息,B用setResult方法设定返回信息,A用onActivityResult方法得到返回的信息,(该onActivityResult的方法在上一个activity结束后,本activity  onStart之后,onResume之前)
setResult(int resultCode)   setResult(int resultCode, Intent data)
onActivityResult(int requestCode, int resultCode, Intent data)
resultCode:  RESULT_CANCELED, RESULT_OK, RESULT_FIRST_USER....或者用户自定义

startService()  or bindService()
sendBroadcast(), sendOrderedBroadcast(), or sendStickyBroadcast().
5、contentResolver能够处理所有和contentProvider相关的事务
6、AndroidManifest.xml确定app需要的权限,声明适用的最小API,声明app需要的硬件和软件特性(如蓝牙服务,多点触控等),app需要的API库,等等。
声明四大组件。
声明组件能力:凡是在其他app中有创建一个待用ACTION_SEND的intent,就会激活该对应的activity。
<intent-filter>

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

                <data android:type="*/*" />

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

</intent-filter>
7、app的资源与代码分离,如animations,menus,styles,colors,layout等xml文件,使我们能够轻易的改变资源,优化app设置,如设定不同语言、屏幕宽度。
8、关于permission:
【如果需要permission is missing,则系统报错为SecurityException】

Caution: Over time, new restrictions may be added to the platform such that, in order to use certain APIs, your app must request a permission that it previously did not need. Because existing apps assume access to those APIs is freely available,
Android may apply the new permission request to the app's manifest to avoid breaking the app on the new platform version. Android makes the decision as to whether an app might need the permission based on the value provided for the 
targetSdkVersion
 attribute.
If the value is lower than the version in which the permission was added, then Android adds the permission.

For example, the 
WRITE_EXTERNAL_STORAGE
 permission
was added in API level 4 to restrict access to the shared storage space. If your 
targetSdkVersion
 is
3 or lower, this permission is added to your app on newer versions of Android.

Beware that if this happens to your app, your app listing on Google Play will show these required permissions even though your app might not actually require them.
To avoid this and remove the default permissions you don't need, always update your 
targetSdkVersion
 to
be as high as possible. You can see which permissions were added with each release in the 
Build.VERSION_CODES
documentation.

More:  应用程序还可以自定义一个特殊的permission,当其他应用需要与其进行交互时,必须先申请这个权限。
在AndroidManifest.xml文件中,自定义permission用<permission>
申请权限,在<uses-permission>中,

Service Permissions:限制哪些元素可以使用服务。
在Context.startService(),Context.stopService(),Context.bindService()时,检测权限。

Broadcast Permissions:限制哪些元素可以发送和接收广播。

Content-provider Permissions

9、设备兼容性:安卓is designed to 在不同类型的设备上运行,手机、平板甚至电视。
不同的xml布局文件适应于不同尺寸的屏幕。安卓系统会基于本地设备的配置来加载合适的资源。
10、支持不同的设备:

语言:res-> values ->strings.xml   res-> values-es ->strings.xml  res-> values-fr ->strings.xml。调用@string/name
屏幕尺寸:大小(small, normal, large, xlarge)和密度(ldpi, mdpi, hdpi, xhdpi)。创建不同大小的图片库,res -> drawable-xhdpi -> XXX.png。 调用@drawable/name。 注意,ldpi通常不需要指定,系统会自动根据hdpi的图片,减半来适应。
横竖屏状态:layout->默认竖屏,layout-land ->
版本:

AndroidMainifest.xml文件中,<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15" />
【安卓系统会自动忽略当前版本不支持的xml中属性,所以使用起来是安全的】在运行的时候检查API是否支持,如ActionBar,支持android3.0以上(API11),例如:
Styles和Themes:可以借用安卓的库,使app始终有最新的look;或者自定义样式。

private void setUpActionBar() {
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
}


 
11、

a. Defining resources
layout, string, pictures, animation

b. implements those activity classes

12、Activity方法实现细节






onCreate(Bundle savedInstanceState) 方法中实现的东西包括:

恢复现场,restore saved state
设置layout,setContentView
初始化View,initView
设定UI元素的动作,link UI elements to code actions

onStart()实现细节:

下载应用状态(如拉取邮件更新)

onStop() ,onDestroy() 可能不会被调用
所以,如果要保存状态,应该在onPause()方法中实现。

13、配置信息可以在运行过程中改变,如keyboard、横竖屏、ScreenSize等等配置。配置一旦被修改,系统会杀死现在的Activity,再重启以加载合适的资源。
针对配置被修改的情况,两种加速加载的方式:
13.1 retain重要的配置信息:
     重写onRetainNonConfigurationInstance(),在onCreate()中调用getLastNonConfigurationInstance()。【此方法Fragment不支持】
13.2 手动处理配置改变,来避免重启:
     在声明文件中android:configeChanges字段添加可以处理的修改。
     

PS:推荐一个免费的论文查重网站PaperFree:http://www.paperfree.cn
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: