Saturday, March 8, 2014

List features available on the Android system

PackageManager.getSystemAvailableFeatures() return FeatureInfo[], a list of features that are available on the system. To get PackageManager object, call the method getPackageManager().

Remark: this example build from Project Wizard of the new Android SDK Tools 22.6, MainActivity extends android.support.v7.app.ActionBarActivity.

MainActivity.java
package com.example.androidfeatures;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.pm.FeatureInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends ActionBarActivity {
 
 static PackageManager packageManager;
 static TextView textViewInfo;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  if (savedInstanceState == null) {
   getSupportFragmentManager().beginTransaction()
     .add(R.id.container, new PlaceholderFragment()).commit();
  }
  
  packageManager = getPackageManager();
  Toast.makeText(MainActivity.this, "onCreate", Toast.LENGTH_SHORT).show();
  
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {

  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // Handle action bar item clicks here. The action bar will
  // automatically handle clicks on the Home/Up button, so long
  // as you specify a parent activity in AndroidManifest.xml.
  int id = item.getItemId();
  if (id == R.id.action_settings) {
   return true;
  }
  return super.onOptionsItemSelected(item);
 }

 /**
  * A placeholder fragment containing a simple view.
  */
 public static class PlaceholderFragment extends Fragment {

  public PlaceholderFragment() {
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
   View rootView = inflater.inflate(R.layout.fragment_main, container,
     false);
   
   
   
   Toast.makeText(getActivity(), "PlaceholderFragment.onCreateView", Toast.LENGTH_SHORT).show();
   textViewInfo = (TextView)rootView.findViewById(R.id.info);
   
   FeatureInfo[] featureInfos = packageManager.getSystemAvailableFeatures();
   
   if(featureInfos == null){
    textViewInfo.setText("NO feature available on the system!!!");
   }else{
    String strInfo = "";
    for(FeatureInfo info : featureInfos){
     strInfo += info.name + "\n";
     strInfo += info.toString() + "\n\n";
    }
    
    textViewInfo.setText(strInfo);
   }

   return rootView;
  }
 }

}

/res/layout/fragment_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androidfeatures.MainActivity$PlaceholderFragment" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </ScrollView>

</LinearLayout>

On Nexus 7 running Android 4.4.2

On HTC One X running Android 4.2.2

On Nexus One running Android 2.3.6

Related:
Check if a specified feature supported in your Android system

No comments: