Android Intent: Types of Android Intents
Android Intent: Types of Android Intents
Android Intent: Types of Android Intents
1) Implicit Intent
Implicit Intent doesn't specifiy 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.
1. Intent intent=new Intent(Intent.ACTION_VIEW);
2. intent.setData(Uri.parse("http://www.google.com"));
3. startActivity(intent);
2) Explicit Intent
Explicit Intent specifies the component. In such case, intent provides the
external class to be invoked.
1. Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
2. startActivity(i);
Android Implicit Intent Example
File: activity_main.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <android.support.constraint.ConstraintLayout xmlns:android="http://
schemas.android.com/apk/res/android"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. tools:context="example.javatpoint.com.implicitintent.MainActivity">
8.
9. <EditText
10. android:id="@+id/editText"
11. android:layout_width="wrap_content"
12. android:layout_height="wrap_content"
13. android:layout_marginEnd="8dp"
14. android:layout_marginStart="8dp"
15. android:layout_marginTop="60dp"
16. android:ems="10"
17. app:layout_constraintEnd_toEndOf="parent"
18. app:layout_constraintHorizontal_bias="0.575"
19. app:layout_constraintStart_toStartOf="parent"
20. app:layout_constraintTop_toTopOf="parent" />
21.
22. <Button
23. android:id="@+id/button"
24. android:layout_width="wrap_content"
25. android:layout_height="wrap_content"
26. android:layout_marginRight="8dp"
27. android:layout_marginLeft="156dp"
28. android:layout_marginTop="172dp"
29. android:text="Visit"
30. app:layout_constraintEnd_toEndOf="parent"
31. app:layout_constraintHorizontal_bias="0.0"
32. app:layout_constraintStart_toStartOf="parent"
33. app:layout_constraintTop_toBottomOf="@+id/editText" />
34. </android.support.constraint.ConstraintLayout>
File: MainActivity.java
1. package example.javatpoint.com.implicitintent;
2. import android.content.Intent;
3. import android.net.Uri;
4. import android.support.v7.app.AppCompatActivity;
5. import android.os.Bundle;
6. import android.view.View;
7. import android.widget.Button;
8. import android.widget.EditText;
9. public class MainActivity extends AppCompatActivity {
10. Button button;
11. EditText editText;
12. @Override
13. protected void onCreate(Bundle savedInstanceState) {
14. super.onCreate(savedInstanceState);
15. setContentView(R.layout.activity_main);
16.
17. button = findViewById(R.id.button);
18. editText = findViewById(R.id.editText);
19. button.setOnClickListener(new View.OnClickListener() {
20. @Override
21. public void onClick(View view) {
22. String url=editText.getText().toString();
23. Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url));
24. startActivity(intent);
25. }
26. });
27. }
28. }
Output