Difference between implicit and explicit intents [

2019-03-23 11:45发布

问题:

This question already has an answer here:

  • What is the different between Explicit and implicit activity call in android? 5 answers

I'm confused about the difference between implicit and explicit intents. What is the purpose of implicit and explicit intents, and why are these concepts used?

I am new to Android applications, so please provide some examples.

回答1:

  1. Explicit Intent: Explicit intent names the component.

  2. Implicit Intent: Implicit Intents have not specified a component.

E.g: The java class which should be called Implicit intent asked the system to perform a service without telling the system which java class should do this service.



回答2:

Implicit activity call

With an intent filter you create action for your activity so other apps can call your activity via an action:

<activity android:name=".BrowserActivitiy" android:label="@string/app_name">
  <intent-filter>
     <action android:name="android.intent.action.VIEW" />
     <category android:name="android.intent.category.DEFAULT" />
     <data android:scheme="http"/> 
  </intent-filter>
</activity>

.

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
startActivity(intent);

Explicit activity call

You make a call that indicates exactly which activity class to use:

Intent intent = new Intent(this, ActivityABC.class);
startActivity(intent);

Here's an additional reference