Android - Intents and Filters hgfh gfh.pptx

FarhanGhafoor7 17 views 17 slides Jul 29, 2024
Slide 1
Slide 1 of 17
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17

About This Presentation

gfhgh


Slide Content

Android - Intents and Filters By Engr. Farhan Ghafoor

What is Android - Intents and Filters? “ Android Intent  is the  message  that is passed between components such as activities, content providers, broadcast receivers, services etc. “ An Android  Intent  is an abstract description of an operation to be performed. It can be used with  startActivity  to launch an Activity,  broadcastIntent  to send it to any interested BroadcastReceiver components, and  startService (Intent)  or  bindService (Intent, ServiceConnection , int )  to communicate with a background Service.

Android - Intents and Filters Android intents are mainly used to : Start the service Launch an activity Display a web page Display a list of contacts Broadcast a message Dial a phone call etc.

Types of Android Intents Implicit Intent Implicit Intent  doesn't specify the component. In such case, intent provides information of available components provided by the system that is to be invoked. For example, you may write the following code to view the webpage. Intent intent= new  Intent( Intent.ACTION_VIEW );   intent.setData ( Uri.parse ( "http://www.google.com.pk" ));   startActivity (intent);  

Types of Android Intents Explicit Intent Explicit Intent  specifies the component. In such case, intent provides the external class to be invoked. Intent  i  =  new  Intent( getApplicationContext (),  ActivityTwo. class );   startActivity ( i );  

Android Implicit Intent - Complete Example activity_main.xml <?xml   version = "1.0"   encoding = "utf-8" ?>    < android.support.constraint.ConstraintLayout   xmlns:android = "http://schemas.android.com/ apk /res/android"         xmlns:app = "http://schemas.android.com/ apk /res-auto"         xmlns:tools = "http://schemas.android.com/tools"         android:layout_width = " match_parent "         android:layout_height = " match_parent "         tools:context = " example.javatpoint.com.implicitintent.MainActivity " >            < EditText             android:id = "@+id/ editText "             android:layout_width = " wrap_content "             android:layout_height = " wrap_content "             android:layout_marginEnd = "8dp"             android:layout_marginStart = "8dp"             android:layout_marginTop = "60dp"             android:ems = "10"             app:layout_constraintEnd_toEndOf = "parent"             app:layout_constraintHorizontal_bias = "0.575"             app:layout_constraintStart_toStartOf = "parent"             app:layout_constraintTop_toTopOf = "parent"   />    <Button             android:id = "@+id/button"             android:layout_width = " wrap_content "             android:layout_height = " wrap_content "             android:layout_marginRight = "8dp"             android:layout_marginLeft = "156dp"             android:layout_marginTop = "172dp"             android:text = "Visit"             app:layout_constraintEnd_toEndOf = "parent"             app:layout_constraintHorizontal_bias = "0.0"             app:layout_constraintStart_toStartOf = "parent"             app:layout_constraintTop_toBottomOf = "@+id/ editText "   />    </ android.support.constraint.ConstraintLayout >   

Android Implicit Intent - Complete Example MainActivity.java package   example.javatpoint.com.implicitintent ;      import   android.content.Intent ;   import   android.net.Uri ;   import  android.support.v7.app.AppCompatActivity;   import   android.os.Bundle ;   import   android.view.View ;   import   android.widget.Button ;   import   android.widget.EditText ;      public   class   MainActivity   extends   AppCompatActivity  {          Button  button ;        EditText   editText ;     @Override         protected   void   onCreate (Bundle  savedInstanceState ) {            super .onCreate ( savedInstanceState );            setContentView ( R.layout.activity_main );              button =  findViewById ( R.id.button );            editText  =   findViewById ( R.id.editText );               button.setOnClickListener ( new   View.OnClickListener () {                @Override                 public   void   onClick (View view) {                   String  url = editText.getText (). toString ();                   Intent intent= new  Intent( Intent.ACTION_VIEW ,  Uri.parse ( url ));                    startActivity (intent);               }           });       }   }  

Android Implicit Intent - Complete Example What is Uri.parse () in Android? Uri stands for Uniform Resource Identifier. Uri is the sequence of the characters used to identify resources uniquely over the internet.

Android Explicit Intent – Complete Example (Android calling one activity from another activity example) activity_main.xml <?xml   version = "1.0"   encoding = "utf-8" ?>    < android.support.constraint.ConstraintLayout   xmlns:android = "http://schemas.android.com/ apk /res/android"         xmlns:app = "http://schemas.android.com/ apk /res-auto"         xmlns:tools = "http://schemas.android.com/tools"         android:layout_width = " match_parent "         android:layout_height = " match_parent "         tools:context = " example.javatpoint.com.explicitintent.FirstActivity " >            < TextView             android:layout_width = " wrap_content "             android:layout_height = " wrap_content "             android:layout_marginEnd = "8dp"             android:layout_marginStart = "8dp"             android:layout_marginTop = "8dp"             android:text = "First Activity"             app:layout_constraintBottom_toBottomOf = "parent"             app:layout_constraintEnd_toEndOf = "parent"       app:layout_constraintHorizontal_bias = "0.454"             app:layout_constraintLeft_toLeftOf = "parent"             app:layout_constraintRight_toRightOf = "parent"             app:layout_constraintStart_toStartOf = "parent"             app:layout_constraintTop_toTopOf = "parent"             app:layout_constraintVertical_bias = "0.06"   />    <Button             android:id = "@+id/button"             android:layout_width = " wrap_content "             android:layout_height = " wrap_content "             android:layout_marginEnd = "8dp"             android:layout_marginStart = "8dp"             android:layout_marginTop = "392dp"             android:onClick = " callSecondActivity "             android:text = "Call second activity"             app:layout_constraintEnd_toEndOf = "parent"             app:layout_constraintStart_toStartOf = "parent"             app:layout_constraintTop_toTopOf = "parent"   />       </ android.support.constraint.ConstraintLayout >   

Android Explicit Intent – Complete Example (Android calling one activity from another activity example) MainActivityOne.java import   android.content.Intent ;   import  android.support.v7.app.AppCompatActivity;   import   android.os.Bundle ;   import   android.view.View ;      public   class   FirstActivity   extends   AppCompatActivity  {          @Override         protected   void   onCreate (Bundle  savedInstanceState ) {            super .onCreate ( savedInstanceState );            setContentView ( R.layout.activity_first );       }        public   void   callSecondActivity (View view){           Intent  i  =  new  Intent( getApplicationContext (),  SecondActivity. class );            i.putExtra ( "Value1" ,  "Android By  Javatpoint " );            i.putExtra ( "Value2" ,  "Simple Tutorial" );            // Set the request code to any code you like, you can identify the            // callback via this code             startActivity ( i );       }     }  

Android Explicit Intent – Complete Example (Android calling one activity from another activity example) activitytwo_main.xml <?xml   version = "1.0"   encoding = "utf-8" ?>    < android.support.constraint.ConstraintLayout   xmlns:android = "http://schemas.android.com/ apk /res/android"         xmlns:app = "http://schemas.android.com/ apk /res-auto"         xmlns:tools = "http://schemas.android.com/tools"         android:layout_width = " match_parent "         android:layout_height = " match_parent "         tools:context = " example.javatpoint.com.explicitintent.SecondActivity " >            < TextView             android:layout_width = " wrap_content "             android:layout_height = " wrap_content "             android:layout_marginEnd = "8dp"             android:layout_marginStart = "8dp"             android:layout_marginTop = "8dp"             android:text = "Second Activity"             app:layout_constraintBottom_toBottomOf = "parent"             app:layout_constraintEnd_toEndOf = "parent"             app:layout_constraintHorizontal_bias = "0.454"              app:layout_constraintLeft_toLeftOf = "parent"             app:layout_constraintRight_toRightOf = "parent"             app:layout_constraintStart_toStartOf = "parent"             app:layout_constraintTop_toTopOf = "parent"             app:layout_constraintVertical_bias = "0.06"   />            <Button             android:id = "@+id/button"             android:layout_width = " wrap_content "             android:layout_height = " wrap_content "             android:layout_marginEnd = "8dp"             android:layout_marginStart = "8dp"             android:layout_marginTop = "392dp"             android:onClick = " callFirstActivity "             android:text = "Call first activity"             app:layout_constraintEnd_toEndOf = "parent"             app:layout_constraintStart_toStartOf = "parent"             app:layout_constraintTop_toTopOf = "parent"   />    </ android.support.constraint.ConstraintLayout >   

Android Explicit Intent – Complete Example (Android calling one activity from another activity example) MainActivityTwo.java import   android.content.Intent ;   import  android.support.v7.app.AppCompatActivity;   import   android.os.Bundle ;   import   android.view.View ;   import   android.widget.Toast ;      public   class   SecondActivity   extends   AppCompatActivity  {          @Override         protected   void   onCreate (Bundle  savedInstanceState ) {            super .onCreate ( savedInstanceState );            setContentView ( R.layout.activity_second );           Bundle extras =  getIntent (). getExtras ();           String value1 =  extras.getString ( "Value1" );           String value2 =  extras.getString ( "Value2" );            Toast.makeText ( getApplicationContext (), "Values are:\n First value: " +value1+                    "\n Second Value: " +value2,  Toast.LENGTH_LONG ).show();       }        public   void   callFirstActivity (View view){           Intent  i  =  new  Intent( getApplicationContext (),  FirstActivity. class );            startActivity ( i );       }  }  

Android Explicit Intent – Complete Example

Email Client - Example Let's assume that you have an Activity that needs to launch an email client and sends an email using your Android device. For this purpose, your Activity would send an ACTION_SEND along with appropriate  chooser , to the Android Intent Resolver. The specified chooser gives the proper interface for the user to pick how to send your email data. Intent email = new Intent ( Intent . ACTION_SEND , Uri . parse ( "mailto:" )); email . putExtra ( Intent . EXTRA_EMAIL , recipients ); email . putExtra ( Intent . EXTRA_SUBJECT , subject . getText (). toString ()); email . putExtra ( Intent . EXTRA_TEXT , body . getText (). toString ()); startActivity ( Intent . createChooser ( email , "Choose an email client from..." ));

Email Client - Example

Web Browser - Example A ssume that you have an Activity that needs to open URL in a web browser on your Android device. For this purpose, your Activity will send ACTION_WEB_SEARCH Intent to the Android Intent Resolver to open given URL in the web browser. The Intent Resolver parses through a list of Activities and chooses the one that would best match your Intent, in this case, the Web Browser Activity. The Intent Resolver then passes your web page to the web browser and starts the Web Browser Activity. String q = “KIET" ; Intent intent = new Intent ( Intent . ACTION_WEB_SEARCH ); intent . putExtra ( SearchManager . QUERY , q ); startActivity ( intent ); Above example will search as  KIET  on android search engine and it gives the result of KIET in your an activity

Q&A