1

I'm a bit confused on Intents.

Why is

Intent implicit=new Intent(IDownload.class.getName()); 

an Implicit intent

while

Intent explicit=new Intent(implicit); 

is an Explicit intent while it doesn't seem to add anything new in its definition. The system doesn't seem to draw any new information that wasn't previously provided by implicit intent above?

In the Android documentation (Intent types),

Explicit intents specify the component to start by name (the fully-qualified class name). You'll typically use an explicit intent to start a component in your own app, because you know the class name of the activity or service you want to start......

Intent implicit=new Intent(IDownload.class.getName()); seems to fulfill this requirement, but is still regarded as an Implicit intent, which as per the documentation:

Implicit intents do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it.....

Intent explicit=new Intent(implicit); seems to contradict this, but is still regarded as an Explicit intent.

UPDATE - Sample implementation

ref : Binding to a Service

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); appContext=(Application)getActivity().getApplicationContext(); Intent implicit=new Intent(IDownload.class.getName()); List<ResolveInfo> matches=getActivity().getPackageManager() .queryIntentServices(implicit, 0); if (matches.size() == 0) { Toast.makeText(getActivity(), "Cannot find a matching service!", Toast.LENGTH_LONG).show(); } else if (matches.size() > 1) { Toast.makeText(getActivity(), "Found multiple matching services!", Toast.LENGTH_LONG).show(); } else { ServiceInfo svcInfo=matches.get(0).serviceInfo; try { String otherHash=SignatureUtils.getSignatureHash(getActivity(), svcInfo.applicationInfo.packageName); String expected=getActivity().getString(R.string.expected_sig_hash); if (expected.equals(otherHash)) { Intent explicit=new Intent(implicit); ComponentName cn=new ComponentName(svcInfo.applicationInfo.packageName, svcInfo.name); explicit.setComponent(cn); appContext.bindService(explicit, this, Context.BIND_AUTO_CREATE); } else { Toast.makeText(getActivity(), "Unexpected signature found!", Toast.LENGTH_LONG).show(); } } catch (Exception e) { Log.e(getClass().getSimpleName(), "Exception trying to get signature hash", e); } } } 
4
  • What makes you say that Intent explicit=new Intent(implicit); is an explicit intent? Commented Jul 29, 2017 at 4:24
  • stackoverflow.com/a/37907805/3663765 @BenP. Commented Jul 29, 2017 at 4:30
  • In that example, the Intent is still an implicit intent until he calls explicit.setComponent(cn). It is the setting of the component name that makes the intent explicit; he's just naming the variable explicit because in a few lines it will be. Commented Jul 29, 2017 at 4:34
  • The handling component is set in the following code, and that make the Intent explicit. Commented Jul 29, 2017 at 4:34

1 Answer 1

2

The linked example includes these lines

 Intent explicit=new Intent(implicit); ComponentName cn=new ComponentName(svcInfo.applicationInfo.packageName, svcInfo.name); explicit.setComponent(cn); 

The first line simply creates a new Intent instance that's a copy of the old one. While the variable might be explicit, at this point in time, it is still an implicit intent.

The second line creates a ComponentName object based on the single matching result from an earlier getActivity().getPackageManager().queryIntentServices(implicit, 0) call. At this point, explicit is still an implicit intent.

The third line is the magic. Once the specific ComponentName is assigned to the Intent instance, explicit truly becomes an explicit intent.

Sign up to request clarification or add additional context in comments.

2 Comments

IDownload.class.getName() doesn't this qualify as a fully-qualified class name - Android documentation (Intent types)?
Class.getName() returns a String, which means you're using the Intent(String) constructor. In this case, the string is the "action", which is not a "fully-qualified class name". You'd need to use the Intent(Context, Class) or Intent(String, Uri, Context, Class) constructors.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.