android学习1 android应用的基础

android的Application Components四个部分组成:

  1 )Activities

  2)  Services

  3 )Broadcast receivers

  4 )Content provider

1 Activities

      一个activity是一个可视化的用户界面,可以上面添加layout view button等。一个android由多个activity组成,各个activity之间相互独立。每一个activity实现都是Activity这个基类的子类。

2 Services

     service程序没有用户界面,但是可以长时间在后台运行,比如可以音乐的后台播放功能。   

3 Broadcast receivers

     Broadcast receivers 是一个用来接收响应外部事件消息(broadcast announcements 我翻译不好)的组件。Broadcast receivers没有用户界面,用户看不到它,但是它可以通过一个activity、震动、响铃、状态栏等提示用户有外部消息。所有的事件接收响应继承自BroadcastReceiver基类。

4 Content provider

     Content provider提供一组标准的方法接口,供其他应用程序调用。例如,应用程序A通过实现ContentProvider的接口将自己数据暴露出去,外部应用程序可以通过ContentProvider接口对A的数据进行操作(增删改查等)。

      android中数据是私有的不公开的(文件存储数据、SQLite数据库数据库、或者其他数据),通过Content provider可以将私有数据暴露出去。

Intent 和 Intent Filter(参考android入门开发与实践一书)

      一个Intent就是一次就将要执行操作的抽象描述(抽象啊)。Intent最重要的两个部分是Action(Intent的动作)和Data(Action对应的数据)。Action的类型有MAIN(Activity的门户)、VIEW、EDIT等等,Action对应的Data以URI形式描述。例如要查看某人联系方式,需要创建一个Action类型为view的Intent,以及一个表示此人的URI。Intent也可以进行Activity之间的跳转。

               Intent  in = new Intent(A.class, B.class);  startActivity(in);

               上面代码实现了Activity A 跳转到Activity B。

     Intent Filter用于描述一个Activity(或Intent Receiver)能操作哪些Intent。例如一个Activity要显示某人联系方式,需要声明一个Intent Filter,Intent Filter需要知道怎么处理view动作和表示某人的URI,Intent Filter一般在AndroidManifest.xml中定义。

官方关于Intents的文档说明,先记录,慢慢理解。

Activating components: intents

Content providers are activated when they're targeted by a request from a ContentResolver. The other three components — activities, services, and broadcast receivers — are activated by asynchronous messages called intents. An intent is an Intent object that holds the content of the message. For activities and services, it names the action being requested and specifies the URI of the data to act on, among other things. For example, it might convey a request for an activity to present an image to the user or let the user edit some text. For broadcast receivers, the Intent object names the action being announced. For example, it might announce to interested parties that the camera button has been pressed.

There are separate methods for activiating each type of component:

  • An activity is launched (or given something new to do) by passing an Intent object to Context.startActivity() or Activity.startActivityForResult(). The responding activity can look at the initial intent that caused it to be launched by calling its getIntent() method. Android calls the activity's onNewIntent() method to pass it any subsequent intents.

    One activity often starts the next one. If it expects a result back from the activity it's starting, it calls startActivityForResult() instead of startActivity(). For example, if it starts an activity that lets the user pick a photo, it might expect to be returned the chosen photo. The result is returned in an Intent object that's passed to the calling activity's onActivityResult()method.

  • A service is started (or new instructions are given to an ongoing service) by passing an Intent object to Context.startService(). Android calls the service's onStart() method and passes it the Intent object.

    Similarly, an intent can be passed to Context.bindService() to establish an ongoing connection between the calling component and a target service. The service receives the Intent object in an onBind() call. (If the service is not already running, bindService() can optionally start it.) For example, an activity might establish a connection with the music playback service mentioned earlier so that it can provide the user with the means (a user interface) for controlling the playback. The activity would call bindService() to set up that connection, and then call methods defined by the service to affect the playback.

    A later section, Remote procedure calls, has more details about binding to a service.

  • An application can initiate a broadcast by passing an Intent object to methods like Context.sendBroadcast(), Context.sendOrderedBroadcast(), and Context.sendStickyBroadcast() in any of their variations. Android delivers the intent to all interested broadcast receivers by calling their onReceive() methods.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值