Do implicit intents do a broadcast internally? - android

Following piece of code open an URL using implicit intent.
EditText editText = (EditText) findViewById(R.id.url_editText);
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(editText.getText().toString()));
startActivity(myIntent);
It shows up all the applications that support this action Intent.ACTION_VIEW like Chrome, Firefox etc. Hence I am assuming this procedure internally does a broadcast with action Intent.ACTION_VIEW. Please correct me if I have misunderstood.
As per above assumption I tried the foloowing code
Intent myIntent = new Intent(Intent.ACTION_VIEW);
sendBroadcast(myIntent);
but it does not work. Nothing is shown. What is the difference. Can someone clear my confusion?

This has to do with IntentFilters. Before launching the Activity, it's asking the system to give it the list of everything that's an Activity and can handle the intent -- no broadcasting involved here.
As for sendBroadcast() -- it's essentially the same thing but for BroadcastReceivers. The mechanism is the same: match intent filters, deliver the Intent, but the Intent is delivered to all the receivers regardless of their quantity (as opposed to what startActivity() does -- because it can only result in starting a single activity, hence the need to choose one if there are multiple that match).
I don't think there are any BroadcastReceivers registered for Intent.ACTION_VIEW (since it's an action whose purpose is to start an activity, there's no logical reason to listen for it and start nothing, except count activity launches or something) but you can register one yourself and see what happens.

Hence I am assuming this procedure internally does a broadcast with action Intent.ACTION_VIEW.
No.
but it does not work. Nothing is shown
Of course.
What is the difference.
startActivity() != sendBroadast(). They are separate operations, just as addition and subtraction are separate mathematical operations.
If you wish to think of the Intent system as being a bit like a message bus, that bus has three totally separate channels:
activities (startActivity())
services (startService(), bindService())
broadcasts (sendBroadcast(), sendOrderedBroadcast(), etc.)

The difference between those two is just who receives the Intent. If you call sendBroadcast() the Intent will be sent to BroadcastReceivers. If you call startActivity() the Intent will be sent to Activities. That's the reason why this:
Intent myIntent = new Intent(Intent.ACTION_VIEW);
sendBroadcast(myIntent);
Doesn't start an Activity, because the Intent is only visible to BroadcastReceivers.
The same goes for startService(). By calling that method the Intent will only target Services. I guess the confusion comes from the word broadcast. It implies that it is sent everywhere and visible to everyone, but that is not the case. Broadcasts are only visible to BroadcastReceivers just like if you call startActivity() the Intent will only target Activities and nothing else.
You can find more information here.

Related

How intents and startActvity works internally?

Everyone knows if you create intent to start another activity, you pass in as a parameter into startActivity. But I just thought about the possible scenario: intent says system "call this activity", the system sees manifest and then runs activity, or this running acts internally in the app, something like "call some method of some class"?
Probably a stupid question, but I couldn't find enough info. So how does it works?
Following is the way how intent communication works:
Activity A creates an Intent with an action description and passes it to startActivity().
The Android System searches all apps for an intent filter that matches the intent. When a match is found,
the system starts the matching activity (Activity B) by invoking its onCreate() method and passing it the Intent.!

What's the relationship between the Activity ,Action and Intent in Android?

I fell confused about the relationship between the Activity, Action and Intent?
And also an another question about the Implicit intents : for example I want to start this action ACTION_CALL to make a phone call,obviously, this is an Implicit intent, so I should write like this Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
But I should also need to set the data. Here is my question : How can I know what's the uri looks like? What's the schema?Please just don't tell the simple answer.How do you know that? Is there any API I should look for? And I know in the manifest <data android:scheme="xxxxx"/> So where is the manifest about the action or activity Intent.ACTION_CALL? I can't find it.
Activity - It says, from where to where you want to send message e.g: from Activity A to Activity B.
Action - It says, what this message means e.g: Open a URL in webview or make a call etc.
Intent - It says, how to transfer the message and other extra information e.g: directly through explicit intent or indirectly by selecting from multiple options through implicit intent.

Differents between sendBroadcast (intent) and startActivity(intent) in android?

When i send a broadcast by context.sendBroadcast(intent1) method with parameter: intent1 and start a activity by context.startActivity(intent2) method with parameter: intent2. What is difference between them. Are intent1 and intent2 implicit intent with define: new intent(action_do_something). Can anyone help me ? Thank a lot
As the names suggest, sendBroadcast will send a message to no particular recipient. It just transmits a message like a radio tower. You have to listen for broadcasts.
On the other hand, startActivity starts an activity(the onResume() of the activity will be eventually called).
The names are saying it all. When you send a broadcast message you need to call the sendBroadcast. but to start activity u need the other method. There is no direct comparision between these two as the purpose of these two things is totally different.
In both case the intent is used for a common reason, first two define the receiver. In the broadcast message the intent is passed to make sure which type of receiver can catch this. And for startActivity it is used to do same but for to make sure which activity will be started. and in both cases intent is used to pass data.

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

What is the difference between explicit and implicit activity call in android? If you explain the answer with a simple example will be good.
For example:
implicit activity call
In intent filter you create action for you activity, so other app can call your activity via this action as following:
<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>
And the other way to call implicit Intent is below:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
startActivity(intent);
Explicit activity call
You make a call that indicate exactly which activity class:
Intent intent = new Intent(this, ActivityABC.class);
intent.putExtra("Value", "This value for ActivityABC");
startActivity(intent);
Hope this help you understand more about Explicit and implicit activity call in android.
You can get more detail about Android Intent here
Explicit Intents are used to call a specific component. When you know which component you want to launch and you do not want to give the user free control over which component to use.For example, you have an application that has 2 activities. Activity A and activity B. You want to launch activity B from activity A. In this case you define an explicit intent targeting activityB and then use it to directly call it.
Implicit Intents are used when you have an idea of what you want to do, but you do not know which component should be launched. Or if you want to give the user an option to choose between a list of components to use. If these Intents are send to the Android system it searches for all components which are registered for the specific action and the data type. If only one component is found, Android starts the component directly. For example, you have an application that uses the camera to take photos. One of the features of your application is that you give the user the possibility to send the photos he has taken. You do not know what kind of application the user has that can send photos, and you also want to give the user an option to choose which external application to use if he has more than one. In this case you would not use an explicit intent. Instead you should use an implicit intent that has its action set to ACTION_SEND and its data extra set to the URI of the photo.
An explicit intent is always delivered to its target, no matter what it contains; the filter is not consulted. But an implicit intent is delivered to a component only if it can pass through one of the component's filters
Every time I get confused among these in either interview. So, I have summarised it like this, may it help someone to keep this difference in mind.
Summary:
In Implicit Intents, the user implicitly tells the system WHAT should be done, without specifying who should do.
In Explicit Intents, the user explicitly tells the system WHOM to be triggered for whatever the work is.
See Intent Resolution here
http://developer.android.com/guide/topics/intents/intents-filters.html
Explicit intents (activities) refer to a specific class, and in general, are only available to your packages. Implicit intents refer to intent filters where apps publicly announce that they can handle certain types of data or can provide specific services, e.g. send an email. With implicit intents, the users chooses which activity (typically a package) to use to handle the intent or if a default handler is set, it is launched.
When to use which?
Explicit intent: When you know which component can handle your request. So you explicitly mention that component name in the intent.
Intent i = new Intent(context,DetailActivity.class); // DetailActivity.class is the component name
startActivity(i);
Implicit intent: When you don't know which application can handle your request then you mention the action in intent and let the OS decide which application/s is/are suitable for your task.
Example: Play music
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(file);
startActivity(intent);
How OS decides?
When there is implicit call with an intent then OS takes out the action and then it matches with all the intent-filters of all the registered activities of all application using PackageManager and then populates the result as a list. It is called intent resolution
So there is a possibility that no application is available in your device which can handle your request. In that case, you will get NullPointer Exception.
So a safer way to call implicit intent would be this
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(file);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
There are two types of intents:
Explicit Intent:
While creating an Intent object when we explicitly specify and pass on the target component name directly in the intent, it’s an explicit intent.
Implicit Intent:
In this case we delegate the task of evaluating the registered components (registration is usually done using intent filters that we’ll cover later) to Android based on the intent data and the intended action (like send an email, capture a photo, pin location on a map, etc.) that we pass. So Android will automatically fire up the component from the same app or some other app that can handle the intent message/job. The idea in this case is that, let’s say we have to pin a location on a map, for that we don’t have to code our own activity to handle that. Instead just pass on the location data to an app like Google maps that can do the job on our app’s behalf.
source : http://codetheory.in/android-intents/
Implicit intent doesn't specify the component. Intent provides the information of a component
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.google.com"));
startActivity(intent);
whereas,
Explicit intent specify the component. The intent provides information about the class.
Intent i = new Intent(this, ClassB.class);
startActivity(i);

Android: possible to unicast an intent?

In Android, is it possible to unicast an Intent to an Activity? The only options I see for sending intents is either to start an activity or issue a broadcast, but I only want a specific target to receive my Intent.
It is possible to set a specific ComponentName via either a constructor or the setComponent and setClassName methods.
maybe try to
specify your intent and put private BroadcastReceiver in your target?
Yes. Declare your Activity without any Intent filters:
<activity android:name=".MapActivity">
</activity>
Then send your startActivity with an Intent that has a component name:
ComponentName comp = new ComponentName("package.name","package.name.MapActivity");
Intent intent = new Intent();
intent.setComponent(comp);
this.startActivity(intent);
You could also put a different application's Activity in the ComponentName field.
More about this here.
From dev guide about intent resolution seems that with explicit intents you get the behavior you're searching for:
Explicit intents designate the target
component by its name […] explicit
intents are typically used for
application-internal messages
As long as you're specifying the target the system should not search for a match among registered filters:
In the absence of a designated target,
the Android system must find the best
component (or components) to handle
the intent
Also, as you asked about sending a message to an Activity, it's worth noticing:
There is no overlap within these
messaging systems: Broadcast intents
are delivered only to broadcast
receivers, never to activities or
services. An intent passed to
startActivity() is delivered only to
an activity, never to a service or
broadcast receiver, and so on.

Categories

Resources