Skip to content

Instantly share code, notes, and snippets.

@MohammadSamandari
Last active March 30, 2020 00:16
Show Gist options
  • Save MohammadSamandari/4caced28575e1318579d556c5545def8 to your computer and use it in GitHub Desktop.
Save MohammadSamandari/4caced28575e1318579d556c5545def8 to your computer and use it in GitHub Desktop.
Implicit Intent / Sending Implicit Intent / Receiveing Implicit Intent

Implicit Intent

  • An implicit Intent allows you to activate an Activity if you know the action, but not the specific app or Activity that will handle that action.
  • An Activity that can receive an implicit Intent must define Intent filters in the AndroidManifest.xml file that match one or more Intent actions and categories.
  • The Android system matches the content of an implicit Intent and the Intent filters of any available Activity to determine which Activity to activate. If there is more than one available Activity, the system provides a chooser so the user can pick one.
  • The ShareCompat.IntentBuilder class makes it easy to build an implicit Intent for sharing data to social media or email.

Task 2: Implement the Open Website button

Go to main activity and see the openWebsite function. Note:This Intent constructor is different from the one you used to create an explicit Intent. In the previous constructor, you specified the current context and a specific component (Activity class) to send the Intent. In this constructor you specify an action and the data for that action. Actions are defined by the Intent class and can include

  • ACTION_VIEW (to view the given data),
  • ACTION_EDIT (to edit the given data),
  • ACTION_DIAL (to dial a phone number). In this case the action is ACTION_VIEW because you want to display the web page specified by the URI in the webpage variable.

Note: we use the intent.resolveActivity() method to check if there is an app to get the intent or not. This request that matches your Intent action and data with the Intent filters for installed apps on the device. You use it to make sure there is at least one Activity that can handle your requests.

Task 3: Implement the Open Location button

Go to main activity and see the openLocation function. Also there is a documention on location with google map in this location: https://developers.google.com/maps/documentation/urls/android-intents

Task 4: Implement the Share This Text button

You can use ShareCompat.IntentBuilder to build an Intent and launch a chooser to let the user choose the destination app for sharing. Go to main activity and see the shareText function. More Information Here: https://www.programcreek.com/java-api-examples/index.php?api=android.support.v4.app.ShareCompat

Task 5: Receive an implicit Intent

allowing an Activity in your app to respond to an implicit Intent sent from some other app. An Activity in your app can always be activated from inside or outside your app with an explicit Intent. To allow an Activity to receive an implicit Intent, you define an Intent filter in your app's AndroidManifest. xml file to indicate which types of implicit Intent your Activity is interested in handling.

we created a new activity and we are going to receive implicit intent and show the content inside a textview.

  • Modify AndroidManifest.xml to add an Intent filter. we added the following lines to the activity.
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" android:host="developer.android.com" />

Process the Intent: process the incoming Intent for any data or extras it includes. In this case, the incoming implicit Intent has the URI stored in the Intent data. Visit the ImplicitIntentReciever Activity to see how to recieve the data. More Information Here: https://developer.android.com/guide/components/intents-filters.html

Handing incoming Information: https://developer.android.com/training/sharing/receive

package mohammad.samandari.implicitintent;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;
public class ImplicitIntentReciever extends AppCompatActivity {
TextView txtImplicitIntentReceiver;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_implicit_intent_reciever);
txtImplicitIntentReceiver = findViewById(R.id.txtImplicitIntentReciever);
//We are going to recieve the implicit intent and check for data inside it.
//then we check the uri to see if it is not empty, we are going to set the text of the text view.
Intent intent = getIntent();
Uri uri = intent.getData();
if (uri != null) {
txtImplicitIntentReceiver.setText(uri.toString());
}
}
}
package mohammad.samandari.implicitintent;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ShareCompat;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
EditText website_edittext, loc_edittext, share_edittext;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
website_edittext = findViewById(R.id.website_edittext);
loc_edittext = findViewById(R.id.location_edittext);
share_edittext = findViewById(R.id.share_edittext);
}
public void openWebsite (View view) {
//This function is going to send a implicit intent to open a url in the browser.
String uri = website_edittext.getText().toString();
Uri website = Uri.parse(uri); //Converting the string to a uri.
Intent intent = new Intent(Intent.ACTION_VIEW, website);
//This code checks to see if there is an app to recieve this intent or not.
//if there is an app, it is going to start the intent.
// Find an activity to hand the intent and start that activity.
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Log.d(TAG, "openWebsite: No Intent to open the website.");
}
}
public void openLocation (View view) {
//This function is going to open a location.
String uri = loc_edittext.getText().toString();
Uri LOCATION = Uri.parse("geo:0,0?q=" + uri);
// Intent intent = new Intent(Intent.ACTION_VIEW, LOCATION);
// if (intent.resolveActivity(getPackageManager()) != null) {
// startActivity(intent);
// } else {
// Log.d(TAG, "openLocation: Error");
// }
ShareCompat.IntentBuilder
.from(this).setChooserTitle("Choose App?").setStream(LOCATION).startChooser();
}
public void shareText (View view) {
//using shareCompat.builder to let the user decide which app is going to be used for the text to be shared with.
String SHARE_TEXT = share_edittext.getText().toString(); // The text that is going to be shared.
String mimeType = "text/plain";
ShareCompat.IntentBuilder
.from(this)
.setType(mimeType)
.setChooserTitle("Share This Text With?")
.setText(SHARE_TEXT)
.startChooser();
}
public void startActivity (View view) {
Intent intent = new Intent(getApplicationContext(), ImplicitIntentReciever.class);
startActivity(intent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment