android <intent>到底是个什么东西

从别的地方转的 感觉写得很好 想要收藏一下,说不定哪天有用。

网上查看了很多资料,看完了发现还是是懂非懂,没办法还是自己看了一遍android sdk帮助文档,现在把关键点列出来记一下。
intent
intent 是android 中三大组件 activities, services, and broadcast receivers之间通信的桥梁。可以在应用运行期间在不同的组件之间传递消息,也可以在不同的应用之间传递消息。
一个intent对象用来描述当前的操作想要处理的数据,或者用来在广播时描述当前发生的事情。
启动一个服务:
        startService(new Intent("tomlee.myAndroidPro.MUSICSERVER"));
启动一个窗口:        
        Intent intent = new Intent();
        intent.setClass(LoginActivity.this, SeePaoPao.class);
        startActivity(intent);
        传递参数到另一个Activity:
        Intent intent = new Intent();        
        intent.putExtra("musicVolume", MainMenu.this.musicVolume);
        intent.putExtra("soundVolume", MainMenu.this.soundVolume);
        intent.setClass(MainMenu.this, SubActivity.class);        
        startActivity(intent);
        接收该intent中参数的方法:
        Intent intent=getIntent(); 
        this.musicVolume=intent.getIntExtra("musicVolume", 0); 
        this.soundVolume=intent.getIntExtra("soundVolume", 0); 
监听广播:
        public class DeviceBootReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {}
        }

NOTE:
上面三种用法中的每种intent传递方式只会传递给相应的组件。
如果是使用startActivity(),则intent只会被传递给Activity
如果是使用startService(),则intent只会传递给service
如果是广播消息的intent则只会被注册过广播事件回调的组件接收到。


intent结构中重要的几个属性字段:
Action:用来说明当前这个Activity可以处理的事情

<action android:name="android.intent.action.MAIN" />,主入口
<action android:name="android.intent.action.DIAL" /> 给指定的号码拨电话 <action android:name="android.intent.action.CALL_BUTTON" />当按下呼叫按键时启动拨号界面或者其它可用的拨号界面 android:name="android.intent.action.MAIN" />,主入口
<action android:name="android.intent.action.DIAL" /> 给指定的号码拨电话 <action android:name="android.intent.action.CALL_BUTTON" />当按下呼叫按键时启动拨号界面或者其它可用的拨号界面


category:当前组件的附加信息:
                <category android:name="android.intent.category.LAUNCHER" /> 说明是当前应用首个加载的画面
                <category android:name="android.intent.action.CREATE_SHORTCUT" />创建快捷方式
data:一般会跟action中定义的功能对应,即某种功能对应的数据路径。
当我们要做编辑时编辑的数据是什么在哪里,要查看时数据的路径是什么。


可以在使用intent时赋值也可以在xml中定义。

 

AndroidManifest.xml中定义intent-filter时的用法:

把sdk中的文档搞过来,写的比较细可以在记不大清楚时再回顾下,比较方便点:
For example, consider the Note Pad sample application that allows user to browse through a list of notes data and view details about individual items. Text in italics indicate places were you would replace a name with one specific to your own package.

 

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"        package="com.android.notepad">      <application android:icon="@drawable/app_notes"              android:label="@string/app_name">           <provider class=".NotePadProvider"                  android:authorities="com.google.provider.NotePad" />           <activity class=".NotesList" android:label="@string/title_notes_list">              <intent-filter>                  <action android:name="android.intent.action.MAIN" />                  <category android:name="android.intent.category.LAUNCHER" />              </intent-filter>              <intent-filter>                  <action android:name="android.intent.action.VIEW" />                  <action android:name="android.intent.action.EDIT" />                  <action android:name="android.intent.action.PICK" />                  <category android:name="android.intent.category.DEFAULT" />                  <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />              </intent-filter>              <intent-filter>                  <action android:name="android.intent.action.GET_CONTENT" />                  <category android:name="android.intent.category.DEFAULT" />                  <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />              </intent-filter>          </activity>           <activity class=".NoteEditor" android:label="@string/title_note">              <intent-filter android:label="@string/resolve_edit">                  <action android:name="android.intent.action.VIEW" />                  <action android:name="android.intent.action.EDIT" />                  <category android:name="android.intent.category.DEFAULT" />                  <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />              </intent-filter>               <intent-filter>                  <action android:name="android.intent.action.INSERT" />                  <category android:name="android.intent.category.DEFAULT" />                  <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />              </intent-filter>           </activity>           <activity class=".TitleEditor" android:label="@string/title_edit_title"                  android:theme="@android:style/Theme.Dialog">              <intent-filter android:label="@string/resolve_title">                  <action android:name="com.android.notepad.action.EDIT_TITLE" />                  <category android:name="android.intent.category.DEFAULT" />                  <category android:name="android.intent.category.ALTERNATIVE" />                  <category android:name="android.intent.category.SELECTED_ALTERNATIVE" />                  <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />              </intent-filter>          </activity>       </application>  </manifest><manifest xmlns:android="http://schemas.android.com/apk/res/android"        package="com.android.notepad">      <application android:icon="@drawable/app_notes"              android:label="@string/app_name">           <provider class=".NotePadProvider"                  android:authorities="com.google.provider.NotePad" />           <activity class=".NotesList" android:label="@string/title_notes_list">              <intent-filter>                  <action android:name="android.intent.action.MAIN" />                  <category android:name="android.intent.category.LAUNCHER" />              </intent-filter>              <intent-filter>                  <action android:name="android.intent.action.VIEW" />                  <action android:name="android.intent.action.EDIT" />                  <action android:name="android.intent.action.PICK" />                  <category android:name="android.intent.category.DEFAULT" />                  <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />              </intent-filter>              <intent-filter>                  <action android:name="android.intent.action.GET_CONTENT" />                  <category android:name="android.intent.category.DEFAULT" />                  <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />              </intent-filter>          </activity>           <activity class=".NoteEditor" android:label="@string/title_note">              <intent-filter android:label="@string/resolve_edit">                  <action android:name="android.intent.action.VIEW" />                  <action android:name="android.intent.action.EDIT" />                  <category android:name="android.intent.category.DEFAULT" />                  <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />              </intent-filter>               <intent-filter>                  <action android:name="android.intent.action.INSERT" />                  <category android:name="android.intent.category.DEFAULT" />                  <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />              </intent-filter>           </activity>           <activity class=".TitleEditor" android:label="@string/title_edit_title"                  android:theme="@android:style/Theme.Dialog">              <intent-filter android:label="@string/resolve_title">                  <action android:name="com.android.notepad.action.EDIT_TITLE" />                  <category android:name="android.intent.category.DEFAULT" />                  <category android:name="android.intent.category.ALTERNATIVE" />                  <category android:name="android.intent.category.SELECTED_ALTERNATIVE" />                  <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />              </intent-filter>          </activity>       </application>  </manifest>

 

The first activity, com.android.notepad.NotesList, serves as our main entry into the app. It can do three things as described by its three intent templates:

  1.  <intent-filter>      <action android:name="android.intent.action.MAIN" />      <category android:name="android.intent.category.LAUNCHER" />  </intent-filter>

    This provides a top-level entry into the NotePad application: the standard MAIN action is a main entry point (not requiring any other information in the Intent), and the LAUNCHER category says that this entry point should be listed in the application launcher.

  2.  <intent-filter>      <action android:name="android.intent.action.VIEW" />      <action android:name="android.intent.action.EDIT" />      <action android:name="android.intent.action.PICK" />      <category android:name="android.intent.category.DEFAULT" />      <data mimeType:name="vnd.android.cursor.dir/vnd.google.note" />  </intent-filter>

    This declares the things that the activity can do on a directory of notes. The type being supported is given with the <type> tag, wherevnd.android.cursor.dir/vnd.google.note is a URI from which a Cursor of zero or more items (vnd.android.cursor.dir) can be retrieved which holds our note pad data (vnd.google.note). The activity allows the user to view or edit the directory of data (via the VIEW and EDIT actions), or to pick a particular note and return it to the caller (via the PICK action). Note also the DEFAULT category supplied here: this is required for the Context.startActivity method to resolve your activity when its component name is not explicitly specified.

  3.  <intent-filter>      <action android:name="android.intent.action.GET_CONTENT" />      <category android:name="android.intent.category.DEFAULT" />      <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />  </intent-filter>

    This filter describes the ability return to the caller a note selected by the user without needing to know where it came from. The data typevnd.android.cursor.item/vnd.google.note is a URI from which a Cursor of exactly one (vnd.android.cursor.item) item can be retrieved which contains our note pad data (vnd.google.note). The GET_CONTENT action is similar to the PICK action, where the activity will return to its caller a piece of data selected by the user. Here, however, the caller specifies the type of data they desire instead of the type of data the user will be picking from.

Given these capabilities, the following intents will resolve to the NotesList activity:

  • { action=android.app.action.MAIN } matches all of the activities that can be used as top-level entry points into an application.

  • { action=android.app.action.MAIN, category=android.app.category.LAUNCHER } is the actual intent used by the Launcher to populate its top-level list.

  • { action=android.intent.action.VIEW data=content://com.google.provider.NotePad/notes } displays a list of all the notes under "content://com.google.provider.NotePad/notes", which the user can browse through and see the details on.

  • { action=android.app.action.PICK data=content://com.google.provider.NotePad/notes } provides a list of the notes under "content://com.google.provider.NotePad/notes", from which the user can pick a note whose data URL is returned back to the caller.

  • { action=android.app.action.GET_CONTENT type=vnd.android.cursor.item/vnd.google.note } is similar to the pick action, but allows the caller to specify the kind of data they want back so that the system can find the appropriate activity to pick something of that data type.

The second activity, com.android.notepad.NoteEditor, shows the user a single note entry and allows them to edit it. It can do two things as described by its two intent templates:

  1.  <intent-filter android:label="@string/resolve_edit">      <action android:name="android.intent.action.VIEW" />      <action android:name="android.intent.action.EDIT" />      <category android:name="android.intent.category.DEFAULT" />      <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />  </intent-filter>

    The first, primary, purpose of this activity is to let the user interact with a single note, as decribed by the MIME type vnd.android.cursor.item/vnd.google.note. The activity can either VIEW a note or allow the user to EDIT it. Again we support the DEFAULT category to allow the activity to be launched without explicitly specifying its component.

  2.  <intent-filter>      <action android:name="android.intent.action.INSERT" />      <category android:name="android.intent.category.DEFAULT" />      <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />  </intent-filter>

    The secondary use of this activity is to insert a new note entry into an existing directory of notes. This is used when the user creates a new note: the INSERT action is executed on the directory of notes, causing this activity to run and have the user create the new note data which it then adds to the content provider.

Given these capabilities, the following intents will resolve to the NoteEditor activity:

  • { action=android.intent.action.VIEW data=content://com.google.provider.NotePad/notes/{ID} } shows the user the content of note {ID}.

  • { action=android.app.action.EDIT data=content://com.google.provider.NotePad/notes/{ID} } allows the user to edit the content of note {ID}.

  • { action=android.app.action.INSERT data=content://com.google.provider.NotePad/notes } creates a new, empty note in the notes list at "content://com.google.provider.NotePad/notes" and allows the user to edit it. If they keep their changes, the URI of the newly created note is returned to the caller.

The last activity, com.android.notepad.TitleEditor, allows the user to edit the title of a note. This could be implemented as a class that the application directly invokes (by explicitly setting its component in the Intent), but here we show a way you can publish alternative operations on existing data:

 <intent-filter android:label="@string/resolve_title">      <action android:name="com.android.notepad.action.EDIT_TITLE" />      <category android:name="android.intent.category.DEFAULT" />      <category android:name="android.intent.category.ALTERNATIVE" />      <category android:name="android.intent.category.SELECTED_ALTERNATIVE" />      <data android:mimeType="vnd.android.cursor.item/vnd.google.note" />  </intent-filter>

In the single intent template here, we have created our own private action calledcom.android.notepad.action.EDIT_TITLE which means to edit the title of a note. It must be invoked on a specific note (data type vnd.android.cursor.item/vnd.google.note) like the previous view and edit actions, but here displays and edits the title contained in the note data.

In addition to supporting the default category as usual, our title editor also supports two other standard categories: ALTERNATIVE and SELECTED_ALTERNATIVE. Implementing these categories allows others to find the special action it provides without directly knowing about it, through the queryIntentActivityOptions(ComponentName, Intent[], Intent, int)method, or more often to build dynamic menu items with addIntentOptions(int, int, int, ComponentName, Intent[], Intent, int, MenuItem[]). Note that in the intent template here was also supply an explicit name for the template (viaandroid:label="@string/resolve_title") to better control what the user sees when presented with this activity as an alternative action to the data they are viewing.

Given these capabilities, the following intent will resolve to the TitleEditor activity:

  • { action=com.android.notepad.action.EDIT_TITLE data=content://com.google.provider.NotePad/notes/{ID} } displays and allows the user to edit the title associated with note {ID}.

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值