Intents和Intent Filters

来源:互联网 发布:js 二进制流转字符串 编辑:程序博客网 时间:2024/06/05 20:04
转载请注明出处:http://blog.csdn.net/coder_jone/article/details/40044079

    Intent是一个你可以用来从其它应用组件请求一个动作(action)的消息传递对象,它们可以通过多种方式促进不同组件的联系,开发中主要用于以下三种:

一、启动一个界面(Activity)

    当我们有需要启动一个Activity用于用户界面展示时,需要通过Intent告知操作系统并指明要跳转的Activity的类名,操作系统再根据我们的意图(Intent)来跳转到指定的Activity,我们通过以下的方式来启动一个Activity,其中context为应用的上下文,XX为需要启动的Activity的类名:

Intent intent = new Intent(context, XX.class);startActivity(intent);

Intent在告知操作系统启动Activity的同时能携带单个数据:

Intent intent = new Intent(context, XX.class);intent.putExtra("extra_key", extra_value);startActivity(intent);

携带一组数据(也可以用下面的方式替代上面携带单个数据的方式)

Intent intent = new Intent(context, XX.class);Bundle bundle = new Bundle();bundle.putString("string_extra_key", "string_extra_value");bundle.putInt("int_extra_key", int_extra_value);...intent.putExtras(bundle);startActivity(intent);

通过上面的方式我们启动了一个Activity,那么我们要怎么获取到传递过来的数据呢?其实很简单,我们来看看获取数据的代码:

Intent intent = getIntent();String extraData = intent.getStringExtra("string_extra_key");

如果你是通过Bundle则可以通过下面的方式来获取数据:

Intent intent = getIntent();Bundle bundle = intent.getExtras();String stringExtraData = bundle.getStringExtra("string_extra_key");int intExtraData = bundle.getBoolean("int_extra_key");...

知道如何启动一个Activity后你可能还需要学会另外一种启动界面的方法,与日常开发中具体的需求对应:我们需要从A界面传递一些数据到B界面,经B界面做一定处理后返回A界面并带回处理完成的数据,那么你需要使用另外一个启动Activity的方法startActivityForResult(intent, requestCode),核心代码如下(其中REQUESTCODE为请求码,是一个大于等于0的整数,它用于区分不同的请求,因为我们可能一个界面通过startActivityForResult(intent, requestCode)启动多个界面做不同的事情,比如我们在一个用户资料设置的界面,而设置昵称和个性签名分别在两个不同的界面完成,那么我们需要启动两个不同的界面完成资料设置并返回给主界面显示):

Intent intent = new Intent(context, B.class);intent.putExtra("extra_key", extra_value);startActivityForResult(intent, REQUESTCODE);

B界面获取到传递的数据经处理点击结束按钮后需要将处理好的数据返回个A界面,具体代码如下:

Intent intent = new Intent();Bundle bundle = new Bundle();bundle.putString("str_extra_key", "string_extra_value");bundle.putInt("int_extra_key", int_extra_value);...intent.putExtras(bundle);setResult(RESULT_OK, intent);

   那么我们在A界面又要怎么来接收B界面传递来的数据呢?其实很简单,往往复杂的东西都被API底层封装好了我们只要知道怎么用就OK。我们需要做的是在A界面复写onActivityResult(int requestCode, int resultCode, Intent data)方法,看到了吧!回调接口已经将我们需要的参数都带上了,是不是很简单呢?你需要做的就是判断返回的是哪一个requestCode(也就是我们刚刚通过startActivityForResult(intent, REQUESTCODE)B界面中的REQUESTCODE),然后判断resultCode(注意:返回码另外一个作用是告知A界面你处理的结果,包括RESULT_CANCELED RESULT_OK、RESULT_FIRST_USER三个常量,你可以在B界面的setResult(resultCode, intent)中指定其中的一种,具体什么时候返回码与上面三种的哪一种对应大家可以自己去试试),判断完成后从intent中获取数据的方法与在B界面获取相同这里不再赘述。

二、启动服务(Service)

    服务(Service)与Activity同级,是Android四大基本组件之一,与Activity不同的是Service不是用于用户界面展示而是用来处理一些后台操作,如文件下载、音乐播放等。启动一个服务的方式有两种startService(Intent service)bindService (Intent service, ServiceConnection conn, int flags)

    首先我们来看看如何使用startService(Intent service)来启动一个服务并且传递一些数据,其中MyService是一个继承自Service的子类

Intent intent = new Intent(context, MyService.class);Bundle bundle = new Bundle();bundle.putString("string_extra_key", "string_extra_value");...intent.putExtras(bundle);startService(intent);

看了上面的代码是不是觉得启动一个服务跟启动一个界面差不多啊?是的,只是换了类名。

除了使用startService(Intent service)来启动一个服务外我们还可以通过bindService(Intent service, ServiceConnection conn, int flags)来启动一个服务,这会比前一种方式稍复杂些,具体怎么使用这里就不描述了,在后面更新的微博中将会详细介绍,这里我们主要来看看Intent在其中扮演的角色:

Intent intent = new Intent(context, MyService.class);bindService(intent, conn, Context.BIND_AUTO_CREATE);

Intent在里面扮演的是指定要启动的服务的类名及传递数据的携带。


三、发送广播(Broadcast

    广播是一个任何应用都能接收的消息,操作系统为系统事件发送多种广播,如系统重启或者设备开始充电等,你也可以通过Intent发送一个广播给其它应用或者应用组件,核心代码如下:

Intent intent = new Intent();intent.setAction(BROADCAST_ACTION);sendBroadcast(intent);

当然你也可以通过Intent传递一些数据,BroadcastReceiveronReceive(Context context, Intent intent)回调方法中可以取到这些数据。

    Android之所以能通过Intent能明确的知道要激活哪个Activity、Service、BroadcastReceiver全得益于Intent Filters,因为所有的Activity、Service、BroadcastReceiver都在AndroidManifest.xml中做了<intent-filter>...</intent-filter>配置,在里面可以指定action、categoryextraflags

    通过上面的学习主要是介绍了Intent在界面、服务、广播间交互所起的作用,在服务及广播部分写得比较简单,因为后面的微博会相继更新。


0 0
原创粉丝点击