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

      Python create package

      Create a root folder Create a sub-folder "example_pkg" that contains the funtionallity packaging_tutorial/ example_pkg/ __init__.py In the root folder create the following structure  packaging_tutorial/ example_pkg/ __init__.py tests/ setup.py LICENSE README.md in the setup.py contains the configuration of the packages your package is found by find_packages() import setuptools with open ( "README.md" , "r" ) as fh : long_description = fh . read () setuptools . setup ( name = "example-pkg-YOUR-USERNAME-HERE" , # Replace with your own username version = "0.0.1" , author = "Example Author" , author_email = "author@example.com" , description = "A small example package" , long_description = long_description , long_description_content_type = "text/markdown" , url = "https://github.com/pypa/sam...

      Rails - Basic Steps III

      pValidations Validations are a type of ActiveRecord Validations are defined in our models Implement Validations Go to   root_app/app/models Open files  *.rb for each model Mandatory field validates_presence_of   :field Ex:   validates_presence_of    :title Classes The basic syntax is class MyClass        @global_variable                def my_method              @method_variable        end end Create an instance myInstance = MyClass.new Invoke a mehod mc.my_method class() method returns the type of the object In Ruby, last character of method define the behavior If ends with a question -> return a boolean value If ends with an exclamation -> change the state of the object Getter / Setter method def global_variable       return @global_variable end ...

      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   ...