• Android: Receiving Data from the Send Intent,自己app注册系统分享


    当用户在系统的专辑,点击共享时。通过我们自己的app。分享此图片。

    1.注册

    主要是在AndroidManifest.xml中,对activity注冊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>

    2.处理:

    在Activity中,获取Intent中的文本和图片等。

    void onCreate (Bundle savedInstanceState) {
        ...
        // 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/")) {
                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
        }
    }

    很多其它阅读:

    简单举例: http://code.tutsplus.com/tutorials/android-sdk-receiving-data-from-the-send-intent--mobile-14878

    很多其它交流。Android开发联盟QQ群:272209595


    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    Mac上Homebrew的使用 (Homebrew 使 OS X 更完整)
    Redis 集群方案
    mac下java 开发环境搭建
    [转贴]有关Angular 2.0的一切
    XmlSerializer 对象的Xml序列化和反序列化,XMLROOT别名设置
    【人人为我,我为人人】大量免费电子书持续更新中,2014年8月13日更新
    KAFKA分布式消息系统
    Apache Kafka —一个不同的消息系统
    分布式消息系统Kafka初步
    mysql exists 如何使用
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4869105.html
Copyright © 2020-2023  润新知