博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Sending Content to Other Apps
阅读量:6690 次
发布时间:2019-06-25

本文共 5805 字,大约阅读时间需要 19 分钟。

Sending Content to Other Apps

This lesson teaches you to

You should also read

When you construct an intent, you must specify the action you want the intent to "trigger." Android defines several actions, including which, as you can probably guess, indicates that the intent is sending data from one activity to another, even across process boundaries. To send data to another activity, all you need to do is speicify the data and its type, the system will identify compatible receiving activities and display them to the user (if there are multiple options) or immediately start the activity (if there is only one option). Similarly, you can advertise the data types that your activities support receiving from other applications by specifying them in your manifest.

Sending and receiving data between applications with intents is most commonly used for social sharing of content. Intents allow users to share information quickly and easily, using their favorite applications.

Note: The best way to add a share action item to an is to use , which became available in API level 14. is discussed in the lesson about .

Send Text Content


Figure 1. Screenshot of intent chooser on a handset.

The most straightforward and common use of the action is sending text content from one activity to another. For example, the built-in Browser app can share the URL of the currently-displayed page as text with any application. This is useful for sharing an article or website with friends via email or social networking. Here is the code to implement this type of sharing:

Intent sendIntent = new Intent();sendIntent.setAction(Intent.ACTION_SEND);sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");sendIntent.setType("text/plain");startActivity(sendIntent);

If there's an installed application with a filter that matches and MIME type text/plain, the Android system will run it; if more than one application matches, the system displays a disambiguation dialog (a "chooser") that allows the user to choose an app. If you call for the intent, Android will always display the chooser. This has some advantages:

  • Even if the user has previously selected a default action for this intent, the chooser will still be displayed.
  • If no applications match, Android displays a system message.
  • You can specify a title for the chooser dialog.

Here's the updated code:

Intent sendIntent = new Intent();sendIntent.setAction(Intent.ACTION_SEND);sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");sendIntent.setType("text/plain");startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to));

The resulting dialog is shown in figure 1.

Optionally, you can set some standard extras for the intent: , , , . However, if the receiving application is not designed to use them, nothing will happen. You can use custom extras as well, but there's no effect unless the receiving application understands them. Typically, you'd use custom extras defined by the receiving application itself.

Note: Some e-mail applications, such as Gmail, expect a for extras like and , use to add these to your intent.

Send Binary Content


Binary data is shared using the action combined with setting the appropriate MIME type and placing the URI to the data in an extra named . This is commonly used to share an image but can be used to share any type of binary content:

Intent shareIntent = new Intent();shareIntent.setAction(Intent.ACTION_SEND);shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);shareIntent.setType("image/jpeg");startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));

Note the following:

  • You can use a MIME type of "*/*", but this will only match activities that are able to handle generic data streams.
  • The receiving application needs permission to access the data the points to. There are a number of ways to handle this:
    • Write the data to a file on external/shared storage (such as the SD card), which all apps can read. Use to create the that can be passed to the share intent. However, keep in mind that not all applications process a file:// style .
    • Write the data to a file in your own application directory using with mode after which can be used to return a . As with the previous option, will create a file:// style for your share intent.
    • Media files like images, videos and audio can be scanned and added to the system using . The callback returns a content:// style suitable for including in your share intent.
    • Images can be inserted into the system using which will return a content:// style suitable for including in a share intent.
    • Store the data in your own , make sure that other apps have the correct permission to access your provider (or use ).

Send Multiple Pieces of Content


To share multiple pieces of content, use the action together with a list of URIs pointing to the content. The MIME type varies according to the mix of content you're sharing. For example, if you share 3 JPEG images, the type is still "image/jpeg". For a mixture of image types, it should be "image/*" to match an activity that handles any type of image. You should only use "*/*" if you're sharing out a wide variety of types. As previously stated, it's up to the receiving application to parse and process your data. Here's an example:

ArrayList
imageUris = new ArrayList
();imageUris.add(imageUri1); // Add your image URIs hereimageUris.add(imageUri2);Intent shareIntent = new Intent();shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);shareIntent.setType("image/*");startActivity(Intent.createChooser(shareIntent, "Share images to.."));

As before, make sure the provided point to data that a receiving application can access.

转载地址:http://urhao.baihongyu.com/

你可能感兴趣的文章
转:程序员面试什么最重要
查看>>
团队项目(六)- 事后诸葛亮分析(江山代有才人秃)
查看>>
Linux基本命令(二)-----vim相关命令
查看>>
一些常用的正则表达式
查看>>
UI设计,使用感知分层技术
查看>>
poj1905 Expanding Rods
查看>>
Thread 中的 中断
查看>>
【模板】三分法
查看>>
2015 多校联赛 ——HDU5416(异或)
查看>>
hdu 4533 线段树(问题转化+)
查看>>
IIS7 无法访问请求的页面,因为该页的相关配置数据无效
查看>>
第十一篇、HTML5隐藏播放器播放背景音乐
查看>>
ASP.NET 页生命周期事件
查看>>
21备忘录模式Memento
查看>>
dijkstra算法【图论 - 最短路径】
查看>>
04-关于DOM的事件操作,DOM介绍
查看>>
使用Spring实现多数据源操作
查看>>
《高效程序员的45个习惯》--学无止境(Ⅲ)
查看>>
Docker安装Nginx1.11.10+php7+MySQL
查看>>
《Linux内核设计与实现》课本第四章自学笔记——20135203齐岳
查看>>