Application Components
- Activity
- Primary class for user interaction
- Provide the GUI
- Main 3 components
- Services - Support ground back operations
- Support interaction with remote processes
- BroadcastReceiver - Responds to an event on the device
- An example is the sms
- ContentProvider - Allow to share data among applications
- Uses database-style inferface
Building an App
- Define resources
- Package
- Manifest
- Install & Run
- Signing the app http://developer.android.com/tools/publishing/app-signing.html
Activity
- The activity order is established in the task backstack
- The activity lifecycle
- Resumed / Running
- Paused
- Stopped
- Methods for an activity
- onCreate
- onStart - when the app become visible
- onResume - when is able to interact with the user
- onPause
- onStop - when the app become invisible
- onDestroy - in not called when android kill the app by the lack of memory
- onRestart - when the user press back and show the previous activity
Invoke and activity
- startActivity
- startActivityForResult
- When this method is used, override the method onActivityResut()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK
&& requestCode == PICK_CONTACT_REQUEST) {
}
}
Tips
- To avoid Resume the activity when change from vertical to landscape position change the configuration in the AndroidManifest file.
In the AndroidManifest.xml inside the tags; <application> , <activity>. Add the line
android:configChanges="orientation|keyboardHidden|screenSize"Intents
- Data Structure that represents
- Operation to be performed (Select a contact, dial a phone number)
- Event that has ocurred
Intent Field
- Action
- Data
- Category
- Type
- Ex: IMAGE/*, IMAGE/PNG, IMAGE/JPG, TEXT/HTML
- Component
- Extra
- Flag
Actions
- ACTION_DIAL
- Ex: Intent intent = new Intent(Intent.ACTION_DIAL);
- ACTION_EDIT - Display data to edit
- ACTION_SYNC - Synchonize device data with server
- ACTION_MAIN - Starts an initial activity of app
Data
- To pass data to an Intent is through an URI
- Ex: Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:+55555"));
Category
- Additional information that can handle the intent
- CATEGORY_BROWSER
- CATEGORY_LAUNCHER - Initial activity of a task & is listed in the top-level app launcher
Type
- Define the type of the information
- Intent.setType(String type)
- Intent.setDataAndType(Uri data, String type)
Component
- Used when there is only one component that should receive the intent
- Intent intent = new Intent( Context packageContext, Class<?> cls)
- setComponent()
- setClassName()
- setClass()
Extra
- Add extra information in a Map form (Key-value pairs)
- Intent.EXTRA_EMAIL
- to set extra data depends of the intent
Flag
- Show how the intent has to handle
- FLAG_ACTIVITY_NO_HISTORY -this does not put the activity in the History Stack
- FLAG_DEBUG_LOG_RESOLUTION - this show more logging information when the intent is processed
Invoke another activity
- Directly
- Intent helloAndroidIntent = new Intent(Context packageContext, Class<?> cls)
- Indirectly
- In this case Android, only compare with the available applications that match in:
- Action
- Data (Type & URI)
- Category
- Can specified in the AndroidManifest.xml, including the data
- To receive implicit intents an activity should specify an IntenFilter tag
- <category android:name="android.intent.category.DEFAULT" />
- Also can specify a Priority
- Values can be > 1000 , < 1000
- Higher values. higher priorities
- http://developer.android.com/guide/components/intents-filters.html
<intent-filter>
.....
<action android:name="android.intent.action.DIAL" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.DEFAULT" />
.....
</intent-filter>
<data
android:mimeType="string"
android:scheme="string"
android:host="string"
android:port="string"
android:path="string"
android:pathPattern="string"
android:pathPrefix="string"
/>
</activity>
Invoke activity from another application
- Invoker application
- In the Activity call the other activity using startActivity() method
- startActivity(new Intent(ACTIVITY_OTHER_APP));
- ACTIVITY_OTHER_APP - is the activity defined in the AndroidManifest in the other app
- Calling application
- In the AndroidManifest define the <intent-filter> to link the activity to be called.
<activity>
<intent-filter>
<action android:name="ACTIVITY_OTHER_APP" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Comentarios
Publicar un comentario