【Android 基础 2.3】Implicit intents

2018-11-14 10:31:21

Introduction

With an implicit intent, you active an activity without knowing which app or activity will handle the task.

Conversely, your activity can declare one or more intent filters in the AndroidManifest.xml file to advertise that the activity can accept implicit intents, and to define the types of intents that the activity will accept.

In this practical we’ll build an app that sends an implicit intent to perform each of the following tasks:

  • Open a URL in a web browser.
  • Open a location on a map.
  • Share text.
  • open camera

Open website

    fun openWebsite(view: View) {
        val url = website_edittext.text.toString()
        val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
        if (intent.resolveActivity(packageManager) != null) {
            startActivity(intent)
        } else {
            Log.d("ImplicitIntents", "Can't handle this intent!");
        }
    }

Open location

    fun openLocation(view: View) {
        val loc = location_edittext.text.toString()
        val intent = Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=$loc"))

        if (intent.resolveActivity(packageManager) != null) {
            startActivity(intent)
        } else {
            Log.d("ImplicitIntents", "Can't handle this intent!");
        }
    }

Share text

    fun shareText(view: View) {
        val text = share_edittext.text.toString()
        val mimeType = "text/plain"
        ShareCompat.IntentBuilder
            .from(this)
            .setType(mimeType)
            .setChooserTitle("Share this text with:")
            .setText(text)
            .startChooser()
    }

Take photo

    fun openCamera(view: View) {
        val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        if (intent.resolveActivity(packageManager) != null) {
            startActivityForResult(intent, TAKE_PICTURE)
        } else {
            Log.d("ImplicitIntents", "Can't handle this intent!");
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == TAKE_PICTURE) {
            if (resultCode == Activity.RESULT_OK && data != null) {
                Log.d(TAG, "onActivityResult: ")
                val bitmap = data.extras["data"] as Bitmap
                picture.setImageBitmap(bitmap)
            }
        }
    }

Summary

  • An implicit Intent allows you to activate an Activity if you know the action, but not the specific app or Activity that will handle that action.
  • An Activity that can receive an implicit Intent must define Intent filters in the AndroidManifest.xml file that match one or more Intent actions and categories.
  • The ShareCompat.IntentBuilder class makes it easy to build an implicit Intent for sharing data to social media or email.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值