0

I want to write a code in Android that can send an app's .apk file and a text message to WhatsApp. How should I go about it?
This is the basic framework of my code:

package com.example.testapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
CoolCoder
  • 786
  • 7
  • 20
sumit
  • 11
  • 1
  • Does this answer your question? [How can i share apk file in my app (send app itself)](https://stackoverflow.com/questions/39485995/how-can-i-share-apk-file-in-my-app-send-app-itself) – Izhan Ali May 29 '21 at 07:37

1 Answers1

0

Google: sending message from application to whatsapp

public void openWhatsApp(View view){
    PackageManager pm=getPackageManager();
    try {
        Intent waIntent = new Intent(Intent.ACTION_SEND);
        waIntent.setType("text/plain");
        String text = "This is  a Test"; // Replace with your own message.

        PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
        //Check if package exists or not. If not then code
        //in catch block will be called
        waIntent.setPackage("com.whatsapp");

        waIntent.putExtra(Intent.EXTRA_TEXT, text);
        startActivity(Intent.createChooser(waIntent, "Share with"));

    } catch (PackageManager.NameNotFoundException e) {
        Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
                .show();
    }catch(Exception e){
        e.printStacktrace();
    }

}

Timur Shubin
  • 35
  • 1
  • 9