简体   繁体   中英

Explicit and Implicit intents

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);
    }
  }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM