跟Google学习Android开发-起始篇-共享内容(2)

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


正如你的应用程序可以将数据发送到其他应用程序,所以也可以很容易接收来自应用程序的数据。想想用户如何与你的应用程序交互,以及你要从其他应用程序接收什么样类型的数据。例如,一个社交网络应用程序可能会对从另一个应用程序接收文本内容感兴趣,像一个有趣的网页的URLGoogle+ Android应用程序接受文字单个或多个图像。有了这个程序,用户可以轻松地开始新的Google+发布来自Android库应用程序中的照片。


更新你的Manifest


意图过滤器通知系统,应用程序组件愿意接受什么意图。与你在使用意图发送内容到其它应用程序那课程中通过ACTION_SEND操作构建意图的方式相似,为了能够接收这个操作的意图您要创建意图过滤器。您使用 <intent-filter> 元素在你的manifest中定义一个意图过滤器。例如,如果您的应用程序处理接收文本内容,任何类型单一图片,或任何类型的多个图片,你的manifest看起来应该像这样子:

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

注:对于意图过滤器和意图解决方案的更多信息,请阅读意图和意图过滤器

当另一个应用程序试图通过建设意图,并把它传递给startActivity()分享任何这些东西,你的申请将被列为意向选择中的一个选项。如果用户选择应用程序时,将启动相应的活动(在上面的例子ui.MyActivity)。然后,它是由你来处理的内容适当地在自己的代码和UI


处理传入的内容


要处理意图分发的内容,通过调用getIntent() 来获得意向对象开始。一旦你有了这个对象,你可以检查其内容以确定下一步该怎么做。请记住,如果这个活动可以从系统的其他部件开启,如启动器,那么你就需要在检查意图时考虑到这一点。

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 textbeing sent
        } elseif (type.startsWith("image/")){
            handleSendImage(intent);// Handlesingle image being sent
        }
    } else if (Intent.ACTION_SEND_MULTIPLE.equals(action)&& type != null) {
        if (type.startsWith("image/")){
            handleSendMultipleImages(intent);// Handlemultiple 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”)线程。

更新用户界面,可以像填充EditText那么简单,也可以像在图像上应用一个有趣的照片滤镜那么复杂。它真的是取决于应用程序的下一步会发生什么。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值