无法从 Android Leanback 应用中的 VideoDetailsFragment 访问 getPackageManager GetActivityInfo

Unable to access getPackageManager GetActivityInfo from VideoDetailsFragment in Android Leanback app

我想为 Android TV 制作一个应用程序,所以我使用 Android Studio 中默认的 Android TV activity 作为我项目的模板,我想在 VideoDetailsFragment 中添加一个按钮,此按钮仅在包管理器检测到辅助 activity 处于活动状态后才会出现。

如果包管理器检测到我的辅助 activity 处于活动状态并且 运行ning 它应该显示一个按钮来禁用它,如果 activity 被禁用它应该显示一个按钮来启用它,但是...包管理器无法获取 activity 的名称,它实际上无法获取任何包或 activity 的名称,它总是显示 NameNotFoundException我的两个活动都在清单中 android:name="android.intent.action.MAIN" 因为我想从启动器为两者添加启动意图,但是辅助 activity 有标签 android:enabled="false" 因为我想放置按钮在 VideoDetailsFragment 中应该启用和禁用 activity.

这是我的清单:

<?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>

这是 VideoDetailsFragment 代码:

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);
}

这里是 android studio 中错误显示的屏幕截图:

这是另一个显示建议答案之一的屏幕截图,但它也不起作用:

我将其包装到一个 try catch 中,但是当我 运行 我的应用程序时,它总是在 Android 监视器中抛出 NameNotFoundException

我做错了什么?

您需要定义正确的组件名称:

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

试一试:

try {
    ActivityInfo info = pm.getActivityInfo(cn, 0);
} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
}