Android System Services Android System Services The underlying

  • Slides: 11
Download presentation
Android System Services

Android System Services

Android System Services The underlying Android OS makes a variety of services available to

Android System Services The underlying Android OS makes a variety of services available to the app via system calls Connectivity. Manager: info about network Location. Manager: accessing location services Notification. Manager: alerting the user

Connectivity. Manager Provides information about the networking capabilities of the device Some applications may

Connectivity. Manager Provides information about the networking capabilities of the device Some applications may behave differently depending on whether the device has network connectivity, and what kind There are no special APIs for doing networking

In a class that extends Activity. . . 1 2 3 4 5 6

In a class that extends Activity. . . 1 2 3 4 5 6 7 8 9 10 11 12 13 Connectivity. Manager manager = (Connectivity. Manager) get. System. Service(Context. CONNECTIVITY_SERVICE); Network. Info info = manager. get. Active. Network. Info(); if (info. is. Connected()) { // see what type of network it is int type = info. get. Type(); // check if it's wifi connectivity if (type == Connectivity. Manager. TYPE_WIFI). . . // check if it's 3 G connectivity else if (type == Connectivity. Manager. TYPE_MOBILE). . . }

Location. Manager Provides information about the geophysical location of the device Can quickly get

Location. Manager Provides information about the geophysical location of the device Can quickly get the last known location, or register to be notified when the location changes GPS is more accurate but slower (and tends to use more battery)

In a method in the class that extends Activity. . . 1 2 3

In a method in the class that extends Activity. . . 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Location. Manager location. Manager = (Location. Manager) get. System. Service(Context. LOCATION_SERVICE); String provider = Location. Manager. GPS_PROVIDER; // Or use Location. Manager. NETWORK_PROVIDER // now get the “current” location Location last. Known. Location = location. Manager. get. Last. Known. Location(provider); if (last. Known. Location != null) { // do whatever you want with the Location object // you can get: latitude, longitude, altitude // speed, bearing // time, accuracy. . . }

In a method in the class that extends Activity 1 2 3 4 5

In a method in the class that extends Activity 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Location. Manager location. Manager = (Location. Manager) get. System. Service(Context. LOCATION_SERVICE); // Define a listener that responds to location updates Location. Listener location. Listener = new Location. Listener() { // Called when a new location is found by the location provider. public void on. Location. Changed(Location location) { // do whatever with the Location object } public void on. Status. Changed(String provider, int status, Bundle extras) {} public void on. Provider. Enabled(String provider) {} }; public void on. Provider. Disabled(String provider) {} // Register listener with Location Manager to receive updates location. Manager. request. Location. Updates( Location. Manager. GPS_PROVIDER, 0, // time interval 0, // distance interval location. Listener);

Configuration and Control Be sure to add the following line to the manifest <uses-permission

Configuration and Control Be sure to add the following line to the manifest <uses-permission android: name="android. permission. ACCESS_FINE_LOCATION" /> Control GPS coordinates through Eclipse – Window → Show View → Other → Android → Emulator Control – enter coordinates manually, or use GPX or KML file

Notification. Manager Allows an application to put a message in the status bar at

Notification. Manager Allows an application to put a message in the status bar at the top of the display Specify the icon and “alert text” to appear Also specify the “title” and the “message” that are shown when the user pulls down the status bar Can add sound, vibration, flashing lights, etc.

In a class that extends Activity. . . 1 2 3 4 5 private

In a class that extends Activity. . . 1 2 3 4 5 private static final int NOTIFICATION_ID = 1; // call this method to show the notification/alert protected void show. Notification() { String ns = Context. NOTIFICATION_SERVICE; Notification. Manager m. Notification. Manager = (Notification. Manager) get. System. Service(ns); int icon = R. drawable. icon; Char. Sequence ticker. Text = "Alert!"; // text at top long when = System. current. Time. Millis(); 6 7 8 9 10 Notification notification = new Notification(icon, ticker. Text, when); Context context = get. Application. Context(); Char. Sequence content. Title = "Important message"; // title Char. Sequence content. Text = "Hello World!"; // message Intent notification. Intent = new Intent(this, this. get. Class()); Pending. Intent content. Intent = Pending. Intent. get. Activity( this, 0, notification. Intent, 0); 11 12 13 14 15 16 17 18 19 20 notification. set. Latest. Event. Info(context, content. Title, content. Text, content. Intent); } m. Notification. Manager. notify(NOTIFICATION_ID, notification);