0

I want to make an app for Android TV, so I am using the default Android TV activity in Android Studio as a template for my project, I want to add a button in the VideoDetailsFragment, this button only appears after the package manager detects that a secondary activity is active.

If the package manager detects my secondary activity is active and running it should show a button to disable it and if the activity is disabled it should show a button to enable it, but... the package manager can't get the name of the activity, it can't actually get the name of any package or activity, it always show NameNotFoundException both of my activities are android:name="android.intent.action.MAIN" in the Manifest because I want to add a launch intent for both from the launcher, but the secondary activity has the label android:enabled="false" because the button I want to put in the VideoDetailsFragment is supposed to enable and disable that activity.

Here is my manifest:

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

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

<uses-feature
    android:name="android.hardware.touchscreen"
    android:required="false" />
<uses-feature
    android:name="android.software.leanback"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.Leanback">
    <activity
        android:name=".MainActivity"
        android:banner="@mipmap/banner"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:logo="@mipmap/banner"
        android:screenOrientation="landscape">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".DetailsActivity" />
    <activity android:name=".PlaybackOverlayActivity" />
    <activity android:name=".BrowseErrorActivity" />
    <activity android:name=".SecondaryActivity"
        android:icon="@mipmap/ic_launcher"
        android:banner="@mipmap/banner"
        android:label="@string/app_name"
        android:screenOrientation="landscape"
        android:enabled="false">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Here is the VideoDetailsFragment code:

private void setupDetailsOverviewRow() {
    Log.d(TAG, "doInBackground: " + mSelectedMovie.toString());
    final DetailsOverviewRow row = new DetailsOverviewRow(mSelectedMovie);
    row.setImageDrawable(getResources().getDrawable(R.drawable.default_background));
    int width = Utils.convertDpToPixel(getActivity()
            .getApplicationContext(), DETAIL_THUMB_WIDTH);
    int height = Utils.convertDpToPixel(getActivity()
            .getApplicationContext(), DETAIL_THUMB_HEIGHT);
    Glide.with(getActivity())
            .load(mSelectedMovie.getCardImageUrl())
            .centerCrop()
            .error(R.drawable.default_background)
            .into(new SimpleTarget<GlideDrawable>(width, height) {
                @Override
                public void onResourceReady(GlideDrawable resource,
                                            GlideAnimation<? super GlideDrawable>
                                                    glideAnimation) {
                    Log.d(TAG, "details overview card image url ready: " + resource);
                    row.setImageDrawable(resource);
                    mAdapter.notifyArrayItemRangeChanged(0, mAdapter.size());
                }
            });
    /******************************/
    /*HERE IS THE PROBLEMATIC CODE*/
    /******************************/
    PackageManager pm = getActivity().getPackageManager();
    ComponentName cn = new ComponentName(getActivity(), "com.valecast.myapp.SecondaryActivity");
    ActivityInfo info = pm.getActivityInfo(cn, 0); //<===IT NEVER LOCATE THIS
    if (info != null && info.enabled) {
        // Component is enabled
        row.addAction(new Action(ACTION_BUY, "it works"));
    } else {
        // Component is disabled
        row.addAction(new Action(ACTION_BUY, "it doesn't"));
    }
    /******************************/
    row.addAction(new Action(ACTION_WATCH_TRAILER, getResources().getString(
            R.string.watch_trailer_1), getResources().getString(R.string.watch_trailer_2)));
    row.addAction(new Action(ACTION_RENT, getResources().getString(R.string.rent_1),
            getResources().getString(R.string.rent_2)));
    row.addAction(new Action(ACTION_BUY, getResources().getString(R.string.buy_1),
            getResources().getString(R.string.buy_2)));

    mAdapter.add(row);
}

And here is a screenshot of the error show in android studio:

Screenshot

Here is another screenshot showing one of the suggested answer, but it doesn't work neither:

enter image description here

I wrapped that into a try catch, but when I run my app it always throw the NameNotFoundException in the Android Monitor.

What I am doing wrong?

Valerie Castle
  • 113
  • 1
  • 11

1 Answers1

1

You need to define the proper ComponentName:

new ComponentName("com.valecast.myapp", "com.valecast.myapp.SecondaryActivity");

and have a try catch:

try {
    ActivityInfo info = pm.getActivityInfo(cn, 0);
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}
petrumo
  • 1,116
  • 9
  • 18
  • Thank you, but it doesn't seems to work that way neither, I updated my question with a second screenshot. – Valerie Castle Dec 06 '16 at 09:26
  • you need to wrap it with a try catch though – petrumo Dec 06 '16 at 09:28
  • I updated the second screenshot again, the same error, but this time in android Monitor – Valerie Castle Dec 06 '16 at 09:57
  • use the code to display all the activities in there – petrumo Dec 06 '16 at 10:22
  • PackageInfo info = pm.getPackageInfo("com.valecast.myapp", PackageManager.GET_ACTIVITIES); System.out.println("Info :" + info + " : " + info.activities + " : " + info.packageName); for (ActivityInfo ai : info.activities) { System.out.println("Activity:" + ai.name); } – petrumo Dec 06 '16 at 10:23
  • also as you have 2 main activities check this post for adding taskAffinity http://stackoverflow.com/questions/15526805/two-main-activities-in-androidmanifest-xml – petrumo Dec 06 '16 at 10:30
  • oh, I get it, but my activities don't have the `` they have the `` I removed that line from the secondary activity, but it doesn't show any difference, what I did notice is that the `System.out.println` doesn't show the SecondaryActivity, is looks like it's not there... any clues? – Valerie Castle Dec 06 '16 at 10:41
  • it looks like the `android:enabled="false"` on my manifest disables the activity to show in the logs, but that's the whole point, I want to enable and disable that activity with the button I want to show. – Valerie Castle Dec 06 '16 at 10:58
  • how do you enable the activity? – petrumo Dec 06 '16 at 11:03
  • Like on this previous question [link](http://stackoverflow.com/questions/40975139/enable-or-disable-launch-intent-filter-activities-with-one-button/40976859?noredirect=1#comment69172450_40976859) – Valerie Castle Dec 06 '16 at 11:05
  • 1
    I just got it, the problem is that my SecondaryActivity is disabled by default, then the script will look for it in order to display the enable button, so there is no way I can show the enable button since android doesn't find the SecondaryActivity, I need another approach. – Valerie Castle Dec 06 '16 at 11:23