• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
CS 683 Emerging Technologies Fall Semester, 2008 Doc 21 ...
 

CS 683 Emerging Technologies Fall Semester, 2008 Doc 21 ...

on

  • 1,417 views

 

Statistics

Views

Total Views
1,417
Views on SlideShare
1,417
Embed Views
0

Actions

Likes
0
Downloads
4
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    CS 683 Emerging Technologies Fall Semester, 2008 Doc 21 ... CS 683 Emerging Technologies Fall Semester, 2008 Doc 21 ... Presentation Transcript

    • CS 683 Emerging Technologies Fall Semester, 2008 Doc 21 Android Services Dec 4 2008 Copyright ©, All rights reserved. 2008 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http:// www.opencontent.org/openpub/) license defines the copyright on this document.
    • References Google Android Documentation, http://code.google.com/android/documentation.html 2
    • Examples See Android ApiDemos com.example.android.apis.app classes 3
    • Keystroke Example Show how to react to keystrokes not handled by views 4
    • Keystroke Activity Methods public boolean onKeyDown(int keyCode, KeyEvent event) Called when a key was pressed down and not handled by any of the views inside of the activity public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) Called when multiple down/up pairs of the same key have occurred in a row. public boolean onKeyUp(int keyCode, KeyEvent event) Called when a key was released and not handled by any of the views inside of the activity 5
    • KeyDownExample public class GoodStuff extends Activity { private Button keyText; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); keyText = (Button) this.findViewById(R.id.update); } public boolean onKeyDown(int keyCode, KeyEvent event) { keyText.setText(String.valueOf(keyCode)); switch (keyCode) { case (KeyEvent.KEYCODE_T): showToast(); break; default: return super.onKeyDown(keyCode, event); } return true; } 6 Warning KeyEvent.KEYCODE_T is not the ascii of t or T
    • Toast private void showToast() { keyText.setText("N"); Toast.makeText(this, "This is a test", Toast.LENGTH_LONG).show(); } 7 Toast displays a text dialog that will vanish is a short time.
    • Service Examples public class GoodStuff extends Activity { private Button keyText; Will use keystroke public boolean onKeyDown(int keyCode, KeyEvent event) { to start examples keyText.setText(String.valueOf(keyCode)); switch (keyCode) { case (KeyEvent.KEYCODE_T): showToast(); break; case (KeyEvent.KEYCODE_A): etc. default: return super.onKeyDown(keyCode, event); } return true; } 8
    • Service A component Runs in the background for an indefinite period of time Not interacting with the user 9
    • Existing Services ACTIVITY_SERVICE  Interacting with the global system state.  ALARM_SERVICE  Receiving intents at a time of your choosing.  AUDIO_SERVICE  Management of volume, ringer modes and audio routing.  CLIPBOARD_SERVICE  Accessing and modifying global clipboard.  CONNECTIVITY_SERVICE  Management of network connections.  KEYGUARD_SERVICE  Controlling keyguard.  LAYOUT_INFLATER_SERVICE  Inflating layout resources in this context.  LOCATION_SERVICE  Controlling location updates.  NOTIFICATION_SERVICE  Informing the user of background events.  POWER_SERVICE  Controlling power management SEARCH_SERVICE  Handling searches.  SENSOR_SERVICE  Accessing sensors.  TELEPHONY_SERVICE  Management the telephony features of the device.  VIBRATOR_SERVICE  WALLPAPER_SERVICE  Accessing wallpapers.  WIFI_SERVICE  Management of Wi-Fi access.  WINDOW_SERVICE  Accessing the system's window manager.  10
    • Vibrate Service private void vibrate() { keyText.setText("V"); Vibrator vibrate = (Vibrator) getSystemService(VIBRATOR_SERVICE); vibrate.vibrate(2000); } 11
    • Vibrate Permission <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="edu.sdsu.cs683" android:versionCode="1" android:versionName="1.0.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".GoodStuff" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.VIBRATE"></uses- permission> </manifest> 12
    • Notifications 13
    • Setting the Notification private void notifyUser() { NotificationManager noteManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification note = new Notification(R.drawable.happy, "This is a test", System.currentTimeMillis()); note.defaults = Notification.DEFAULT_SOUND; PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, GoodStuff.class), 0); note.setLatestEventInfo(this, "Title Text", "Message Text", contentIntent); // We use a layout id because it is a unique number. noteManager.notify(R.layout.main, note); } 14
    • PendingIntent getActivity PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags) context Context in which this PendingIntent should start the activity requestCode not used intent Intent of the activity to be launched flags FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT 15
    • Turning It Off private void notificationOff() { NotificationManager noteManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); noteManager.cancel(R.layout.main); } 16
    • Alarms PendingIntent selfIntent; private void alarmOn() { selfIntent = PendingIntent.getActivity(this, 0, new Intent(this, GoodStuff.class), 0); long alarmTime = SystemClock.elapsedRealtime() + 60* 1000; AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, alarmTime, selfIntent); Toast.makeText(this, "Set alarm",Toast.LENGTH_LONG).show(); } private void alarmOff() { AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); } 17