Presentation is loading. Please wait.

Presentation is loading. Please wait.

Technische Universität München Services, IPC and RPC Gökhan Yilmaz, Benedikt Brück.

Similar presentations


Presentation on theme: "Technische Universität München Services, IPC and RPC Gökhan Yilmaz, Benedikt Brück."— Presentation transcript:

1 Technische Universität München Services, IPC and RPC Gökhan Yilmaz, Benedikt Brück

2 Technische Universität München Services Is an application component Can perform long-running operations in the background Does not provide a user interface Can be started by another application component (e.g. Activity or Service) Possibility of binding to a service an perform IPC Do not necessarily run in own thread or process But runs even in background 26.10.11PR App - Gökhan Yilmaz, Benedikt Brück2

3 Technische Universität München Services Has to define an own Thread, when tasks takes longer than five seconds Simple way: Use “IntentService” Create subclass of “IntentService” override onHandleIntent(android.content.Intent), put workload there Superclass creates a separate thread and performs lifecycle operations More complicated way: “use Service” itself Starting component has to call startService in both ways 26.10.11PR App - Gökhan Yilmaz, Benedikt Brück3

4 Technische Universität München Services Create subclass of “Service” Override four main functions: onCreate() onStartCommand(android.content.Intent, int, int) onBind(android.content.Intent) onDestroy() Declare in manifest More attributes like permission can be found in android developers guide 26.10.11PR App - Gökhan Yilmaz, Benedikt Brück4

5 Technische Universität München Services onCreate() Is called when service is first created Should contain one-time setup procedures onStartCommand(android.content.Intent, int, int) Is called, when another component (e.g. Activity) calls startService(android.content.Intent) If service is not bound, stopSelf() or stopService(…) has to be called Intent is used for IPC, for flags see Android Developer Guide) Returns constant (e.g START_STICKY, see Android Developer Guide) onBind(android.content.Intent) Is called, when another component calls bindService(..) (see RPC) onDestroy() Is called on destruction Should contain clean-up 26.10.11PR App - Gökhan Yilmaz, Benedikt Brück5

6 Technische Universität München IPC Means Inter-Process-Communication Way for exchanging data among multiple processes Processes can be Services, Activities, … Two Types of IPC exist in Android Intent based IPC RPC 26.10.11PR App - Gökhan Yilmaz, Benedikt Brück6

7 Technische Universität München Intent Based IPC Data can be passed in both directions using Intent objects Enables a convenient, high-level system of inter-process communication. Intent objects are passed from process to process, using methods such as startActivity and startActivityForResult To receive data the Activity should have implemented onActivityResult to receive the result. 26.10.11PR App - Gökhan Yilmaz, Benedikt Brück7

8 Technische Universität München Intent Based IPC PutExtra and getExtra methods are defined to provide general purpose IPC in Intent Intent data can only be sent at the start of a component (startActivity, startService) or be received at the end (onActivityResult) During process execution broadcasts must be used 26.10.11PR App - Gökhan Yilmaz, Benedikt Brück8

9 Technische Universität München Example of Intent Based IPC (send data to another activity via Intent) void returnResult(String greeting) { // Create the Intent object Intent i = new Intent(); // Put an extra named "result" in the intent i.putextra("result", greeting); // Make this Intent the result for this activity setResult(RESULT_OK, i); // End this activity finish(); } 26.10.11PR App - Gökhan Yilmaz, Benedikt Brück9

10 Technische Universität München Examples of Intent Based IPC to receive the data protected void onActivityResult(int requestCode, int resultCode, Intent result) { if (resultCode == RESULT_OK) { String greeting = result.getStringExtra("result"); someTextview.setText(greeting); } 26.10.11PR App - Gökhan Yilmaz, Benedikt Brück10


Download ppt "Technische Universität München Services, IPC and RPC Gökhan Yilmaz, Benedikt Brück."

Similar presentations


Ads by Google