Saturday, March 8, 2014

Check if a specified feature supported in your Android system

To check if the devices supports the specified feature, call packageManager.hasSystemFeature(<packagemanager .feature_...="">) method. Returns true if the devices supports the feature, else false. Read http://developer.android.com/reference/android/content/pm/PackageManager.html for the FEATURE list.

MainActivity.java
package com.example.androidfeatures;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
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;

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);
   
   String strInfo = "";
   
   strInfo += "has System Feature - BLUETOOTH: " +
     packageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH) + "\n";
   strInfo += "has System Feature - BLUETOOTH_LE: " +
     packageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE) + "\n";
   strInfo += "has System Feature - CAMERA_ANY: " +
     packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY) + "\n";
   strInfo += "has System Feature - CAMERA: " +
     packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA) + "\n";
   strInfo += "has System Feature - CAMERA_AUTOFOCUS: " +
     packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_AUTOFOCUS) + "\n";
   strInfo += "has System Feature - NFC: " +
     packageManager.hasSystemFeature(PackageManager.FEATURE_NFC) + "\n";
   strInfo += "has System Feature - USB_ACCESSORY: " +
     packageManager.hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY) + "\n";
   strInfo += "has System Feature - USB_HOST: " +
     packageManager.hasSystemFeature(PackageManager.FEATURE_USB_HOST) + "\n";
   strInfo += "has System Feature - WIFI: " +
     packageManager.hasSystemFeature(PackageManager.FEATURE_WIFI) + "\n";
   strInfo += "has System Feature - WIFI_DIRECT: " +
     packageManager.hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT) + "\n";

   textViewInfo.setText(strInfo);
   
   return rootView;
  }
 }

}

/res/layout/fragment_main.xml file refer to the last post.


No comments:

Post a Comment