whatsapp not opening programmatically - android

have run the code in my phone or just the "else" condition is working.on running the app just displays the "package is not installed"
"Kotlin"
val button = findViewById<Button>(R.id.openwhtaspchat)
button.setOnClickListener {
val uri = Uri.parse("smsto"+"+923354859491")
val intent = Intent(Intent.ACTION_SENDTO,uri)
intent.setPackage("com.whatsapp")
if (intent.resolveActivity(this.packageManager)!=null){
startActivity(intent)
}else{
Toast.makeText(this,"package is not installed",Toast.LENGTH_SHORT).show()
}

when u wanna send something to specific app use
val intent = Intent(Intent.ACTION_SEND, uri)
i'm using always that metode not this one
val intent = Intent(Intent.ACTION_SENDTO, uri)
i'm not sure about kotlin, i use java but Intent are same i think

Related

How to open a uri in file manager in Android

I'm trying to make a button in my app when user click on it, it'll open a specific URI in file manager but the best I could've done is that the button opens recent tab in default file manager.
Please, if it's possible, suggest me a code which opens a chooser for user to choose between his file manager applications and when user chose, that file manager opens in specific URI that I defined in my code.
Here is my code:
val intent = Intent(Intent.ACTION_GET_CONTENT)
val uri = Uri.parse(
//my path
)
intent.data = uri
intent.type = "*/*"
startActivity(Intent.createChooser(intent, "Open folder"))
Also one of the users suggested me to use INITIAL_URI I've did it like this but didn't work :
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
val uri = Uri.parse(
//my path
)
intent.data = uri
intent.type = "*/*"
intent.putExtra("android.provider.extra.INITIAL_URI", uri)
intent.putExtra("android.content.extra.SHOW_ADVANCED", true)
startActivity(Intent.createChooser(intent, "Open folder"))
suggest me a code which opens a chooser for user to choose between his file manager applications and when user chose, that file manager opens in specific URI that I defined in my code
That has never been a pattern in Android app development. There is no standard Intent action for what you seek that is likely to be implemented by much of anything, let alone a significant number of file manager apps.
fun openNewTabWindow(urls: String, context : Context) {
val uris = Uri.parse(urls)
val intents = Intent(Intent.ACTION_VIEW, uris)
val myV = Bundle()
myV.putBoolean("new_window", true)
intents.putExtras(myV)
context.startActivity(intents)
}

start directory of a file explorer Intent for Android

What is the correct way to start a File Explorer by Intent in a specified directory?
Following code snippet works fine, except that it starts in the wrong directory.
The desired starting point would be at "selectedUri"
val selectedUri = Uri.parse(externalStorage.toString() + "DCIM/Camera/")
val intent= Intent(Intent.ACTION_GET_CONTENT).apply{
addCategory(Intent.CATEGORY_OPENABLE)
type = "image/*"
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
you forgot to set the intent data
val selectedUri = Uri.parse(externalStorage.toString() + "DCIM/Camera/")
val intent= Intent(Intent.ACTION_GET_CONTENT).apply{
addCategory(Intent.CATEGORY_OPENABLE)
data = selectedUri
type = "image/*"
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}

Send text message to WhatsApp and SMS using intent create chooser not working anymore

The code below used to work a week ago. It's purpose is to allow the user to choose if he want to send a text message using WhatsApp or SMS, but now when I choose WhatsApp it doesn't do anything, although SMS keeps working.
Looking at the logcat it's printing: 2018-10-25 18:28:28.915 2147-6714/? I/ActivityManager: START u0 {act=android.intent.action.SENDTO dat=smsto:xxxxxxxxxxx flg=0x3000000 cmp=com.whatsapp/.Conversation (has extras)} from uid 10096 Even passing a valid number with country code, it is printing smsto:xxxxxxxxxxx.
Is there any working code for this purpose or know about this problem?
fun sendMessageToNumber(number: String, text: String) {
val cleanNumber = number.cleanText()
val uri = Uri.parse("smsto:$cleanNumber")
val sendIntent = Intent(Intent.ACTION_SENDTO, uri)
sendIntent.putExtra("sms_body", text)
context?.startActivity(Intent.createChooser(sendIntent, context.getString(R.string.fragment_account_chooser_message_title)))
}
In one of my project I have used following code for sending whatsapp message :
fun sendWhatsappMsg(){
var toNumber = "+91 xxxxx xxxxx" // contains spaces.
toNumber = toNumber.replace("+", "").replace(" ", "")
val sendIntent = Intent("android.intent.action.MAIN")
sendIntent.putExtra("jid", "$toNumber#s.whatsapp.net")
sendIntent.putExtra(Intent.EXTRA_TEXT, "Hello")
sendIntent.action = Intent.ACTION_SEND
sendIntent.setPackage("com.whatsapp")
sendIntent.type = "text/plain"
startActivity(sendIntent)
}
And to send text message I have used following code :
fun sendTextMsg(){
val phone = "xxxxxxxxxx"
val msg = "smsto:" + phone
val smsUri = Uri.parse(msg)
val smsIntent = Intent(Intent.ACTION_SENDTO, smsUri)
startActivity(smsIntent)
}
You can try this. Both are working for me.

Pre fill SMS in facebook messenger

I would like to pre fill a SMS for user who uses facebook messenger.
Here you can find my Kotlin code :
val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("sms:" + smsToString)
intent.putExtra("sms_body", provider.getSmsBody())
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val list = context!!.packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY)
val context = activity!!.applicationContext
if (list.isNotEmpty()) {
context.startActivity(intent)
}
This code launch the facebook messenger app with the new sms window, and with the list of contacts prefilled.
But, the sms_body is empty. The same code works when the default sms app is Android Messages.
Is there a solution ?
Thanks :)

got crash when try to use intent to send email (Kotlin)

I got a crash when I try to send an email using intent with kotlin
here's my function
/**
* intentEmail is called when we need to send email
*
* #param price int
*/
fun intentEmail(price: Int) {
var intent = Intent(Intent.ACTION_SEND)
//intent.putExtra(Intent.EXTRA_EMAIL, addressees)
intent.data= Uri.parse("mailto:")
intent.putExtra(Intent.EXTRA_SUBJECT, "Just Java order for $name")
intent.putExtra(Intent.EXTRA_TEXT, createOrderSummary(price))
if(intent.resolveActivity(packageManager) != null){
startActivity(intent)
}
}
and the crash happens when calling startActivity(intent)
and here's my LogCat
maybe your phone is not accept this intent action. you should use try catch the avoid this crash.
you can also use the phone's other "send mail" app, so that you can find out what's the correct intent.
The issue was in
var intent = Intent(Intent.ACTION_SEND)
when I changed it to
var intent = Intent(Intent.ACTION_SENDTO)
it works fine thanks to #lampenlampen

Categories

Resources