Differences

This shows you the differences between two versions of the page.

Link to this comparison view

dapm:laboratoare:03 [2017/02/20 12:33]
127.0.0.1 external edit
dapm:laboratoare:03 [2021/04/04 19:47] (current)
ioana_maria.culic
Line 1: Line 1:
-===== Laboratorul 03. =====+ ===== Lab 05 - Intents ​===== 
 +**Intents** \\
  
 +An Intent is a messaging object you can use to request an action from another app component. Although intents facilitate communication between components in several ways, there are three fundamental use cases:\\
 +- Starting an activity\\
 +- Starting a service\\
 +- Delivering a broadcast\\
 +\\
 +An Activity represents a single screen in an app. You can start a new instance of an Activity by passing an Intent to startActivity(). The Intent describes the activity to start and carries any necessary data.\\
 +\\
 +{{:​dapm:​laboratoare:​lab3_intent.png?​500|}}\\
 + How an implicit intent is delivered through the system to start another activity:\\
 +[1] Activity A creates an Intent with an action description and passes it to startActivity().[2] The Android System searches all apps for an intent filter that matches the intent. When a match is found, [3] the system starts the matching activity (Activity B) by invoking its onCreate() method and passing it the Intent. \\
 +\\
 +Example to start an activity\\
 +   ​Intent intent= new Intent(this,​ SecondClass.class);​
 +   ​startActivity(intent);​
 +\\
 +**Extras**\\
 +Key-value pairs that carry additional information required to accomplish the requested action. Just as some actions use particular kinds of data URIs, some actions also use particular extras.\\
 +\\
 +Example: send a string to the secondActivity\\
 +   //In the firstActivity
 +   ​Intent i = new Intent(FirstActivity.this,​ SecondActivity.class); ​  
 +   ​String strName = "Lab3 is great!";​
 +   ​i.putExtra("​STRING_I_NEED",​ strName);
  
 +   //In the secondActivity
 +   ​public void onCreate(Bundle savedInstanceState) {
 +      super.onCreate(savedInstanceState);​
 +      setContentView(R.layout.secondactivity);  ​
 +          ​
 +      Intent intent = getIntent();​
 +      Bundle extras = intent.getExtras(); ​      
 +      if(extras != null) {
 +          String getString= extras.getString("​STRING_I_NEED"​); ​          
 +      }
 +  }
 +
 +   
 +You can add extra data with various putExtra() methods, each accepting two parameters: the key name and the value. You can also create a Bundle object with all the extra data, then insert the Bundle in the Intent with putExtras().
 +\\
 +\\
 +When you use an implicit intent, the Android system finds the appropriate component to start by comparing the contents of the intent to the intent filters declared in the **manifest file** of other apps on the device. If the intent matches an intent filter, the system starts that component and delivers it the Intent object.
 +
 +__Activity example from a manifest file__\\
 +  <​activity android:​name="​MainActivity">​
 +      <!-- This activity is the main entry, should appear in app launcher -->
 +      <​intent-filter>​
 +          <action android:​name="​android.intent.action.MAIN"​ />
 +          <​category android:​name="​android.intent.category.LAUNCHER"​ />
 +      </​intent-filter>​
 +  </​activity>​
 +  ​
 +  <​activity android:​name="​ShareActivity">​
 +      <!-- For example, here's an activity declaration with an intent filter to receive an ACTION_SEND intent when the data type is text: -->
 +      <​intent-filter>​
 +          <action android:​name="​android.intent.action.SEND"/>​
 +          <​category android:​name="​android.intent.category.DEFAULT"/>​
 +          <data android:​mimeType="​text/​plain"/>​
 +      </​intent-filter>​
 +  </​activity>​
 +  ​
 +====== Exercises : ======
 + 
 +**Ex 1 ** 
 +Make a new project with 2 activities (MainActivity and FindTheNumberActivity.
 +Add the seccondActivity (FindTheNumberActivity) in manifest file\\
 +\\
 +**Ex 2 **
 +MainActivity will be a login Page like in the picture.\\
 +Add an action to the button: when clicked, it will check the username and the password field, and if it's __correct__,​ it will redirect you to the secondActivity (FindTheNumberActivity). \\
 +Make the username and the password be the string __student__.\\
 +**Attention !** \\
 +You get the text from an editText with "​getText()"​ method, and you convert it to string with "​toString()"​ method.\\
 +The password and the username field are editText. And the text from the password field will have to be hidden (add android:​inputType="​textPassword"​)\\
 +{{:​dapm:​laboratoare:​lab3_login.png?​255|}}
 +
 +**Ex 3 **
 +Make the mini-game like in the picture. If the number is lower, show a toast with the message "​Lower",​ and if it is higher with the message "​Higher"​. If the number is correct, reset the number and show another toast with "You won! Try again" message.\\
 +{{:​dapm:​laboratoare:​lab3_findthenumber.png?​250|}}
 +\\
 +**Ex 4 **
 +In the FindTheNumberActivity,​ when the button is cliked, change it's color (red<​->​blue)
dapm/laboratoare/03.1487586826.txt.gz · Last modified: 2018/03/09 14:44 (external edit)
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0