Lecture 6 WebView & Sensors
Lecture 6 WebView & Sensors
WebView
Sensors
• WebView is a view that display web pages inside your application.
In order to use it, you have to get a reference of this view in Java file. To get a reference, create
an object of the class WebView. Its santax is:
browser.loadUrl("http://www.abc.com");
If you click on any link inside the webpage of the WebView , that page will not be loaded
inside your WebView. In order to do that you need to extend your class from WebViewClient
and override its method. Its syntax is −
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Permissions
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = ed1.getText().toString();
wv1.getSettings().setLoadsImagesAutomatically(true);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv1.loadUrl(url);
}
});
}
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Android Sensors
Android Sensors Overview
Android Sensors:
MIC
Camera
Temperature
Location (GPS or Network)
Orientation
Accelerometer
Proximity
Pressure
Light
Supported Android Sensors
The following is a list of the sensor-types currently available.
➤ Sensor.TYPE_ACCELEROMETER A three-axis accelerometer sensor that returns the current acceleration
along three axes in m/s2. The accelerometer is explored in greater detail later in this chapter.
➤ Sensor.TYPE_GYROSCOPE A gyroscopic sensor that returns the current device orientation on three
axes in degrees.
➤Sensor.TYPE_LIGHT An ambient light sensor that returns a single value describing the ambient
illumination in lux. A light sensor is commonly used to dynamically control the screen brightness.
➤ Sensor.TYPE_MAGNETIC_FIELD A magnetic field sensor that finds the current magnetic field in
microteslas along three axes.
➤ Sensor.TYPE_ORIENTATION An orientation sensor that returns the device orientation on three axes in
degrees.
➤ Sensor.TYPE_PRESSURE A pressure sensor that returns a single value, the current pressure exerted on
the device in kilopascals.
➤ Sensor.TYPE_PROXIMITY A proximity sensor that indicates the distance between the device and the
object in meters. How a target object is selected, and the distances supported, will depend on the
hardware implementation of the proximity detector. A typical use for the proximity sensor is to detect
when the device is being held up against the user’s ear and to automatically adjust screen brightness.
Sensor.TYPE_TEMPERATURE A thermometer that returns temperature in degrees Celsius. The
temperature returned may be the ambient room temperature, device battery temperature, or remote
sensor temperature, depending on the hardware implementation.
Getting the Relevant System
Service
The first step in registering is to obtain a reference to the relevant sensor manager
SensorManager senMgr;
}
Getting list of sensors supported.
sMgr = (SensorManager)this.getSystemService(SENSOR_SERVICE);
List<Sensor> list = sMgr.getSensorList(Sensor.TYPE_ALL);
Registering for Sensor Updates
The SensorManager handles registrations for
Accelerometer, Temp, Proximity, Gyro, . . . .
In order for an object to receive updates from sensors, it must implement the
SensorEventListener interface
Once the SensorManager is obtained, you must obtain a reference to the specific
sensor you are interested in updates from
The arguments passed into the registerListener method determine the sensor that
you are connected to and the rate at which it will send you updates
public onCreate() {
.
.
.
senMgr = (SensorManager)getSystemService(SENSOR_SERVICE);
sen_acc = senMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
senMgr.registerListener(this, sen_acc,
SensorManager.SENSOR_DELAY_NORMAL);
}
The SensorEventListener Interface
Because there is one interface for multiple types of sensors, listening to multiple
sensors requires switching on the type of event (or creating separate listener objects)
Also forces registration at the same rate per listener
Simple approach:
public class MyActivity … implements SensorListener{
}
}
}
}