Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
fw:android1 [2014/02/03 22:44] alfred [Applications] |
fw:android1 [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 4: | Línea 4: | ||
| ==== Applications ==== | ==== Applications ==== | ||
| There are several classes inside the SDK which are useful to develop Android applications... | There are several classes inside the SDK which are useful to develop Android applications... | ||
| - | * Activity: Is the main class to develop user interaction. Usually they implement are focused on a single action. | + | * **Activity**: Is the main class to develop user interaction. Usually they implement are focused on a single action. |
| - | * Services: They run in the background while other applications are also running. They can perform long running actions or support interaction with other processes. | + | * **Services**: They run in the background while other applications are also running. They can perform long running actions or support interaction with other processes. |
| - | * Broadcast Receiver: Are subscribers\listeners to events (like the SMS reception...). | + | * **Broadcast Receiver**: Are subscribers\listeners to events (like the SMS reception...). |
| - | * Content Provider: Are sub-applications to exchange data between applications. They use a database style interface. | + | * **Content Provider**: Are sub-applications to exchange data between applications. They use a database style interface. |
| + | === Activities === | ||
| + | An application at least has an activity. Activity initialization is in ''onCreate()'' method, there you'll: | ||
| + | - Restore an application saved state. | ||
| + | - Set the content view. | ||
| + | - Initialize UI elements. | ||
| + | - Link UI elements to code actions. | ||
| + | First line of ''onCreate()'' method has to be ''super.onCreate''. \\ | ||
| + | To access another activity we'll use ''findViewById'' method. \\ | ||
| + | To start an activity programatically... | ||
| + | - You'll need to create an Intent object that specifies which Activity is gonna start. | ||
| + | - You'll pass this Intent to the methods ''startActivity'' or ''startActivityForResult''. | ||
| + | === Create Activity === | ||
| + | <code java> | ||
| + | Button launchActivityTwoButton = (Button) findViewById(R.id.bLaunchActivityTwo); | ||
| + | launchActivityTwoButton.setOnClickListener(new OnClickListener() { | ||
| + | @Override | ||
| + | public void onClick(View v) { | ||
| + | Intent activity2 = new Intent(ActivityOne.this, ActivityTwo.class); | ||
| + | startActivity(activity2); | ||
| + | } | ||
| + | }); | ||
| + | </code> | ||
| + | === Finish Activity === | ||
| + | <code java> | ||
| + | Button closeButton = (Button) findViewById(R.id.bClose); | ||
| + | closeButton.setOnClickListener(new OnClickListener() { | ||
| + | @Override | ||
| + | public void onClick(View v) { | ||
| + | ActivityTwo.this.finish(); | ||
| + | } | ||
| + | }); | ||
| + | </code> | ||
| + | === Recover the activity status === | ||
| + | When an Activity is finished it can be restored using a Bundle object, it stores key-value pairs. To save additional data about the activity state, you must override the onSaveInstanceState() callback method. Then the system will pass the same Bundle object to both the onRestoreInstanceState() and onCreate() methods. | ||
| + | <code java> | ||
| + | static final String STATE_SCORE = "playerScore"; | ||
| + | static final String STATE_LEVEL = "playerLevel"; | ||
| + | ... | ||
| + | @Override | ||
| + | public void onSaveInstanceState(Bundle savedInstanceState) { | ||
| + | savedInstanceState.putInt(STATE_SCORE, mCurrentScore); | ||
| + | savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel); | ||
| + | super.onSaveInstanceState(savedInstanceState); | ||
| + | } | ||
| + | </code> | ||
| + | <code java> | ||
| + | protected void onCreate(Bundle savedInstanceState) { | ||
| + | super.onCreate(savedInstanceState); // Always call the superclass first | ||
| + | if (savedInstanceState != null) { | ||
| + | mCurrentScore = savedInstanceState.getInt(STATE_SCORE); | ||
| + | mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL); | ||
| + | } else { | ||
| + | // Probably initialize members with default values for a new instance | ||
| + | } | ||
| + | </code> | ||
| + | You may choose to implement onRestoreInstanceState(), which the system calls after the onStart() method. The system calls onRestoreInstanceState() only if there is a saved state to restore, so you do not need to check whether the Bundle is null: | ||
| + | <code java> | ||
| + | public void onRestoreInstanceState(Bundle savedInstanceState) { | ||
| + | // Always call the superclass so it can restore the view hierarchy | ||
| + | super.onRestoreInstanceState(savedInstanceState); | ||
| + | |||
| + | // Restore state members from saved instance | ||
| + | mCurrentScore = savedInstanceState.getInt(STATE_SCORE); | ||
| + | mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL); | ||
| + | } | ||
| + | </code> | ||
| + | ==== Activity lifecicle ==== | ||
| + | {{ :fw:android:az0jw.png?300 |}} | ||
| + | The activity will be activated between the ''onStart'' and the ''onStop'' steps. \\ | ||
| + | The activity will be visible in the foreground between the ''onResume'' and the ''onPause'' steps. \\ | ||
| + | ''onRestart'' is called when the activity has been stoped and is about to be called again. \\ | ||
| + | ''onResume'' happens when the activity is about to start interacting with the user. | ||
| + | === Tasks === | ||
| + | A task is a set of related Activities, they don't have to be part of the same application. A task could be the contacts screen, which can be called from single activities. | ||
| + | === Configuration changes === | ||
| + | Detection of configuration changes like change the mobile orientation from portrait to landscape. | ||
| + | ==== Resources ==== | ||
| + | === R class === | ||
| + | ''R'' class is an authomaticly generated class to access resources from code. | ||
| + | Accessing resources from code: | ||
| + | * To access layouts: ''R.layout.layout_name'' | ||
| + | |||
| + | === Strings === | ||
| + | Strings are stored in ''res/values/*.xml'' file, can store visual styles as well as text, and there are three types: | ||
| + | - Current strings | ||
| + | - String array | ||
| + | - Plurals: Plural values for single strings. | ||
| + | A string definition is: | ||
| + | <code xml> | ||
| + | <string name="hello">Hello world!</string> | ||
| + | </code> | ||
| + | To access them from other resources we will do: | ||
| + | <code> | ||
| + | @string/string_name | ||
| + | </code> | ||
| + | === Layout files === | ||
| + | The UI for applications in xml format. They are stored in ''res/layout/*.xml'' files. | ||
| + | To access them from other resources we will do: | ||
| + | <code> | ||
| + | @layout/layout_name | ||
| + | </code> | ||
| + | |||
| + | ==== Controls ==== | ||
| + | You can have a TextView in your layout and want to access it by code... | ||
| + | <code java> | ||
| + | protected void onCreate(Bundle savedInstanceState) { | ||
| + | super.onCreate(savedInstanceState); | ||
| + | setContentView(R.layout.activity_one); | ||
| + | mTvCreate = (TextView)findViewById(R.id.create); | ||
| + | ... | ||
| + | </code> | ||
| + | ==== AndroidManifest.xml ==== | ||
| + | It's a file that defines the developed application. It includes... | ||
| + | * Application name | ||
| + | * Which components are included in this application. | ||
| + | * Permissons. | ||
| + | * Minimum API required. | ||
| ===== Tools ===== | ===== Tools ===== | ||
| ==== LogCat ==== | ==== LogCat ==== | ||