1/22

Android Development

Android Framework Basics

2/22

Agenda

3/22

Service

4/22

Service Lifecycle

center

5/22

System Services

6/22

Custom Service

<service
  android:name="MyService"
  android:icon="@drawable/icon"
  android:label="@string/service_name">
</service> 
7/22

Custom Service

public class MyService extends Service {

  @Override
public int onStartCommand(Intent intent, int flags, int startId) {
    //TODO do something useful
    return Service.START_NOT_STICKY;
 }
  @Override
public IBinder onBind(Intent intent) {
  	//TODO for communication return IBinder implementation
    return null;
 }
} 
8/22

Service Restart Options

Option Description
Service.START_STICKY Service is restarted, if it gets terminated. Intent data passed to the onStartCommand method is null. Used for services which manages their own state and do not depend on the Intent data.Service is not restarted. Used for services which are periodically triggered anyway. The service is only restarted, if the runtime has pending startService() calls since the service termination.
Service.START_NOT_STICKY Service is not restarted. Used for services which are periodically triggered anyway. The service is only restarted, if the runtime has pending startService() calls since the service termination.
Service.START_REDELIVER_INTENT Similar to Service.START_STICKY but the original Intent is re-delivered to the onStartCommand method.
9/22

Starting a Service

// Create an explicit intent
Intent i= new Intent(context, MyService.class);

// if needed, add data to the intent
i.putExtra("Param1", "Data for the service");
context.startService(i); 
10/22

Custom Service

public class ExampleService extends IntentService {
	
	public ExampleService() {
		super("ExampleService");
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		int waitTime = (int) intent.getExtras().get("wait_time");
	// do work here, e.g. download file
		try {
			Thread.sleep(waitTime*1000);
			intent.setAction("MYSERVICE_FINISHED_ACTION");
			LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	...
11/22

Broadcast Receiver

12/22

Broadcast Receiver

Event Description
Intent.ACTION_BOOT_COMPLETED Boot completed. Requires the android.permission.RECEIVE_BOOT_COMPLETED permission
Intent.ACTION_POWER_CONNECTED Power got connected to the device
Intent.ACTION_POWER_DISCONNECTED Power got disconnected from the device
Intent.ACTION_BATTERY_LOW Battery gets low, typically used to reduce activities in your app which consume power
Intent.ACTION_BATTERY_OKAY Battery status good again
13/22

Broadcast Receiver

public class MyPhoneReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		Toast.makeText(context,"Incoming phone call", Toast.LENGTH_LONG).show();
	}
}
14/22

Broadcast Receiver

<receiver android:name="MyPhoneReceiver" >
 <intent-filter>
  <action android:name="android.intent.action.PHONE_STATE" >
  </action>
 </intent-filter>
</receiver>
15/22

Local Broadcasts

public void onResume() {
  super.onResume();

  // Register mMessageReceiver to receive messages.
  LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("my-event"));
}
16/22

Local Broadcasts

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    // Extract data included in the Intent
    String message = intent.getStringExtra("message");
    Log.d("receiver", "Got message: " + message);
  }
};
17/22

Local Broadcasts

protected void onPause() {
  // Unregister since the activity is not visible
  LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
  super.onPause();
} 
18/22

Local Broadcasts

19/22

Sending a Broadcast

Intent intent = new Intent();
intent.setAction("my-event");
sendBroadcast(intent); 
Intent intent = new Intent();
intent.setAction("my-event");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent); 
20/22

Android Assignment 3

What you will learn

+

21/22

Summary

22/22

Recab Questions