Android知识Android开发Android技术知识

APP之间共享数据(一)

2017-01-04  本文已影响316人  Big不吃鱼

传递Text数据

如果有安装过的多个APP能够匹配ACTION_SEND这个action并且能够接受MIME TYPE为text/plain,则系统会弹出选择框允许用户选择想要传递给的APP。

ShareText.png

代码如下:

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

如果想总是显示出多个匹配的应用的选择框,可以使用Intent.createChooser(),这样可以保证即使之前已经对这个action做出过默认选择,仍然会显示一个选择框。可以通过createChooser指定一个title。
代码如下:

        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().getString(R.id.send_to)));

传递二进制数据

向EXTRA_STREAM传递data的Uri即可传递二进制的数据,该方法通常用来分享Image。
代码如下:

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

传递多条数据集

使用ACTION_SEND_MULTIPLE这个action来传递多条数据。
代码如下:

       ArrayList<Uri> imageUris = new ArrayList<Uri>();
       imageUris.add(imageUri1);
       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 .."));

接收数据(从其他APP)

Update Your Manifest

使用<intent-filter>元素在Manifest里注册,可以指定接收特定类型的数据,假如你想接收的text类型和任何类型的单张图片或者多张图片,你可以向这样定义你的manifest:

        <activity android:name=".MainActivity">
            <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.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>
Handle the incoming Content

通过调用getIntent()方法得到intent对象,根据内容的不同,做不同的处理,例如:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get intent, action and MIME type
        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/")) {
                handSendImage(intent); // handle single image being sent
            }
        } else if (Intent.ACTION_SEND_MULTIPLE.equals(action)) {
            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 images 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
        }
    }
Adding an Easy Share Action

使用ShareActionProvider在actionbar上添加一个快捷分享按钮.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/menu_item_share"
        android:showAsAction="ifRoom"
        android:title="Share"
        android:actionProviderClass="android.widget.ShareActionProvider" />

</menu>
    private ShareActionProvider mShareActionProvider;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // inflate menu resource file
        getMenuInflater().inflate(R.menu.share_menu, menu);

        // locate MenuItem with ShareActionProvider
        MenuItem item = menu.findItem(R.id.menu_item_share);

        // fetch and store ShareActionProvider
        mShareActionProvider = (ShareActionProvider) item.getActionProvider();

        // return true to display menu
        return true;
    }

    // call to update the share intent
    private void setShareIntent(Intent shareIntent) {
        if (mShareActionProvider != null) {
            mShareActionProvider.setShareIntent(shareIntent);
        }
    }
上一篇下一篇

猜你喜欢

热点阅读