Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getlaunchintentforpackage returning null

Can someone tell me please why this gives me null on the variable intent?

public class MainActivity extends AppCompatActivity {
    private static String action = "com.google.android.youtube";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onResume() {
        super.onResume();
        this.launch();
    }

    public void launch() {
        PackageManager pm = getPackageManager();
        Intent intent = pm.getLaunchIntentForPackage(action);
        System.out.println("debug: " + intent);
        //startActivity(intent);
    }
}

i test this on a pixel 4 and i never do uninstall youtube. also, sidequest list the app and gives me the package com.google.android.youtube

like image 390
alegro sandermann Avatar asked Oct 27 '20 09:10

alegro sandermann


2 Answers

For many (perhaps most) PackageManager methods, on Android 11 and higher, you need to take into account package visibility rules and add a <queries> element to your manifest to declare what third-party apps you are interest in discovering or analyzing.

like image 125
CommonsWare Avatar answered Sep 23 '22 02:09

CommonsWare


Add below code in manifiest at below level hope it will work with you.

<manifest
    <application
            >
        </application>

        <queries>
        <intent>
         <action android:name="com.google.android.youtube.api.service.START"/>
        </intent>
        </queries>
    </manifest>
like image 31
Ejaz Ahmad Avatar answered Sep 22 '22 02:09

Ejaz Ahmad