1/23

Lecture Mobile Application Development (119310)

Android Framework Basics

2/23

Agenda

3/23

Service

4/23

Service Lifecycle

center

5/23

System Services

6/23

Custom Service

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

7/23

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/23

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/23

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/23

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/23

Android Assignment - Service

12/23

Broadcast Receiver

13/23

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
14/23

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();
 }
}

15/23

Broadcast Receiver

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

16/23

Local Broadcasts

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

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

17/23

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);
  }
};

18/23

Local Broadcasts

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

19/23

Local Broadcasts

20/23

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); 

21/23

Android Assignment - Broadcast Receiver

22/23

Summary

23/23

Recab Questions