StupidBeauty
Read times:2362Posted at: - no title specified

安卓开发文档翻译:接收来自其它应用的简单数据,Receiving Simple Data from Other Apps

妳的应用程序可以向其它应用程序发送数据,同样地, 它也可以轻易地 从其它应用程序接收数据。考虑 一下,用户会如何地与妳的应用程序交互, 以及, 妳需要从其它应用程序中接收什么数据。例如, 一个社交网络应用程序,可能想要从其它应用程序接收文本内容 ,比如说有意思的网页地址。 Google+ 这个安卓应用 会接收文本、单张图片或多张图片。利用 这个应用,用户就能够轻易地从安卓图库应用中发起一个新的带图片的 Google+消息

更新妳的清单文件

意图过滤 器,形成 了一套体系,使得特定 的应用程序能够表达自己 想要接收的意图。 向其它应用发送简单数据 课程中,我们讲述了,如何构造 一个带有 ACTION_SEND 动作的意图。 类似地,妳需要创建意图过滤器,以便接收 带有这个动作的意图。 妳可以在清单文件中使用 <intent-filter> 元素来定义一个意图过滤器。例如,假设 ,妳的应用程序能够接收文本内容 、任意类型 的单张图片或多张图片,则,清单文件中会有这样一砣:

<activity android:name = ".ui.MyActivity" >

<intent-filter>

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

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

<data android:mimeType = "image/*" />

</intent-filter>

<intent-filter>

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

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

<data android:mimeType = "text/plain" />

</intent-filter>

<intent-filter>

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

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

<data android:mimeType = "image/*" />

</intent-filter>

</activity>

注意 欲了解更多关于意图过滤器和意图解析的信息,则阅读 意图 和意图过滤器

每当 有另一个应用程序构造 了一个意图,并且将它传递给 startActivity() ,以尝试分享任何 此种类型的内容时, 妳的应用程序就会被 列于意图选择 器中,作为其中的一个选项。如果用户选择 了妳的应用程序,则,对应 的活动 ( 此例子中即是 .ui.MyActivity ) 会被启动。之后,对于 该内容的处理,就看妳的了。

处理传入的内容

要想处理由 Intent 传递过来的内容,则,首先调用 getIntent() 以获取到 Intent 对象。获取 到该对象之后, 就可以检查其内容,以决定下一步该做什么。注意 ,如果 这个活动也可以被系统的其它部分启动,例如应用启动器,则, 妳需要 在检查意图时考虑到这一点。

void onCreate ( Bundle savedInstanceState ) {

...

// 获取意图 、动作及多媒体(MIME)类型

Intent intent = getIntent ();

String action = intent . getAction ();

String type = intent . getType ();

if ( Intent . ACTION_SEND . equals ( action ) && type != null ) {

if ( "text/plain" . equals ( type )) {

handleSendText ( intent ); // 处理 要发送的文本内容

} else if ( type . startsWith ( "image/" )) {

handleSendImage ( intent ); // 处 理要发送的单张图片

}

} else if ( Intent . ACTION_SEND_MULTIPLE . equals ( action ) && type != null ) {

if ( type . startsWith ( "image/" )) {

handleSendMultipleImages ( intent ); // 处理 要发送的多张图片

}

} else {

// 处理其它意图,例如 ,由主屏幕启动时产生的意图

}

...

}

void handleSendText ( Intent intent ) {

String sharedText = intent . getStringExtra ( Intent . EXTRA_TEXT );

if ( sharedText != null ) {

// 更新界面 ,以反映出当前在分享文本内容

}

}

void handleSendImage ( Intent intent ) {

Uri imageUri = ( Uri ) intent . getParcelableExtra ( Intent . EXTRA_STREAM );

if ( imageUri != null ) {

// 更新界面 ,以反映出当前在分享 单张图片

}

}

void handleSendMultipleImages ( Intent intent ) {

ArrayList < Uri > imageUris = intent . getParcelableArrayListExtra ( Intent . EXTRA_STREAM );

if ( imageUris != null ) {

// 更新界面 ,以反映出当前在分享 张图片

}

}

注意 一定要仔细检查传入的数据,因为 妳永远不知道其它的应用程序会向妳发送些什么东西。例如,可能 会设置了错误的多媒体类型,或者,即将发送的图片可能会大 得冒屎 。另外 ,记得要在单独的线程中处理二进制数据,而不要在主(界面)线程中处理。

更新界面 的过程,可以简单到仅仅填充某个 EditText 的内容,或者 也可以更复杂,例如 向图片应用某个有趣的照片滤镜。 下一步的动作,的确是取决于妳的。

指南针

Your opinions
Your name:Email:Website url:Opinion content:
- no title specified

HxLauncher: Launch Android applications by voice commands