2016-12-06 40 views
0

我想爲Android TV的應用程序,所以我用Android Studio中默認的Android電視活動作爲我的項目模板,我想添加一個按鈕VideoDetailsFragment,只有在包管理器檢測到輔助活動處於活動狀態後纔會出現此按鈕。無法從VideoDetailsFragment訪問getPackageManager GetActivityInfo Android中的Leanback應用

如果程序包管理器檢測我的次要活動活躍,就應該表現出一個按鈕來禁用它,如果活動被禁用它應該顯示一個按鈕來啓用它,但是......包管理器不能得到該活動的名稱,它實際上並不能得到任何包或活動的名稱,它總是顯示NameNotFoundException我的兩個活動都android:name="android.intent.action.MAIN"的清單,因爲我想添加一個啓動Intent兩個從發射,但二級活動有標籤android:enabled="false"因爲我想放在VideoDetailsFragment按鈕應該啓用和禁用該活動。

這裏是我的清單:

<?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 s tudio:

Screenshot

下面是該建議的答案之一另一屏幕,但它不工作也不:

enter image description here

我那裹成一個嘗試捕捉,但是當我運行我的應用程序,它總是在Android監視器中投擲NameNotFoundException

我做錯了什麼?

回答

1

您需要定義適當的組件名稱:

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

,並有一個嘗試捕捉:

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

謝謝你,但它似乎並不工作方式都不是,我更新了我的問題與第二個屏幕截圖。 –

+0

你需要用try catch來包裝它雖然 – petrumo

+0

我更新了第二個屏幕截圖,同樣的錯誤,但是這次在android monitor –