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:
Here is another screenshot showing one of the suggested answer, but it doesn't work neither:
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?