Getting Help
In our last article we discussed the benefits of leveraging the services of other applications before attempting to duplicate existing functionality in our own applications.
Built-in applications such as Maps, Email, Phone (yes that is an application too), SMS, Media Player, Camera and others are all at our disposal and may be called upon to bring sought-after functionality to our applications.
We have looked at some of these previously — most recently the Photo Booth application which demonstrated taking a picture within an Android Application.
There are two excellent reasons to use existing services available to us in Android, or any mobile environment for that matter.
First, we cannot possibly be experts on every facet of mobile apps programming, nor do we have endless budgets and limitless resources — using other people’s code where proper just makes sense today.
Secondly, we need to offer consistency to the user. You know the lack of consistency when you see it — an application or piece of a user interface that just looks out of place. We need to avoid causing confusion for our users and as a side benefit we reduce the amount of training, support and documentation necessary on our part. The more seamless and intuitive an application appears with respect to the over all user experience on a given platform, the more your application seems “built-in”. In most cases, this is exactly what we want our user’s experience to be.
Market Opportunity
In addition to the built-in applications, we can leverage third party applications and libraries also. This is an opportunity for both the application developer looking to leverage existing functionality to augment their application as well as an opportunity for the developer looking to provide functionality to other developer’s applications.
This idea had a successful implementation in the early mobile days of Palm OS. One application can launch another application, passing with it various bits of information governing the requested operation.
Let’s look at an example. Let’s say you have written the latest super list management software. Your users are asking you for the ability to create a hard-copy of the list. You know, print it out. On paper.
You have three options:
- Ignore the requests and maybe they go away!
- Become an expert in all things printing including communications, print drivers, image processing, etc.
- Use a third party provider of printing functionality. This choice may be broken up further into a couple of different deployment models:
- SDK based where you compile in the functionality
- Separate application is installed which handles the printing requests
Assuming we don’t want our users to go away and we also don’t want to become experts in all things printing, we choose option 3, to leverage a third party developer’s toolset. We don’t want to force all of our users to “pay” for this functionality so we make it optional. The application adds a menu for printing and we only print if the third party application is available. The third party printing tool may be then licensed separately and only the users who care about it need to pay for it.
The question now is “how” to make this happen from a technical perspective?
Starting other applications
This article demonstrates launching other applications to perform some operation that our application does not directly implement. We look at how this is done with Android and then enumerate some of the more popular examples from the Android SDK.
Regardless of what you are trying to accomplish on the Android platform you run into the Intent class sooner than later.
An instance of the Intent class represents a statement of “need” or “interest” that an application is expressing.
For example, an application can broadcast an Intent which is asking for help — something like: “Please handle this piece of data for me”. Or, “I need contacts”, please present a list of contacts for me to choose from.
Looking back to our Photo Booth example, we initiate the camera capture process by creating an Intent as shown in the following code snippet.
Intent action = new Intent("android.media.action.IMAGE_CAPTURE");
This Intent indicates that we want to capture an image with the camera.
We have two options for submitting this request: startActivity or startActivityForResult.
We use startActivity when we don’t care about keeping control within our application. An example of this might be where we want to leave our application and proceed with another operation altogether such as reading a map or making a phone call.
startActivity(action);
The startActivityForResult method is used when we prefer to maintain control of the situation. This method asks another Activity to perform an operation but then return control back to our application, hence the “ForResult” in the name. When using this method we need to provide more code to make things work.
class someclass {
final int TAKE_PICTURE = 1;
private void somemethod() {
Intent action = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(action,TAKE_PICTURE);
}
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
if (requestCode == TAKE_PICTURE) {
if (resultCode == RESULT_OK) {
Bitmap b = (Bitmap) data.getExtras().get("data");
someImageView.setImageBitmap(app.b);
}
}
}
}
Walking through this code, we see that the “somemethod” method initializes an instance of an Intent. It then requests that some other Activity within the Android platform be started with a call to startActivityForResult, passing along the Intent instance with with an integer identifier. Android attempts to dispatch this event, which in this case is handled by the built-in camera application. When the picture taking process is completed, the camera application packages up the image into new Intent instance and responds back to the caller in an asynchronous fashion.
When control returns to the calling application, the method named onActivityResult is invoked along with three parameters: a requestCode, a resultCode and an Intent which contains any relevant data associated with the Activity.
Because a single application might make many such requests, and for different reasons, the requestCode parameter is used to help organize the results. Assuming the resultCode is “OK”, the application can now process the results, which in this case involves handling an image.
The image is extracted from the Intent and shown visually to the user by setting the bitmap image property in an ImageView.
Much of Android programming is “rinse and repeat” on this basic model of setting up data and passing it along to another “Activity” to carry out the operation. This Activity may be a built-in application, a third-party application, or even another Actvity within the same application. To call another Activity within the same application the Intent is constructed in a slightly different manner:
Intent action = new Intent(getContext(),OtherActivity.clsss);
startActivity(action);
//or
startActivityForResult(action,1);
Advertising
The Intent identifies a desired action. When an application implements an action that it is capable of processing it must advertise this capability within its AndroidManifest.xml file. This manifest file contains all manner of configuration and environment information about the application. The piece that we are interested in here is something known as the “intent-filter”.
<activity android:name=".SomeThirdPartyApp" android:label="@string/app_name">
<intent-filter>
<action android:name="com.somethirdparty.SOME_ACTION" />
</intent-filter>
</actvity>
In order to launch this application a calling application would use code along these lines.
Intent action = new Intent("com.msi.somethirdparty.SOME_ACTION");
startActivity(action);
That’s all there is to it. Optionally, the Intent instance may contain information to be acted upon as either data or a reference to data such as a file name or unique record id in a database, etc.
Some popular ACTIONs
The Android platform includes a number of commonly used operations that your application might want to leverage. We’ll conclude with a list of some of the more popular ACTIONs and their respective intent-filter names.
- ACTION_VIEW — android.intent.action.VIEW — used to view a piece of data such as a contact record
- ACTION_DELETE — android.intent.action.DELETE — delete a specific piece of data, which is passed along with the Intent
- ACTION_PICK — android.intent.action.PICK — pick from a list of available entries, based on the type of data requested such as phone number
- ACTION_DIAL — android.intent.action.DIAL — bring up the phone application, ready to dial a passed along number
For a complete list of available Intent actions for built-in applications, please see the Android Intent documentation.
In an upcoming article we will have a look at providing access from one application to another on the iPhone/iPad platform.
Comments on "Launching Android Apps"
cheap car insurance quotes Mesa AZ average car insurance rates in Wayne MI full coverage auto insurance Odessa TX auto insurance quotes Tupelo MS affordable car insurance Rockville MD affordable car insurance Hendersonville NC no down payment auto insurance in Cheyenne WY auto insurance quotes MD
look auto insurance Beaumont TX cheap auto insurance quotes Arvada CO car insurance Acworth GA low income car insurance dmv Bayonne NJ car insurance with no license in La Crosse WI cheapest auto insurance Hinesville GA car insurance rates Brick NJ non owners auto insurance quotes Parkville MD
auto insurance quotes Lithonia GA cheapest car insurance Bellingham WA auto owners insurance North Tonawanda NY cheapest auto insurance in High Point NC best auto insurance in Austin TX average car insurance rates in North Richland Hills TX cheap non owners insurance in Newark DE auto insurance quotes Beloit WI
list of auto insurances in Lady Lake FL low income auto insurance IN car insurance quote Powder Springs GA
http://carinsurancecnv.info/NC/Wake-Forest/non-owners-car-insurance-quotes/ http://autoinsurancequotesoirw.info/TX/Corpus-Christi/cheap-non-owners-insurance-in/ http://autoinsuranceimn.pw/NJ/Hoboken/cheap-non-owners-insurance/ http://autoinsurancequoteupp.top/CT/Hartford/low-income-car-insurance-dmv/ http://carinsuranceocv.info/DE/Wilmington/auto-insurance/ http://autoinsurancequoteupp.top/CA/Poway/auto-insurance-rates/
cheap full coverage auto insurance Manchester NH car insurance Grand Junction CO us agency car insurance Circleville OH non owners auto insurance quotes San Jose CA
people http://carscoverageonline.com insurance mexico http://autoinsurancequotesro.info check discussed http://autoinsuranceweb.top seek other higher http://autoinsurancequotesem.us engine overheats see link http://carinsurancemr.net age
http://carinsurancewnm.info/LA/Prairieville/affordable-car-insurance/ http://autoinsuranceocq.top/HI/Honolulu/cheap-non-owners-insurance-in/ http://carinsurancecne.info/FL/Windermere/best-car-insurance-in/ http://carinsurancequoteuku.top/IA/Iowa-City/average-car-insurance-rates-in/ http://autoinsurancequoteyom.pw/KS/Kansas-City/cheap-non-owners-insurance-in/ http://carinsurancequotesosx.info/GA/Newnan/no-down-payment-car-insurance-in/
http://autoinsuranceocq.top/WA/Spokane/look-auto-insurance/ http://autoinsurancequotesswr.top/IL/Effingham/auto-insurance-rates/ http://autoinsurancequotesswr.top/OR/Eugene/cheap-full-coverage-car-insurance/ http://autoinsuranceocq.top/CA/Whittier/direct-auto-insurance/ http://carinsurancewnm.info/ND/Fargo/car-insurance/ http://carinsurancedhk.top/NC/Salisbury/best-car-insurance-in/ http://autoinsuranceocq.top/SC/Summerville/low-income-car-insurance-dmv/
cheap auto insurance quotes Omaha NE full coverage car insurance Belleville MI http://www.carinsurancequotegeh.info/ car insurance in Topeka KS car insurance Prairie Home MO auto insurance quotes Sunnyvale CA
average car insurance rates in Terre Haute IN cheapest car insurance Lenexa KS free car insurance quotes Brunswick OH free auto insurance quotes Saint Paul MN
http://autoinsurancequotesael.pw/NJ/Sicklerville/cheap-full-coverage-auto-insurance/ http://autoinsuranceoie.info/CA/San-Diego/car-insurance-rates/ http://carinsurancewnm.info/WI/La-Crosse/cheap-non-owners-insurance/ http://carinsurancequotegeh.info/MI/Kalamazoo/payless-auto-insurance/ http://autoinsurancequoteupp.top/MI/Bloomfield-Hills/cheap-non-owners-insurance-in/ http://autoinsurancequotesswr.top/CA/Valencia/no-down-payment-car-insurance-in/ http://carinsurancequotesosx.info/GA/Savannah/auto-insurance-quotes/
http://autoinsuranceocq.top/CA/Chino/car-insurance-in/ http://carinsurancewnm.info/CA/El-Cajon/cheapest-auto-insurance-in/ http://carinsurancecne.info/MI/Southfield/look-auto-insurance/ http://carinsurancequotesiur.top/WI/cheapest-auto-insurance-in/ http://carinsurancecne.info/CA/Corona/non-owners-car-insurance-quotes/
no down payment car insurance in Spring Hill FL auto insurance Pinellas Park FL cheap full coverage auto insurance San Bernardino CA car insurance in Hesperia CA low income auto insurance dmv Jackson MI car insurance Beaverton OR low income auto insurance dmv Wilson NC cheap sr22 insurance Adrian MI