Android: calling Activities explicitly using Intent Url scheme - android

im trying to understand how Intent URL's working in android . i know that i can make implicit call to activities which has a intent-filter set in AndroidManifest file . but as far I've read we can call application Activities which has no intent-filter set in AndroidManifest . i tried some cases but i couldn't make it happen . i wrote a simple application with some Activities (no intent-filter) but i couldn't run them using Intent URL . and i couldn't find any useful example on explicit intent url's in the internet (and stackoverflow) .
so i'd be thankful if anyone could provide a simple exmple for explicit Activity calls using intent url's or a brief explanation .
Regards,
Mohammad

This is a little late for this answer, But for future reference, I'm thinking by Intent Url you mean the Uri of your intent.
There is an easy to find the Uri format of any intent (explicit or implicit) just by declaring an Intent object with those parameters in java and then calling the toUri method which gives you the String format of that intent. You can then use it in your web page to communicate with your app.
Intent intent = new Intent(this, SomeActivity.class);
String intentUri = intent.toUri(0);

Related

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.

How to redirect from own application another application?

I am creating my own app and at one point I want that user can go to any other application.So what will be the best code for that?
You can do that in many ways, but one of the common method is intents. You can create implicit intents for this purpose.
here is a sample code :
//From MainActivity.onCreate()
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://google.com"));
startActivity(intent);`
you can find further details about intents here.

Different types of Android Intents

I have recently started a new Android project and I'm working off the previous developer's code. I'm relatively new to Android and I've come across something that I'm unsure of.
What is the difference between this:
Intent intent = new Intent("com.example.project.MENU");
and this:
Intent intent = new Intent(this, DisplayMenu.class);
I understand what the 2nd code snippet does, I just can't get my head around as to what the first one is doing? Is it referencing the file in the package? Thanks
The first one is an implicit intent, while the second is an explicit intent.
The first one fired an Intent for the action com.example.project.MENU. If you look inside you project AndroidManifest.xml you can see some <intent-filter> balise. This baslise register activity, service or broadcast receiver to different actions.
This mecanism can be used to allow third party app to launch some of your activities.
You can see more on this tutorial http://www.vogella.com/tutorials/AndroidIntent/article.html#intenttypes
Basically an Intent carries some information that are used by the system in order to determine which component should be called for executing the action.
These information are:
Component name: the name of the component that should be launched. (If present the Intent is Explicit)
Action: it specifies the generic action that should be executed (es. ACTION_VIEW, ACTION_SEND). It determines how the rest of the intent is strucutred.
Data: represents the URI that refers to the object that should be associated with the action. For example with the action ACTION_EDIT, the Data should contain the URI of the document that you want modify.
Category: Additional infromation (for example if you want that your app is shown in the launcher you can use CATEGORY_LAUNCHER)
Extras: keys-values pairs that carries additional information
Flags: it is like a metadata that specify how the intent should be managed by the system.
The Intent class provides a lot of different constructors.
The first one you asked for is public Intent (String action)
So, this sets the Action, and lets null all other fields.
The second one public Intent (Context packageContext, Class<?> cls) creates an intent for a specific component by its Component name. All other fields are null. This is a Explicit Intent, since you declare exactly which component should receive it.
The first one is used when you need to call Intent from System
such as Open Camera, Gallery, or Share something to other Application
for example
// this one call Camera to Capture Image
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// this one call gallery to let you select image
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
and That MediaStore.something here is just a Path to the system
for example
MediaStore.ACTION_IMAGE_CAPTURE = "android.media.action.IMAGE_CAPTURE"
Intent.ACTION_PICK = "android.intent.action.PICK"
The first type of intent is mostly used if you want to open another application from your application while the second type of intent is used to open another activity in your application.

Start Wikipedia via Intent

I tried to start the Wikipedia App via Intent but I always get an InvocationTargetException, because the Activity can't be found. I can detect that Wikipedia is installed, but I can't start it. I know that I can also use the Intent Filter and call the Wikipedia URL, but then the user can decide between app and browser (both works different with the search param, which is not very useful) and I want to decide it for the user.
I tried it with following code:
final Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "searchString");
intent.setType("text/plain");
intent.setPackage("org.wikipedia");
Can somebody tell me, what I am doing wrong there or is it just not possible to start the wikipedia app? :-/
Finally I come to a solution after I was looking in the wikipedia code on github (https://github.com/wikimedia/apps-android-wikipedia). The app listens only for View Intents, which means that following code works:
final Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(searchUri);
intent.setPackage("org.wikipedia");
The problem here is, that only URI's for existing articles were handled and no search operation can be used. After watching a little bit more in the code, I found a PageActivity which also listens for ACTION_SEARCH Intents. This would work, when in the AndroidManifest.xml this Intent Filter would be also available.

Which function does intent have? [duplicate]

This question already has answers here:
What is an Intent in Android?
(14 answers)
Closed 8 years ago.
I dont know much about intent (or android) so.. Can someone please explain me what is it exactly? i have search on the internet, A LOT.
Also what does each line of this code do?
Intent intent = new Intent (this, DisplayMessageActivity.class);
intent.putExtra("a", "b");
Thanks in advance
I suggest reading Android Intents
You couldn't have search for very long, since this is very basic topic.
I suggest you read more of Android's API guides.
Line 1 = Create message that describes what to do, in this case start "DisplayMessagActivity"
Line 2 = Add content to the message
Intent intent = new Intent (this, DisplayMessageActivity.class);
For this line, its function is to create a navigation from the current activity/page to the displaymessageactivity page.
it is like from here to there.
For this intent.putExtra("a", "b"); the purpose of this is to put like a temp storage/variable to pass to the next page for retrieval. In this case, you put the value "b" in the variable "a". With this method, you can use the value on the other activity or page.
All the above are just storing of info, it is not executed yet. if you want to execute the intent do the following
startActivity(intent);
The best example to state the behavior of Intent is it behaves like a POSTMAN that delivers message to the stated address.
Whether it may be calling service ,BroadCastRecivers ,Activity they are used in number of occassion.
Intents are asynchronous messages which allow application components
to request functionality from other Android components. Intents allow
you to interact with components from the same applications as well as
with components contributed by other applications. For example, an
activity can start an external activity for taking a picture.
Intents are objects of the android.content.Intent type. Your code can
send them to the Android system defining the components you are
targeting. For example, via the startActivity() method you can define
that the intent should be used to start an activity.
An intent can contain data via a Bundle. This data can be used by the
receiving component.
Intents can be used to start Service, call Activty, call Sub Activity, transfer the data between Activity or retrieve the data from Activity

Categories

Resources