Intent Management & Resolution;    Processing Results Returned by     Started Android Components                      Vlad...
Outline    ●        Review of Implicit & Explicit Intents    ●        Intent Management & Resolution    ●        Processin...
Review of Explicit & Implicit Intentshttp://www.youtube.com/vkedco   www.vkedco.blogspot.com
Review: Explicit and Implicit Intents    ●        Android supports two types of intents: explicit or        implicit    ● ...
Review: Explicit Intent Example    ●        Intent i = new Intent(this, ImageDisplayAct01.class);    ●        If ImageDisp...
Review: Implicit Intents    ●        Implicit intents must specify the type of action they        should perform    ●     ...
Review: Implicit Intent Example        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    ●        If there is a c...
Intent Management & Resolutionhttp://www.youtube.com/vkedco   www.vkedco.blogspot.com
Component Launch via Intents    ●        Third-party components are launched via        Intents    ●        There is no gu...
Two Intent Management Classes    ●        PackageManager – this class is used to        retrieve info about various compon...
Intent Resolution with PackageManager         // 1. Create an intent         Intent intent = new Intent(Intent.ACTION_VIEW...
Processing Results Returned by               Started Componentshttp://www.youtube.com/vkedco   www.vkedco.blogspot.com
Returning Results    ●        Components launched via startActivity() by        other components (aka parent components)  ...
Implementation Steps    ●        All launchable components must be registered on        the ecosystem via intent filters  ...
startActivityForResult()    ●        This method takes two arguments: an Intent        object and a request code    ●     ...
onActivityResult()    ●        A child component, when launched, can get additional        information about the parents r...
Example    ●        Write an application that has one parent activity        and two child activities    ●        The 1st ...
Snapshots                           Clicks on explicit buttons                           launch appropriate               ...
Code Organization    ●        Parent activity - StartActivityForResultAct    ●        The 1st child activity - FibonacciNu...
What Does Parent Do?    ●        Defines request codes for children    ●        Defines request actions that the children ...
Define Request Codes      // 1. Define Request Codes      final static int FIBO_REQUEST_CODE = 1;      final static int PR...
Explicit Intent Construction      // 1. Define explicit intent      nth = Integer.parseInt(mEdTxtFibocn.getText().toString...
Implicit Intent Construction      // 1. Define implicit intent      nth = Integer.parseInt(mEdTxtFibocn.getText().toString...
Process Returned Results      public void onActivityResult(int requestCode, int resultCode, Intent returnedData) {        ...
What Does Child Do?    ●        Get parents intent    ●        Get requested parameters from the intent    ●        If par...
What Does Child Do?      // 1. Get parents intent      Intent requestIntent = this.getIntent();      Resources res = this....
Upcoming SlideShare
Loading in …5
×

Android Intent Management & Resolution; Processing Results Returned by Started Components

1,202 views
1,142 views

Published on

Android Intent Management & Resolution; Processing Results Returned by Started Components

Published in: Software
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,202
On SlideShare
0
From Embeds
0
Number of Embeds
343
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Android Intent Management & Resolution; Processing Results Returned by Started Components

  1. 1. Intent Management & Resolution; Processing Results Returned by Started Android Components Vladimir Kulyukinhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  2. 2. Outline ● Review of Implicit & Explicit Intents ● Intent Management & Resolution ● Processing Results Returned by Started Componentshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  3. 3. Review of Explicit & Implicit Intentshttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  4. 4. Review: Explicit and Implicit Intents ● Android supports two types of intents: explicit or implicit ● Explicit intents must know the Java class name of the component that they want to start ● Implicit intents must know the action type they want to be done for themhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  5. 5. Review: Explicit Intent Example ● Intent i = new Intent(this, ImageDisplayAct01.class); ● If ImageDisplayAct01.class is registered with Android and resolved, the corresponding component is launched ● If not, there is a run-time errorhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  6. 6. Review: Implicit Intents ● Implicit intents must specify the type of action they should perform ● If there is a component in the Android ecosystem that is registered to perform the specified action type, it will be launched ● If there are several components in the Android ecosystem that are registered, Android will ask the user to select one via a selection dialog ● If no component is found, nothing happenshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  7. 7. Review: Implicit Intent Example Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); ● If there is a component that can perform MediaStore.ACTION_IMAGE_CAPTURE, it will be launched ● Multiple components may be registered to perform the same actionhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  8. 8. Intent Management & Resolutionhttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  9. 9. Component Launch via Intents ● Third-party components are launched via Intents ● There is no guarantee that a component that handles a specific action is installed in the specific Android ecosystem ● Is there a way to know ahead of time if an intent will be successfully resolved?http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  10. 10. Two Intent Management Classes ● PackageManager – this class is used to retrieve info about various components installed in the ecosystem ● ComponentName – this class is used for identifying application components (Activity, Service, BroadcastReceiver, ContentProvider) available in the home ecosystemhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  11. 11. Intent Resolution with PackageManager // 1. Create an intent Intent intent = new Intent(Intent.ACTION_VIEW); // 2. Get a PackageManager object PackageManager packMan = getPackageManager(); // 3. Use the PackageManager object to resolve a component name ComponentName compName = intent.resolveActivity(packMan); if ( compName == null ) { // 3a) handle absence of the component } else { // 3b) start activity startActivity(intent); }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  12. 12. Processing Results Returned by Started Componentshttp://www.youtube.com/vkedco www.vkedco.blogspot.com
  13. 13. Returning Results ● Components launched via startActivity() by other components (aka parent components) do not return any results to their parents ● But many control flow scenarios require that launched components return results to their parents ● Parents can then use those results to continue their computationhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  14. 14. Implementation Steps ● All launchable components must be registered on the ecosystem via intent filters ● Parents must implement two methods: – startActivityForResult() – onActivityResult() ● The method startActivityForResult() is used to launch components; onActivityResult() is called (by Android) when launched components return resultshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  15. 15. startActivityForResult() ● This method takes two arguments: an Intent object and a request code ● Intent objects can be explicit or implicit ● Request codes are integer constants that uniquely identify child components that return results, because the same parent may launch multiple child components ● Parents can pass additional information to their children via intentshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  16. 16. onActivityResult() ● A child component, when launched, can get additional information about the parents request from the parents intent ● When the child is done with its computation, it can call setResult(result_code, data) and then finish() ● The first argument result_code is generally Activity.RESULT_OK or Activity.RESULT_CANCELED ● The second argument to setResult() is an Intent object where the child can pass computational results to its parenthttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  17. 17. Example ● Write an application that has one parent activity and two child activities ● The 1st child activity computes Fibonacci numbers ● The 2nd child activity computes prime numbers ● The parent activity uses explicit/implicit intents st to launch its children to compute the 1 20 Fibonacci or 1st 20 prime numbershttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  18. 18. Snapshots Clicks on explicit buttons launch appropriate activities via explicit intents and toasts returned results Clicks on implicit buttons do the same via implicit intents source is herehttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  19. 19. Code Organization ● Parent activity - StartActivityForResultAct ● The 1st child activity - FibonacciNumbersAct ● The 2nd child activity - PrimesNumbersActhttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  20. 20. What Does Parent Do? ● Defines request codes for children ● Defines request actions that the children must specify in their filters ● Construct explicit/implicit Intent objects and place requested information into them ● Launch appropriate children via Intent objects with startActivityForResult() ● Implement onActivityResult() to process resultshttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  21. 21. Define Request Codes // 1. Define Request Codes final static int FIBO_REQUEST_CODE = 1; final static int PRIME_REQUEST_CODE = 2; // 2. Define action constants final static String COMPUTE_FIBO_ACTION = "org.vkedco.android.mobicom.fibonacci"; final static String COMPUTE_PRIME_ACTION = "org.vkedco.android.mobicom.primes";http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  22. 22. Explicit Intent Construction // 1. Define explicit intent nth = Integer.parseInt(mEdTxtFibocn.getText().toString()); Intent explFiboIntent = new Intent(this, FibonacciNumbersAct.class); // 2. Place data into explicit intent explFiboIntent.putExtra(mRes.getString(R.string.fibo_request_key), nth); // 3. Launch component startActivityForResult(explFiboIntent, StartActivityForResultMainAct.FIBO_REQUEST_CODE);http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  23. 23. Implicit Intent Construction // 1. Define implicit intent nth = Integer.parseInt(mEdTxtFibocn.getText().toString()); Intent implFiboIntent = new Intent(StartActivityForResultMainAct.COMPUTE_FIBO_ACTION); // 2. Place data into explicit intent implFiboIntent.putExtra(mRes.getString(R.string.fibo_request_key), nth); // 3. Launch component startActivityForResult(implFiboIntent, StartActivityForResultMainAct.FIBO_REQUEST_CODE);http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  24. 24. Process Returned Results public void onActivityResult(int requestCode, int resultCode, Intent returnedData) { super.onActivityResult(requestCode, resultCode, returnedData); switch ( requestCode ) { case StartActivityForResultMainAct.FIBO_REQUEST_CODE: if ( resultCode == Activity.RESULT_OK ) { if ( returnedData.hasExtra(mRes.getString(R.string.fibo_result_key)) ) { Toast.makeText(this, "Fibo = " + returnedData.getIntExtra(mRes.getString(R.string.fibo_result_key), -1), Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "Fibo Computation Failure", Toast.LENGTH_LONG).show(); }} return; case StartActivityForResultMainAct.PRIME_REQUEST_CODE: return; } }http://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  25. 25. What Does Child Do? ● Get parents intent ● Get requested parameters from the intent ● If parameters are not retrieved, then return RESULT_CANCELED ● If parameters are retrieved, do the necessary computation ● Place them into a returned intent object ● Set the returned result to RESULT_OK and return the intent objecthttp://www.youtube.com/vkedco http://www.vkedco.blogspot.com
  26. 26. What Does Child Do? // 1. Get parents intent Intent requestIntent = this.getIntent(); Resources res = this.getResources(); // 2. Get intents parameters if ( requestIntent.hasExtra(res.getString(R.string.fibo_request_key))) { int rslt = fibo(requestIntent.getIntExtra(res.getString(R.string.fibo_request_key), -1)); // 3. If no parameters are passed, return RESULT_CANCELED if ( rslt == -1 ) { this.setResult(RESULT_CANCELED); this.finish(); } else { // 4. do requested computation on the retrieved parameters, place results into an intent // and return RESULT_OK Intent resultIntent = new Intent(); resultIntent.putExtra(res.getString(R.string.fibo_result_key), rslt); this.setResult(RESULT_OK, resultIntent); this.finish(); }}http://www.youtube.com/vkedco http://www.vkedco.blogspot.com

×