0

I find a lot of questions on StackOverflow showing how to open whatsapp in a given contact, but what I need is to launch it main screen (contact list).

I am on a fragment and I have tried the following:

1 Intent with category LAUNCHER

fun openWhats() {
    val intent = Intent(Intent.LAUNCHER )
    intent.setPackage("com.whatsapp")
    activityResultLauncher.launch(intent)
}

2 Intent with category MAIN

fun openWhats() {
    val intent = Intent(Intent.MAIN)
    intent.setPackage("com.whatsapp")
    activityResultLauncher.launch(intent)
}

ERROR: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.MAIN pkg=com.whatsapp }

3 Using Intent with api.whatsapp and category ACTION_VIEW

fun Fragment.openWhats() {
    Intent(Intent.ACTION_VIEW).apply {
        data = Uri.parse("https://api.whatsapp.com")
    }.also {
        startActivity(it)
    }
}

ERROR: Cannot open link

4 Using getLaunchIntentForPackage()

fun Fragment.openApp(packageName: String): Boolean {
    val manager = requireActivity().packageManager
    return try {
        val i = manager.getLaunchIntentForPackage(packageName) ?: return false
        i.addCategory(Intent.CATEGORY_LAUNCHER)
        requireActivity().startActivity(i)
        true
    } catch (e: ActivityNotFoundException) {
        false
    }
}

Does not work

Any idea how to achieve this?

Leonardo Sibela
  • 1,613
  • 1
  • 18
  • 39
  • https://faq.whatsapp.com/511210730860677/?cms_platform=android might help – Ken Wolf Mar 05 '23 at 20:00
  • Thank you @KenWolf, but it only show (as far as I saw) how to open a contact (with or without a given text). I need the contact list – Leonardo Sibela Mar 05 '23 at 20:15
  • You need an android device contact list or need only whats app contact list ? – Tippu Fisal Sheriff Mar 07 '23 at 01:46
  • @TippuFisalSheriff I would like to lauch the whatsapp application. If it would be by starting the What's App Contact List or if it would be by calling something like "lauchAppByPackage("com.whatsapp") (and the list would apear), it does not matter, but it is the What's App Contact list, not the device contact list. Also, if the app was already open before and not closed and it was on another user screen, if the app opens on that screen, it's also not a problem for my usecase. – Leonardo Sibela Mar 08 '23 at 01:47
  • @TippuFisalSheriff also, you said that there's an issue with the what's app package. Can you let me know where you found that? I would like to read about and understand more. – Leonardo Sibela Mar 08 '23 at 01:49
  • Did you find an solution for the above or need to explain. – Tippu Fisal Sheriff Mar 08 '23 at 05:37
  • @TippuFisalSheriff I still didn't find a answer – Leonardo Sibela Mar 10 '23 at 01:34

2 Answers2

0

Use the Below function, you need to pass a context and phone number with the country code.

fun openWhatsAppConversationUsingUri(
        context: Context,
        numberWithCountryCode: String,
    ) {
        val uri: Uri =
            Uri.parse("https://api.whatsapp.com/send?phone=$numberWithCountryCode")
        val sendIntent = Intent(Intent.ACTION_VIEW, uri)
        context.startActivity(sendIntent)
    }
Tippu Fisal Sheriff
  • 2,177
  • 11
  • 19
  • this unfortunatly does not work for my usecase. This opens an user chat screen. I need the list of contacts screen on whats app. – Leonardo Sibela Mar 12 '23 at 21:01
0

With the help of Chat GPT, I found the answer to my questions:

val packageName = "com.whatsapp.w4b"
val className = "com.whatsapp.Main"
val intent = Intent(Intent.ACTION_MAIN)
intent.setClassName(packageName, className)
startActivity(intent)

Chat GPT gave me the code, but with the wrong activity.

Them, he explained me how to use dumpsys activity command to find a list of all activities on my device.

He them helped me to create copy the return of this comand to my computer:

On the adb sheel:

$ dumpsys activity > /sdcard/clipboard.txt

On my computer terminal:

$ $ adb pull /sdcard/clipboard.txt

It took me a while, because dumpsys activity returned a huge amout of text, but i found it here:

Visible recent tasks (most recent first):
  * RecentTaskInfo #0: 
    id=6030 userId=0 hasTask=true lastActiveTime=32552197
    baseIntent=Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.whatsapp.w4b/com.whatsapp.Main }
    baseActivity={com.whatsapp.w4b/com.whatsapp.HomeActivity}
    topActivity={com.whatsapp.w4b/com.whatsapp.HomeActivity}
    realActivity={com.whatsapp.w4b/com.whatsapp.Main}
    isExcluded=false activityType=standard windowingMode=fullscreen supportsSplitScreenMultiWindow=true supportsMultiWindow=true
    taskDescription { colorBackground=#ff303030 colorPrimary=#ff202c33 iconRes=/0 iconBitmap=false resizeMode=RESIZE_MODE_RESIZEABLE_VIA_SDK_VERSION minWidth=-1 minHeight=-1 colorBackgroundFloating=#ff424242 }
    lastSnapshotData { taskSize=Point(1080, 2400) contentInsets=Rect(0, 88 - 0, 144) bufferSize=Point(1080, 2400) }
Leonardo Sibela
  • 1,613
  • 1
  • 18
  • 39