Skip to content

Instantly share code, notes, and snippets.

@mukulchauhan10
Created February 1, 2020 03:35
Show Gist options
  • Save mukulchauhan10/2ec6e93cec388a33cfd5375f24257f93 to your computer and use it in GitHub Desktop.
Save mukulchauhan10/2ec6e93cec388a33cfd5375f24257f93 to your computer and use it in GitHub Desktop.
whatsApp
package com.openwhatsapp
import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.core.text.isDigitsOnly
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn.setOnClickListener {
val msg =
"LOL! why are you clicking it continuously. Because you have nothing to do here"
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
}
intent.action = Intent.ACTION_PROCESS_TEXT
val number= intent?.getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT).toString()
Log.i("enter1", "done")
if (number.isDigitsOnly() || number[0] == '+')
openWhatsapp(number)
else
Toast.makeText(this, "Oops! Provide digits only", Toast.LENGTH_SHORT).show()
}
private fun openWhatsapp(num: String) {
val act = Intent(Intent.ACTION_VIEW) // meaning?
act.setPackage("com.whatsapp")
val contact: String = if (num[0] == '+')
num.substring(1)
else if (num.length == 10)
"91$num"
else if (num.length == 11 && num[0] == '0')
"91" + num.substring(1)
else
num
act.data = Uri.parse("https://wa.me/$contact")
if (packageManager.resolveActivity(act, 0) != null) {
startActivity(act)
} else
Toast.makeText(this, "Oops! What's App isn't installed", Toast.LENGTH_SHORT).show()
finish()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment