Android Open Source - android-sensor-example List Sensor Fragment






From Project

Back to project page android-sensor-example.

License

The source code is released under:

Apache License

If you think the Android project android-sensor-example listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package be.hcpl.android.sensors;
/* www.  jav  a2  s  . c o  m*/
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.List;

import be.hcpl.android.sensors.core.BaseFragment;

/**
 * A fragment that checks all available sensors for this device
 */
public class ListSensorFragment extends BaseFragment {


    // you'll always need a reference to the sensorManager
    private SensorManager mSensorManager;

    // and we will populate this list
    private List<Sensor> deviceSensors;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // we need to retrieve the system service on the parent activity
        mSensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);

        // this is how to list all sensors
        deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);

        // check for a specific type of sensor
        //if (mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) != null){
            // Success! There's a magnetometer.
        //}
        //else {
            // Failure! No magnetometer.
        //}

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_list_sensors, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // TODO list the information here
        // get the listview from our layout
        ListView listView = (ListView)view.findViewById(R.id.list_sensors);

        // and populate it with the most basic view available for listviews, a single text view
        // only made final so we can refer to it in our anonymuous innerclass for clickListener impl
        final ArrayAdapter<Sensor> listAdapter = new ArrayAdapter<Sensor>(getActivity(),
                android.R.layout.simple_list_item_1, android.R.id.text1, deviceSensors.toArray(new Sensor[deviceSensors.size()]));
        // set it to our listview
        listView.setAdapter(listAdapter);

        // on click we want to open the sensor data fragment with information about the selected
        // sensor
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Sensor sensor = (Sensor)listAdapter.getItem(i);

                // and pass it on to the data fragment in a bundle
                Bundle bundle = new Bundle();
                bundle.putInt(SensorDataFragment.KEY_SENSOR_TYPE, sensor.getType());
                getParentActivity().switchFragment(SensorDataFragment.getInstance(bundle));

            }
        });
    }
}




Java Source Code List

be.hcpl.android.sensors.ListSensorFragment.java
be.hcpl.android.sensors.MainActivity.java
be.hcpl.android.sensors.NavigationDrawerFragment.java
be.hcpl.android.sensors.ScheduleServiceFragment.java
be.hcpl.android.sensors.SensorDataFragment.java
be.hcpl.android.sensors.WelcomeFragment.java
be.hcpl.android.sensors.core.BaseFragment.java
be.hcpl.android.sensors.service.SensorBackgroundService.java