Last active
December 10, 2017 15:06
-
-
Save abdul-rehman-2050/28f4a987adde6b837171eecce3d2e4a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun whatsApp() { | |
val smsNumber = "923224522526" | |
val isWhatsappInstalled = whatsappInstalledOrNot("com.whatsapp") | |
if (isWhatsappInstalled) { | |
val sendIntent = Intent("android.intent.action.MAIN") | |
sendIntent.component = ComponentName("com.whatsapp", "com.whatsapp.Conversation") | |
sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators(smsNumber) + "@s.whatsapp.net")//phone number without "+" prefix | |
sendIntent.putExtra(Intent.EXTRA_TEXT, "dummy text from app"); | |
startActivity(sendIntent) | |
} else { | |
val uri = Uri.parse("market://details?id=com.whatsapp") | |
val goToMarket = Intent(Intent.ACTION_VIEW, uri) | |
Toast.makeText(this, "WhatsApp not Installed", | |
Toast.LENGTH_SHORT).show() | |
startActivity(goToMarket) | |
} | |
} | |
private fun whatsappInstalledOrNot(uri: String): Boolean { | |
val pm = packageManager | |
var app_installed = false | |
try { | |
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES) | |
app_installed = true | |
} catch (e: PackageManager.NameNotFoundException) { | |
app_installed = false | |
} | |
return app_installed | |
} | |
val i = Intent(Intent.ACTION_SENDTO, Uri.parse("content://com.android.contacts/data/923224522526")) | |
i.type = "text/plain" | |
i.`package` = "com.whatsapp" // so that only Whatsapp reacts and not the chooser | |
i.putExtra(Intent.EXTRA_SUBJECT, "Subject") | |
i.putExtra(Intent.EXTRA_TEXT, "I'm the body.") | |
startActivity(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment