Android

Android Intent 所有操作

打電話

val uri = Uri.parse("tel:09555")
val intent = Intent(Intent.ACTION_SENDTO, uri)
startActivity(intent)

發簡訊

val uri = Uri.parse("smsto:09555")
val intent = Intent(Intent.ACTION_SENDTO, uri)
intent.putExtra("sms_body", "Hello")
startActivity(intent)

分享文字

val intent = Intent(Intent.ACTION_SEND)
intent.putExtra(Intent.EXTRA_TEXT, "我是誰")
intent.type = "text/plain"
startActivity(intent)

分享圖片(一定要換成uri 參考FileProvider)

//圖片變File
val bitmap = BitmapFactory.decodeResource(resources, R.drawable.dd)
val file = File(externalCacheDir, "gg.png")
val ops = FileOutputStream(file)
bitmap.compress(Bitmap.CompressFormat.PNG, 100, ops)
//File轉Uri
val contentUri = FileProvider.getUriForFile(this, "${BuildConfig.APPLICATION_ID}.provider", file)
val intent = ShareCompat.IntentBuilder
      .from(this)
      .setStream(contentUri)
      .getIntent();
intent.setDataAndType(contentUri,"application/pdf");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);

打開瀏覽器

val uri = Uri.parse("https://google.com")
val intent = Intent(Intent.ACTION_VIEW, uri)
startActivity(intent)

發送Email

val intent= Intent(Intent.ACTION_SEND);
val tos = arrayOf("1@abc.com", "2@abc.com")
val ccs = arrayOf("3@abc.com", "4@abc.com")
val bccs = arrayOf("5@abc.com", "6@abc.com")
intent.putExtra(Intent.EXTRA_EMAIL, tos)
intent.putExtra(Intent.EXTRA_CC, ccs);
intent.putExtra(Intent.EXTRA_BCC, bccs)
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject")
intent.putExtra(Intent.EXTRA_TEXT, "Hello")
intent.type = "message/rfc822"
startActivity(intent)

打開GoogleMap位置

val uri = Uri.parse("geo:25.03,121.56")
val intent = Intent(Intent.ACTION_VIEW, uri)
startActivity(intent)

打開GoogleMap規劃路線

val uri = Uri.parse("https://maps.google.com/maps?f=d&daddr=25.03,121.56")
val intent = Intent(Intent.ACTION_VIEW, uri)
startActivity(intent)

拍照照相機(申請CAMERA權限)

獲取權限
requestPermissions(arrayOf(Manifest.permission.CAMERA), 0)

//創建File
val file = File(externalCacheDir, "gg.png")
val contentUri = FileProvider.getUriForFile(this, "${BuildConfig.APPLICATION_ID}.provider", file)
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
intent.putExtra(MediaStore.EXTRA_OUTPUT, contentUri)
startActivityForResult(intent, 0)

//獲取File
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    val file = File(externalCacheDir, "gg.png")
    Log.e("GGG", file.length().toString())
}

打開Google Play

val uri = Uri.parse("market://details?id=" + "com.fly.waterpass")
val intent = Intent(Intent.ACTION_VIEW, uri)
startActivity(intent)

打開設定

val intent = Intent(Settings.ACTION_SETTINGS)
startActivity(intent)

有關設定的Action
https://www.itread01.com/p/15045.html

解除APK(要加權限)



val uri = Uri.parse("package:com.fly.waterpass")
val intent =  Intent(Intent.ACTION_DELETE, uri);
startActivity(intent);

選取聯絡人

val intent = Intent(Intent.ACTION_VIEW)
intent.data = ContactsContract.Contacts.CONTENT_URI
startActivity(intent)

添加聯絡人

val intent = Intent(Intent.ACTION_INSERT_OR_EDIT)
intent.type = ContactsContract.Contacts.CONTENT_ITEM_TYPE
intent.putExtra(ContactsContract.Intents.Insert.NAME, "My Name")
intent.putExtra(ContactsContract.Intents.Insert.PHONE, "+1234567890")
intent.putExtra(
    ContactsContract.Intents.Insert.PHONE_TYPE,
    ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE
)
intent.putExtra(ContactsContract.Intents.Insert.EMAIL, "com@com.com")
intent.putExtra(
    ContactsContract.Intents.Insert.EMAIL_TYPE,
    ContactsContract.CommonDataKinds.Email.TYPE_WORK
)
startActivity(intent)

打開另一個APP

val intent = Intent(Intent.ACTION_MAIN)
intent.component = ComponentName("com.fly.waterpass", "com.fly.waterpass.MainActivity")
startActivity(intent)

打開錄音機

val intent = Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION)
startActivity(intent)

Google搜尋內容

val intent =  Intent(Intent.ACTION_WEB_SEARCH)
intent.putExtra(SearchManager.QUERY,"searchString")
startActivity(intent)

發表迴響