-1

I try to launch another app in my app

There is 2 methods I have tried.

Method 1

    Intent intent = new Intent(Intent.ACTION_MAIN);
    ComponentName componentName = new ComponentName("com.b_app", "com.b_app.MainActivity");
    intent.setComponent(componentName);
    startActivity(intent);

Method 2

        Intent intent =  getPackageManager().getLaunchIntentForPackage("com.b_app");
        if (intent != null) {
            startActivity(intent);//null pointer check in case package name was not found
        }
        else Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();

Method 1 gives me error :

Unable to find explicit activity class {com.b_app/com.b_app.MainActivity}; have you declared this activity in your AndroidManifest.xml?

Method 2 the Toast shows Error (my else condition),meaning getPackageManager() returns null..

My Manifest in b_app

   <activity android:name="com.b_app.MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

and I'm pretty sure the b_app is installed in my smartphone..

How can I solve it?

achicn3
  • 31
  • 8

2 Answers2

2

the Toast shows Error (my else condition),meaning getPackageManager() returns null

No. You would be getting a NullPointerException in that case. In your case, getLaunchIntentForPackage() is returning null.

That, plus your other symptoms, suggests that you do not have an application whose applicationId is com.b_app installed on this device.

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
  • ok... I check my applicationId in b_app ,and which is com.xxxx.xxxx , so com.b_app has edit to com.xxxx.xxxx ? – achicn3 Nov 25 '18 at 16:11
  • @achicn3: Yes. The value that you use for the `ComponentName` and for `getLaunchIntentForPackage()` needs to be the `applicationId` of the other app. – CommonsWare Nov 25 '18 at 16:28
0

There is a little chance to meet in my sitution.. One of my app was returned null for "getLaunchIntentForPackage" and I was recognize My app is not shown in Device Although it is installed.. Recently I implement Firebase Dynamic... And my application manifest configured as wrongly

Here is wrong one :

<intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>

 </intent-filter>

Correct one is

  <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>

            </intent-filter>

If you implement wrongly your application manifest, User cant install your app... because Google Play "Open" button app is geting invisible

Ucdemir
  • 2,070
  • 2
  • 21
  • 35