当前位置:操作系统 > 安卓/Android >>

Android---接收来自其他应用程序的内容

就像你的应用程序能够把数据发送给其他应用程序一样,它也可以很容易的接收来自其他应用程序的数据。在接收来自其他应用程序的数据时,需要考虑用户如何跟你的应用程序进行交互,以及你的应用程序想要接收的数据类型。例如,一个社交网络应用程序应该对接收文本内容感兴趣,如感兴趣的来自另外一个应用程序的Web网址(URL)。Android的Google+应用程序会接收文本和图片(一张或多张)。使用这个应用程序,用户可以轻松的启动Google+来发送来自Android图库应用中的图片。

更新你的清单

Intent过滤器会通知系统,一个应用程序组件会接收什么样的Intent对象。在清单文件中,使用<intent-filter>元素定义一个Intent过滤器。例如,如果你的应用程序要处理接收的文本、任意类型的图片(一张或多张),你应该像下面这样定义清单文件:

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

注意:有关Intent过滤器和Intent解析的更多信息请看Intent和Intent过滤器

当另外一个应用程序通过构造一个Intent对象,并把它传递给startActivity()来共享这些东西时,你的应用程序就会作为一个列表项被列在Intent选择器中。如果用户选择了你的应用程序,对应的Activity(上例中的.ui.MyActivity)就被启动。然后你就可以在你的代码和UI中处理相应的内容了。

处理输入的内容

要处理由Intent对象所发送的内容,就要从调用getIntent()方法来获得Intent对象开始。一旦你获得了这个对象,就可以检查它的内容来判断下一步的工作。要记住,如果被启动的Activity是来自系统的其他部分,如Launcher,那么在检查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
    }
}

警告:要额外仔细的检查输入的数据,因为你不知道其他的应用程序会给你发送什么内容。例如,MIME类型可能被错误的设置,或者被发送的图片可能超大。还要记住的时,二进制的数据要在独立的线程中处理,而不是在主线程(UI)中。

更新UI可以像填写EditText控件一样简单,或者是应用与复杂的感兴趣的图片过滤。

 

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,