Ir al contenido principal

Android - Basic Steps (Intent)

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


  • 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
<activity>
  <intent-filter>
       .....
       <action android:name="android.intent.action.DIAL" />
       <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

      Entradas populares de este blog

      Android - Basic Steps (Service)

      Service Run in the main thread of the hosting application Android can kill the Service if need the resources Purpose Supporting inter-application method execution Performing background processing Start a Service Call Context.startService(Intent intent)\ To call from a Fragment use getActivity().getApplicationContext().startService( intentService); Executing the service After call startService(...)  In the Service is executed the method onStartCommand(...) If the method returns the constant START_NOT_STICKY then Android will not restart the service automatically if the the process is killedp Foreground To execute the service foreground call the method startForeground() Use this if the user is aware of the process Bind to a Service Call the method Context.bindService( Intent service ServiceConnection con int flags ) Send Toast from the Service On the method onStartCommand receive the message   ...

      Android - Basic Steps (Location & Maps)

      Location Is composed by Latitude Longitude Time-stamp Accuracy Altitude Speed Bearing LocationProvider Types: Network  Wifi access points Cell phone towers GPS Passive Piggyback on the readings requested by other application Permissions Network  android.permission.ACCESS_COARSE_LOCATION android.permission.ACCESS_FINE_LOCATION GPS android.permission.ACCESS_FINE_LOCATION Passive Provider android.permission.ACCESS_FINE_LOCATION LocationManager System service for accessing location data getSystemService( Context.LOCATION_SERVICE ) Functions Determine the last known user location Register for location update Register to receive intents when the device nears or move away from a given geographic area LocationListener Defines callbacks methods that are called when Location or LocationProvider status change. Methods onLocationChanged(...) onProviderDisabled(...) onProviderEnabled(...) onStatusChan...

      IIS - Permisions

      IIS Permissions To enable the Active Directory connection in the IIS, follow the next steps: Go to IIS Go to Application Pool Select your App Pool Select Advanced Settings in the right side In the section Process Model Select in Identity value the property " NetworkService " You don´t need to restart your application