androidandroid-intentandroid-activitywhatsappwhatsapi

How to lauch whatsapp contact list


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?


Solution

  • 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) }