Problems with Intent.SetPackage ("com.whatsapp")

1

I'm doing a mini test app that shares a text string, and I wanted it to do it directly by whatsapp, skipping the step of deploying the app choice to share with.

I found that the intent could be made a intent.setPackage("com.whatsapp") to do it directly after the startActivity, but it does not, and even crashea. If I take that line works perfectly, showing me the apps to choose which to share.

Does anyone know why?

public void onClick(View v) {
    String textoUwu = "uwu";
    Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
    whatsappIntent.setType("text/plain");
    whatsappIntent.setPackage("com.whatsapp");
    whatsappIntent.putExtra(Intent.EXTRA_TEXT,textoUwu);
    try{
        startActivity(whatsappIntent);
    }catch (ActivityNotFoundException ex){
        Toast.makeText(MainActivity.this, "Whastapp No Instalado", Toast.LENGTH_SHORT).show();
    }
}

My Logcat says the following:

  

2018-10-25 10: 48: 00.459 19754-19754 / com.example.ciro.uwuapp.app   E / AndroidRuntime: FATAL EXCEPTION: main       Process: com.example.ciro.uwuapp.app, PID: 19754       android.content.ActivityNotFoundException: No Activity found to handle Intent {act = android.intent.action.SEND typ = text / plain flg = 0x1   pkg = com.whatsapp clip = {text / plain T: uwu} (has extras)}           at android.app.Instrumentation.checkStartActivityResult (Instrumentation.java:1944)           at android.app.Instrumentation.execStartActivity (Instrumentation.java:1618)           at android.app.Activity.startActivityForResult (Activity.java:4501)           at android.support.v4.app.FragmentActivity.startActivityForResult (FragmentActivity.java:767)           at android.app.Activity.startActivityForResult (Activity.java:4459)           at android.support.v4.app.FragmentActivity.startActivityForResult (FragmentActivity.java:754)           at android.app.Activity.startActivity (Activity.java:4820)           at android.app.Activity.startActivity (Activity.java:4788)           at com.example.ciro.uwuapp.feature.MainActivity $ 1.onClick (MainActivity.java:35)           at android.view.View.performClick (View.java:6294)           at android.view.View $ PerformClick.run (View.java:24774)           at android.os.Handler.handleCallback (Handler.java:790)           at android.os.Handler.dispatchMessage (Handler.java:99)           at android.os.Looper.loop (Looper.java:172)           at android.app.ActivityThread.main (ActivityThread.java:6590)           at java.lang.reflect.Method.invoke (Native Method)           at com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run (RuntimeInit.java:438)           at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:807)

    
asked by Ciro Gonzalez 24.10.2018 в 19:35
source

1 answer

0

When the error occurred

  

ActivityNotFoundException: No Activity found to handle Intent

specified in your LogCat:

  

FATAL EXCEPTION: main Process: com.example.ciro.uwuapp.app, PID: 19754   android.content.ActivityNotFoundException: No Activity found to handle   Intent {act = android.intent.action.SEND typ = text / plain flg = 0x1   pkg = com.whatsapp clip = {text / plain T: uwu} (has extras)}

indicates that the operating system did not find any activity that could handle this intent, ACTION_SEND. For this, it is precisely used in the try / catch code, since you can not be sure that there is an activity that can handle your intent.

You must make sure to add the catch to "catch" this type of error and show the toast with the message that WhatsApp is not installed on the device

  try{
        startActivity(whatsappIntent);
    }catch (ActivityNotFoundException ex){

        Toast.makeText(MainActivity.this, "Whastapp No Instalado", Toast.LENGTH_SHORT).show();

    }
    
answered by 25.10.2018 в 18:16