DOC PREVIEW
SDSU CS 696 - Android Services

This preview shows page 1-2-3-18-19-36-37-38 out of 38 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 38 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 696 Emerging Web and Mobile TechnologiesSpring Semester, 2011Doc 24 Android ServicesApr 26, 2011Copyright ©, All rights reserved. 2011 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http://www.opencontent.org/opl.shtml) license defines the copyright on this document.Tuesday, April 26, 2011Service2Runs in the backgroundNo user interactionRuns indefinitelyRuns in thread of hosting processCreate new thread to do workTuesday, April 26, 2011Service Lifecycle3Local ServiceStarted when Context.startService(Intent) is calledThen service's methods are calledonCreate() onStart(Intent, int)Service runs until one of following is calledContext.stopService(Intent)stopSelf()Only one of a service runsImplement onDestroy() to clean upTuesday, April 26, 2011Service Lifecycle4Remote ServiceStarted when bindService (Intent, ServiceConnection, int flags) is calledThen service's method onCreate() is called if need Client received IBinder object to communicate with serviceService runs until no clients are bound to the serviceImplement onDestroy() to clean upTuesday, April 26, 2011Process Lifecycle5Services are killed if low on memoryIf currently executing onCreate(), onStart(), or onDestroy() service not killedIf service has been started process is less important currently visible processesprocess is more important than processes not visible & don't have serviceIf clients are bound to the service process is as important as the most important clientTuesday, April 26, 2011IntentService6IntentServiceSubclass of ServiceCreates worker thread for youonHandleIntent() is called for each intent requestService is stopped when all requests are handledDefault implementation of onBind()onStartCommand()Tuesday, April 26, 2011Permissions7Need to declare service in manifest<service android:name=".AvitarService" />Other applications need <uses-permission> to use serviceTuesday, April 26, 2011Services & Battery life8Waking up a service 6 times an hour for 8 seconds each timeCuts the battery life in halfCan schedule updates at approximate times so system can run multiple requests at same timeTuesday, April 26, 20119Notification without ServiceTuesday, April 26, 2011Notification Without Service10Tuesday, April 26, 2011Send Notification on Button Click11public class ServiceExample extends Activity implements View.OnClickListener { private static final int NOTIFY_ID = 1123; private int count = 0; public void onClick(View v) { TimerTask task = new TimerTask() { public void run() { sendNotification(); } }; new Timer().schedule(task, 5000); } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button ok = (Button) findViewById(R.id.start); ok.setOnClickListener(this); }Tuesday, April 26, 2011The Notification12 private void sendNotification() { Notification note = new Notification(R.drawable.icon, "Wake Up!", System.currentTimeMillis()); PendingIntent intentToStart = PendingIntent.getActivity(this, 0, new Intent(this, ServiceExample.class), Intent.FLAG_ACTIVITY_NEW_TASK); note.setLatestEventInfo(this, "Sleep Time", "Time to get out of bed", intentToStart); note.number = ++count; note.defaults = Notification.DEFAULT_VIBRATE; NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.notify(NOTIFY_ID, note); }}Tuesday, April 26, 2011Need permission to Vibrate13<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="edu.sdsu.cs.whitney" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".ServiceExample" 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-sdk android:minSdkVersion="3" /><uses-permission android:name="android.permission.VIBRATE"></uses-permission></manifest>Tuesday, April 26, 2011Start an activity when clearing Notifications14 private void sendNotification() { Notification note = new Notification(R.drawable.icon, "Wake Up!", System.currentTimeMillis()); PendingIntent intentToStart = PendingIntent.getActivity(this, 0, new Intent(this, ServiceExample.class), Intent.FLAG_ACTIVITY_NEW_TASK); note.setLatestEventInfo(this, "Sleep Time", "Time to get out of bed", intentToStart); note.number = ++count; note.deleteIntent = intentToStart; note.defaults = Notification.DEFAULT_VIBRATE; NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.notify(NOTIFY_ID, note); }Tuesday, April 26, 201115Service Sending NotificationTuesday, April 26, 2011Service that Sends Notifications16Tuesday, April 26, 2011Start Service when Click button17public class ServiceExample extends Activity implements View.OnClickListener { public void onClick(View v) { Intent serviceIntent = new Intent(this, AvitarService.class); startService(serviceIntent); } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button ok = (Button) findViewById(R.id.start); ok.setOnClickListener(this); }}Tuesday, April 26, 2011AvitarService18public class AvitarService extends IntentService { private static final int NOTIFY_ID = 1123; public AvitarService() { super("AvitarServiceThreadName"); } @Override protected void onHandleIntent(Intent intent) { TimerTask task = new TimerTask() { @Override public void run() { sendNotification(); } }; new Timer().schedule(task, 1000); }Tuesday, April 26, 2011The Notification19 private void sendNotification() { Notification note = new Notification(R.drawable.icon, "Nap time!", System.currentTimeMillis()); PendingIntent intentToStart = PendingIntent.getActivity(this, 0, new Intent(this, ServiceExample.class), Intent.FLAG_ACTIVITY_NEW_TASK); note.setLatestEventInfo(this, "Nap Time", "Go to Bed", intentToStart); NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);


View Full Document

SDSU CS 696 - Android Services

Download Android Services
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Android Services and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Android Services 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?