-3

I have tried to open the Gmail app via this:

import android.content.Intent;


public class GmailApp {
    public static void OpenGmail() {

        Intent intent = getPackageManager().getLaunchIntentForPackage("com.google.android.gm");
        if(intent != null){
            startActivity(intent);
        }

    }
}

It says it can't find it. But how?

I've read this article right here Can't find getPackageManager() method in android

But I don't understand it.

Can anyone give me an example code that will work?

Hugo
  • 1
  • 1
  • See if this answers your question : https://stackoverflow.com/questions/3470042/intent-uri-to-launch-gmail-app – oyeraghib Sep 12 '22 at 22:11
  • `getPackageManager` is a method on a `Context`, which your class is not... Pass in a context if you want to call that. It's literally the [first answer](https://stackoverflow.com/a/22396570/9473786) in the question you linked – Tyler V Sep 13 '22 at 04:39
  • Does this answer your question? [Can't find getPackageManager() method in android](https://stackoverflow.com/questions/22396382/cant-find-getpackagemanager-method-in-android) – Robert Sep 13 '22 at 16:16

1 Answers1

-1

There are many potential causes for the symptoms you are describing, but one potential cause is the package visibility restrictions introduced in Android 11.

https://developer.android.com/training/package-visibility/automatic

Try adding this to your manifest:

    <queries>
        <package android:name="com.google.android.gm" />
    </queries>
Maurice Lam
  • 1,577
  • 9
  • 14