Communication Between Activities
Intents

• An intent is an abstract description of an operation to be performed.

• Late runtime binding between the code in different applications is
  performed by the use of Intents.
• Its most significant use is in the launching of activities.
Switching between Activities

• Any Android application will comprise of many activities.
• How do they Interact with one another.


Intent intent = new Intent(CallingActivity.this, NewActivity.class);
startActivity(intent);
Passing Data-Using Intent Extras

• Any Android application will comprise of many activities.
• Data needs to be send from one activity to another.

   Intent intent = new Intent(CallingActivity.this, NewActivity.class);
   Use intent.putExtra(“key", val);
   startActivity(intent);
Bundle

• Bundle is generally used for passing data between Activities. It can hold all
  types of values and can pass them to the new activity.
• You can receive data in another Activity like this:

Bundle extras = intent.getExtras();
String dataReceived = extras.getString(“key");
Shared Preferences

• Provides a way to capture user preferences on the settings of
  a specific application or an activity
• Preferences are typically name value pairs
Example - Shared Preferences
Starting Activity For Result
What is Activity Result?

• Most application consists of multiple activities.
• There comes a need for getting some result form the called
  activity
• Could be the list item selected on any activity etc.
Usage

• Call the StartactivityForResult() from the calling Activity with a
  unique request code.
• Create the overridden method OnActivityResult() in the calling
  Activity.
• Set the result to RESULT_OK with the selection stored in the
  intent.
Example 1:
Example 2:
Notifications in Android
NotificationManager

• To show notifications we use NotificationManager Class
   – NotificationManager notificationManager = (NotificationManager)
     getSystemService(NOTIFICATION_SERVICE);


• We use a PendingIntent to specify the action which should be
  performed once the user select the notification.

• You can Receive the activity via PendingIntent.getActivity()
Notification Extras

• Notification.Builder allows to define actions to the
  notification.

• A PendingIntent itself is simply a reference to a token
  maintained by the system.

• This means that, even if its owning application's process
  is killed, the PendingIntent itself will remain usable from
  other processes.
Notifications
Media in Android
Android Media Player

• MediaPlayer is the easier way, if you just want to play an
  audio file in the background, somewhere in an application
• MediaPlayer class can be used to control playback of
  audio/video files and streams
Usage:

• MediaPlayer mp;
• mp = MediaPlayer.create(this, R.raw.song);
• mp.start();
Video Player-Usage


•   VideoView myVideoView
    =(VideoView)findViewById(R.id.myvideoview);
•   myVideoView.setVideoURI(Uri.parse(SrcPath));
•   myVideoView.setMediaController(new
  MediaController(this));
•   myVideoView.requestFocus();
•   myVideoView.start();
Video Player
•Q & A..?
Android webinar class_3

Android webinar class_3

  • 1.
  • 2.
    Intents • An intentis an abstract description of an operation to be performed. • Late runtime binding between the code in different applications is performed by the use of Intents. • Its most significant use is in the launching of activities.
  • 3.
    Switching between Activities •Any Android application will comprise of many activities. • How do they Interact with one another. Intent intent = new Intent(CallingActivity.this, NewActivity.class); startActivity(intent);
  • 4.
    Passing Data-Using IntentExtras • Any Android application will comprise of many activities. • Data needs to be send from one activity to another. Intent intent = new Intent(CallingActivity.this, NewActivity.class); Use intent.putExtra(“key", val); startActivity(intent);
  • 5.
    Bundle • Bundle isgenerally used for passing data between Activities. It can hold all types of values and can pass them to the new activity. • You can receive data in another Activity like this: Bundle extras = intent.getExtras(); String dataReceived = extras.getString(“key");
  • 6.
    Shared Preferences • Providesa way to capture user preferences on the settings of a specific application or an activity • Preferences are typically name value pairs
  • 7.
    Example - SharedPreferences
  • 8.
  • 9.
    What is ActivityResult? • Most application consists of multiple activities. • There comes a need for getting some result form the called activity • Could be the list item selected on any activity etc.
  • 10.
    Usage • Call theStartactivityForResult() from the calling Activity with a unique request code. • Create the overridden method OnActivityResult() in the calling Activity. • Set the result to RESULT_OK with the selection stored in the intent.
  • 11.
  • 12.
  • 13.
  • 14.
    NotificationManager • To shownotifications we use NotificationManager Class – NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); • We use a PendingIntent to specify the action which should be performed once the user select the notification. • You can Receive the activity via PendingIntent.getActivity()
  • 15.
    Notification Extras • Notification.Builderallows to define actions to the notification. • A PendingIntent itself is simply a reference to a token maintained by the system. • This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes.
  • 16.
  • 17.
  • 18.
    Android Media Player •MediaPlayer is the easier way, if you just want to play an audio file in the background, somewhere in an application • MediaPlayer class can be used to control playback of audio/video files and streams
  • 19.
    Usage: • MediaPlayer mp; •mp = MediaPlayer.create(this, R.raw.song); • mp.start();
  • 20.
    Video Player-Usage • VideoView myVideoView =(VideoView)findViewById(R.id.myvideoview); • myVideoView.setVideoURI(Uri.parse(SrcPath)); • myVideoView.setMediaController(new MediaController(this)); • myVideoView.requestFocus(); • myVideoView.start();
  • 21.
  • 22.

Editor's Notes

  • #7 Java compilers convert your code from human readable Java source files to something called “bytecode” in the Java world. “Bytecode” is interpreted by a JVM, which operates much like a physical CPU might operate on machine code, to actually execute the compiled code. Performance - Java performance in generally second only to C/C++ in common language performance comparisons. In the Java programming language and environment, a just-in-time (JIT) compiler is a program that turns Java bytecode (a program that contains instructions that must be interpreted) into instructions that can be sent directly to the processor.The just-in-time compiler comes with the virtual machine and is used optionally. It compiles the bytecode into platform-specific executable code that is immediately executed
  • #10 such as development toolkits for other programming languages, and can write and contribute their own plug-in modules.
  • #16 A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it. If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call cancel() to remove it.