第2章 Building Apps with Contents Sharing

第1节 Sharing Simple Data

1.1 Sending Simple Date to Other Apps

        When you construct an intend, you must specify the action you want the intent to "trigger",Android defines serveral action,

including ACTION_SENDING which is sending data from one activity to anther,even across process boundaries.

        All you need to do is specify the data and its type, the system will identify compatible receiving activities and display themor

immediately start the activity. You can also advertise the data types that you avtivities support recerving from other applicaion

by specifying them in you manifest.

        We can use intents to share information quickly and easily.Sending and receiving data between applications with intents

is most commonly used for social sharing of content.

        The best way to add a share action item to an ActionBar is to use ShareActionProvider, which became avaible in API

level 14 . 

Send text content

        The most staightforward and common use of the ACTION_SEND action is sending text content from one activity to another.

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);

step 1 : new an intent object;

step 2 : set the action for the intent;

step 3 : set the content;

step 4 : set the MIME type 

step 5 : start the activity


If there is an installed application with a filter that matches ACTION_SEND and MIME type text/plain, the Android system will

 run it; if more than one ,it will be displayed in a dialog that allows the user to choose an app. 

Note : You can also call Intent.createChooser() for the intent in step 5, There are some advantages;

Here is the updated code: 

startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));


Send binary content

Binary data is shared using the ACTION_SEND action combined with setting the appropriate MIME type and placing

the URI to the data in an extra named EXTRA_STREAM. This is commonly used to share an image but can be uses 

to share any type of binary data:

Here is the code to implement this type of sharing

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)));

You can also send multiple pieces of content by using the ACTION_SEND_MULTIPLE action together with a list of

URIs pointing to the content . the MIME type varies according to the mix of content  you are sharing.

Here is an example:

ArrayList<Uri> imageUris = new ArrayList<Uri>();
imageUris.add(imageUri1); // Add your image URIs here
imageUris.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.."));

1.2 Receiving Simple Data From Other Apps

This class will tell you how you application receive simple data from other applications, For example, a user can easily

start a new baidu post with photos from the Android Gallery app . 

Update you mamifest

You define an intent filter in your manifest using<intent-filter>element in order to be able to receive intents which you

constructed in the previous lesson.

Here is an example:

<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>

Note: This example define your application handles receiving text content , a single image of any type, or multiple

images of any type.

          When another application tries to share any of these things by constructing an intent and pasing it to starActivity()

, your application will be listed as an option in the intent chooser.If the user selects your application, the corresponding

activity will be started, It is tnen up to you to handle the content appropriately within you code and UI.


Handle the incoming content

To start by calling getIntent() to get Intent object, as you get the object, you can examine its contents to determine what to do next.

  and this step need you to take into consideration when examining the intent.

Here is the example:

 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); // Handle text being sent
        } else if (type.startsWith("image/")) {
            handleSendImage(intent); // Handle single image being sent
        }
    } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
        if (type.startsWith("image/")) {
            handleSendMultipleImages(intent); // Handle multiple images being sent
        }
    } else {
        // Handle other intents, such as being started from the home screen
    }
    ...
}

void handleSendText(Intent intent) {
    String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
    if (sharedText != null) {
        // Update UI to reflect text being shared
    }
}

void handleSendImage(Intent intent) {
    Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
    if (imageUri != null) {
        // Update UI to reflect image being shared
    }
}

void handleSendMultipleImages(Intent intent) {
    ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
    if (imageUris != null) {
        // Update UI to reflect multiple images being shared
    }
}

Note: Remember to process binary data in a seperate thread rather than the UI thread .


1.3  Adding an Easy Share Action 

By using ActionProvider (API Level 14), you can implement an effective and user friendly share action in you ActionBar easily.

ActionProvider handles both the apprearance and behavior of the item which are attached to a menu item in the ActionBar

You just need to provide a share intent and the ShareActionProvider does the rest.

























  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
项目:使用AngularJs编写的简单 益智游戏(附源代码)  这是一个简单的 javascript 项目。这是一个拼图游戏,也包含一个填字游戏。这个游戏玩起来很棒。有两个不同的版本可以玩这个游戏。你也可以玩填字游戏。 关于游戏 这款游戏的玩法很简单。如上所述,它包含拼图和填字游戏。您可以通过移动图像来玩滑动拼图。您还可以选择要在滑动面板中拥有的列数和网格数。 另一个是填字游戏。在这里你只需要找到浏览器左侧提到的那些单词。 要运行此游戏,您需要在系统上安装浏览器。下载并在代码编辑器中打开此项目。然后有一个 index.html 文件可供您修改。在命令提示符中运行该文件,或者您可以直接运行索引文件。使用 Google Chrome 或 FireFox 可获得更好的用户体验。此外,这是一款多人游戏,双方玩家都是人类。 这个游戏包含很多 JavaScript 验证。这个游戏很有趣,如果你能用一点 CSS 修改它,那就更好了。 总的来说,这个项目使用了很多 javascript 和 javascript 库。如果你可以添加一些具有不同颜色选项的级别,那么你一定可以利用其库来提高你的 javascript 技能。 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值