Home > Mobile >  Android Intent to open other apps returning Null
Android Intent to open other apps returning Null

Time:07-29

I tried to create a simple app which after clicking a button opens whatsapp. I created an intent to open whatsapp . But while clicking the button , the intent returns null

package com.myown.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Telephony;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private final String LOG_TAG = MainActivity.class.getSimpleName();
    Button btn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.whatsapp");
                Log.i(LOG_TAG,"Onclick called");

                if(LaunchIntent!=null)
                {startActivity(LaunchIntent);}
                else
                {
                    Toast.makeText(MainActivity.this,"Application Not Found!",Toast.LENGTH_LONG).show();
                }
            }
        });
    }


}

After Clicking the button , the intent is returning null, and this is the log message i received

2022-07-28 08:47:25.211 26757-26757/com.myown.myapplication D/ViewRootImpl: enqueueInputEventMotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=462.0, y[0]=1368.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x2, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=131790073, downTime=131790073, deviceId=3, source=0x1002, displayId=0 }
2022-07-28 08:47:25.212 26757-26757/com.myown.myapplication D/ViewRootImpl[MainActivity]: processMotionEvent MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=462.0, y[0]=1368.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x2, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=131790073, downTime=131790073, deviceId=3, source=0x1002, displayId=0 }
2022-07-28 08:47:25.218 26757-26757/com.myown.myapplication D/ViewRootImpl[MainActivity]: dispatchPointerEvent handled=true, event=MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=462.0, y[0]=1368.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x2, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=131790073, downTime=131790073, deviceId=3, source=0x1002, displayId=0 }
2022-07-28 08:47:25.291 26757-26757/com.myown.myapplication D/ViewRootImpl[MainActivity]: processMotionEvent MotionEvent { action=ACTION_UP, actionButton=0, id[0]=0, x[0]=462.0, y[0]=1368.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x2, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=131790153, downTime=131790073, deviceId=3, source=0x1002, displayId=0 }
2022-07-28 08:47:25.292 26757-26757/com.myown.myapplication D/ViewRootImpl[MainActivity]: dispatchPointerEvent handled=true, event=MotionEvent { action=ACTION_UP, actionButton=0, id[0]=0, x[0]=462.0, y[0]=1368.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x2, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=131790153, downTime=131790073, deviceId=3, source=0x1002, displayId=0 }
2022-07-28 08:47:25.300 26757-26757/com.myown.myapplication I/MainActivity: Onclick called

This is my xml code:

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <Button
        android:id="@ id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="157dp"
        android:layout_marginTop="345dp"
        android:layout_marginEnd="160dp"
        android:layout_marginBottom="338dp"
        android:text="@string/openwa"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

When I am googling this issue , i got a solution from official Whatsapp Link:https://faq.whatsapp.com/1530794880435103/?locale=en_US

I tried this code segment in my code:

Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send."); sendIntent.setType("text/plain"); startActivity(sendIntent);
sendIntent.setPackage("com.whatsapp");

It now works fine

CodePudding user response:

Try this set of code in our activity to open another application(here Whatsapp) from you own android application.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      openToChat(view, " 919000090000")
    }
});
}

public static void openToChat(View v, String toNumber) {
    String url = "https://wa.me/"   toNumber;
    try {
        PackageManager pm = v.getContext().getPackageManager();
        pm.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES);
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        v.getContext().startActivity(i);
    } catch (PackageManager.NameNotFoundException e) {
        Toast.makeText(MainActivity.activity, "Whatsapp app not installed in your phone", Toast.LENGTH_SHORT).show();
        // v.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
    }
}
  • Related