Implicit Intent

android_learners_hub



Implicit Intent:-
It is said that we use intent there where we don't know what the next activity will be or it specifies an action that can invoke any app on the device able to perform the action.
For example: when we want to share something like images or videos to other people then, we can use Implicit Intent. To share with other people, create an intent with the ACTION_SEND action and add extras(putExtra) that specify the content to share.
In this example we will cover ACTION_SEND action. We can also use URL and ACTION_VIEW. ACTION_VIEW is used when you have some data to display to the user for instance images, video , text and so on.

In activity_main.xml , create a button in the previous app:-
<Button   
 android:id="@+id/implicit"  
 android:layout_width="wrap_content"   
 android:layout_height="wrap_content" 
 android:layout_marginTop="20dp"    
 android:layout_gravity="center"    
 android:text="Implicit"    
 android:textSize="20sp" 
/>

      Add action listener on the Button in MainActivity.java.


Button implicit;
implicit=findViewById(R.id.implicit);
implicit
.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Create the text message with a string Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT,"android_learners_hub"); sendIntent.setType("text/plain"); // Verify that the intent will resolve to an activity if (sendIntent.resolveActivity(getPackageManager()) != null) { startActivity(sendIntent); } } });



Output:-






     For any query feel free to comment.   

Comments

Post a Comment