android之Intent

来源:互联网 发布:怎么获得淘宝代金券 编辑:程序博客网 时间:2024/05/18 20:12
Intent是一个消息对象你可以用来去请求一个来自另外一个app component的action。虽然Intent便于在component之间通信,,到那时有三种基本的用例


1:开启一个activity
a:startActivity
通过传递一个Intent给startActivity()函数,开启一个Activity的新的实例,。Intent中描述新开启的Activity以及带有一些相关的数据
b:startActivityForResult
开启一个新的Activity,当新开启的Activity结束后,返回一定的数据到原始的Activity。


2:开启一个service
service是不带用户界面的在背地里运行的组件,比如说下载文件,播放音乐。
a: startService
开启一个服务来执行一次操作,传递Intent给startService()函数
b:bindService
如果service被设计成服务器-客户端的接口模式,可以从另外一个component绑定一个service。


3:传递一个broadcast
broadcast是一个任何app都可以接受的message。可以通过sendBroadcast(),sendOrderedBroadcast(),sendStickyBroadcast()来给其他app传递broadcast。


Intent的分类
1:显式的Intent
通过指定一个component的全名来指定开启的Intent。
2:隐式的Intent
不指定一个component的名字,只声明它要做的action。
当创建一个隐式的Intent的时候,系统会通过比较manifest中的Intent-filter和Intent的内容,如果Intent的内容和Intent-filter中的相互匹配,系统就会开启那个component来传递Intent,如果有多个Intent-filter匹配的话,系统就会弹出一个对话框让调用者来选择。


Intent负载的信息
Intent上面负载着系统决定启动哪一个component(component的名字,component的category),以及接受的组件需要是用的信息等。
1:Component name
要启动的component的名字,这个是可选的,对于显式的就用名字,隐式的可以通过action,category,data等来判断启动哪一个component。
2:Action
指定要执行的动作
3:data
URI,或者MIME格式的对象。data是由Intent的Action决定的,如果Action是一个ACTION_EDIT,那么data就应当包含编辑文档的URI。
当创建一个Intent的时候,它往往是重要的去指定data的类型。指定data为MIME格式能够辅助系统找到最合适的component来接受Intent,但是MIME有时候可以从URI中推断出来,特别当data是一个content的时候。
setData(),setType(),setDataAndType来设置数据的格式。
4:category
给一个component提供附加信息,一个Intent中可以放置任意数目的category,但是大部分Intent并不需要一个category。
5:extras
就像一些action使用uri,一些action要使用extras来给Intent的action提供附加信息。
6:Flags
这个主要是处理指明系统如何去加载一个activity,例如把新启动的activity加载到哪一个Task中,它加载完毕后怎么处理它。
Intent例子
1:显式调用
// Executed in an Activity, so 'this' is the Context
// The fileUrl is a string URL, such as "http://www.example.com/image.png"
Intent downloadIntent = new Intent(this, DownloadService.class);
downloadIntent.setData(Uri.parse(fileUrl));
startService(downloadIntent);
2:隐式调用
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);
sendIntent.setType(HTTP.PLAIN_TEXT_TYPE); // "text/plain" MIME type


// Verify that the intent will resolve to an activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(sendIntent);
}


3:隐式调用,chooser
Intent intent = new Intent(Intent.ACTION_SEND);
...


// Always use string resources for UI text.
// This says something like "Share this photo with"
String title = getResources().getString(R.string.chooser_title);
// Create intent to show chooser
Intent chooser = Intent.createChooser(intent, title);


// Verify the intent will resolve to at least one activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(sendIntent);
}


4:manifest中设置intent-filter来选择action
它格式是
action
data
category
如果接收的Intent是隐式的,那么必须包含 CATEGORY_DEFAULT category
<activity android:name="ShareActivity">
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="text/plain"/>
    </intent-filter>
</activity>


5:Intent-filter中选择实例
<activity android:name="MainActivity">
    <!-- This activity is the main entry, should appear in app launcher -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>


<activity android:name="ShareActivity">
    <!-- This activity handles "SEND" actions with text data -->
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="text/plain"/>
    </intent-filter>
    <!-- This activity also handles "SEND" and "SEND_MULTIPLE" with media data -->
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <action android:name="android.intent.action.SEND_MULTIPLE"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="application/vnd.google.panorama360+jpg"/>
        <data android:mimeType="image/*"/>
        <data android:mimeType="video/*"/>
    </intent-filter>
</activity>


其中
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
这里的 ACTION_MAIN action和CATEGORY_LAUNCHER category必须成对结合起来。
在ShareActivity中,两个Intent-filter提供了关于send的action,当要share text的时候,这两个都合适。
0 0
原创粉丝点击