Creating Intents with putExtra - android

I have been creating Intents using putExtra() for quite a while now and just read in the Android documentation that I should be prefixing the 'name' with the package name. So, instead of 'putExtra( "ButtonText", "Ok")' it should be more like 'putExtra( "com.mycompany.myapplication.ButtonText", "Ok" ).
Is this really necessary? (seems to be OK without it).
If it is necessary what is the advantage?
Also, is the package name the callers or the one being called? If it is the callers the 'called Activity' has to know about the callers name which wouldn't be very generic.
Thanks

Is this really necessary? (seems to be OK without it).
No, it isn't necessary for a completely stand-alone app but may be considered to be good practice anyway.
It is more important in apps which are publicly available so they can interact but maintain some way of uniquely identifying themselves and the data they're exchanging. As for which package name is used will depend on the context.
To give an abstract example...
Company A produces an app which can provide some sort of data processing which apps produced by Company B and Company C can use. The 'action' for the intent will be named relevant to Company A but the data passed into it by the two 'client' apps will be named relevant to the client apps companies. Example...
AppA's docs...
To request data processing use:
com.companyA.intent.action.PROCESS_DATA
Pass data with the above intent as an extra named:
<your package name>.SOME_DATA
Now when the relevant component of AppA is called with the above, it will check that there's an 'extra' with a name which ends with .SOME_DATA but it will also be able to maintain that data separately from other data provided by other apps due to the unique prefix. So...
Company B code
Intent i = new Intent(com.companyA.intent.action.PROCESS_DATA);
i.putExtra(com.companyB.SomeApp.SOME_DATA, data);
Company C code
Intent i = new Intent(com.companyA.intent.action.PROCESS_DATA);
i.putExtra(com.companyC.SomeOtherApp.SOME_DATA, data);
OK, possibly not one of my better examples but what it comes down to is it's important to see how the Android environment is very much about different application components being able to use each other, pass data and for the source of that data to be uniquely identifiable.

It sounds more like a "best practices" thing than an actual requirement. The benefit you get here is if you stuff a lot of things in generic intents that are handled by several activities spanning several applications, then you're being more specific about the data in your Intent. If you're using these intents only internally in your application and only your own activities are handling them, you're fine the way you are.

I think this is a GREAT idea IF you are wrapping your extended data into a serializable class say com.mycompany.myapp.MyAppData and sending it as:
intent.putExtra("com.mycompany.myapp.MyAppData",myAppData); //==> out
and retrieving it as:
MyAppData myAppData= (com.mycompany.myapp.MyAppData)intent.getSerializableExtra("com.mycompany.myapp.MyAppData",myAppData; // <== in
JAL

Related

How to make api.ai agent learn something dynamically?

I am currently using api.ai , to create agent to perform specific tasks, but one question i don't have answer to is , can i make it learn something while chatting , mean that i speak my name is 'John Cena' and she should store it and then whenever i ask her again bot should answer me that. i know there is a way to do it by logging into api.ai web and manually add entries , but it will not help, is there any work around programmatically or automatically ? the file i've been using to practice is given in github . and here is working DEMO
You basically need for your bot to "learn" facts. There are many different ways to achieve this, but recently the most common way is to arrange knowledge into Semantic "Triples" and store the knowledge into a Graph repository (like Neo4j, Titan, Spark Graph, etc). In your example, "my name is John Cena" would translate into a Triple like ("anubava","Name","John Cena"). That way, the next time you are logged in as anubhava and ask "What is my name?", it would translate into a Graph search that will return "John Cena". A word of caution, achieving this is not trivial and would require some significant amount of fine tuning. For more info, you can check here and here.
Finally, most complete solutions (that I know of), are Server Side solutions. If you want for the whole knowledge base to reside in your mobile device, you could probably use the resources there as inspiration, and build your own Linked Data repository using an embedded database.
Hope this helps. Good luck.
To store and recall the user's name, you'll need to set up a webhook with some basic data persistence capabilities. Any database or key-value store would work fine.
Here's the breakdown:
Implement webhook fulfillment for the intent that captures the user's name. The webhook should store the name along with a unique, identifying ID that you should supply from your front-end in either the sessionId or as a context parameter in your call to /query.
Implement webhook fulfillment for the intent that reads the user's name. The webhook should look up the name by ID and return a response that tells the user their name.
The high-level docs for writing a fulfillment webhook are here:
https://docs.api.ai/docs/webhook

Listening for updates with RESTful APIs

I have a messenger app that makes GET /conversations requests to populate a list of the conversations of the user.
The next step is to make it "listen" for updates so that it marks conversations that have been updated and add conversations that have been created.
Should I use the same /conversations resource to get the updates or should I rather have a separate resource for that? Perhaps, something like /conversationUpdates.
It depends on whether you want to follow RESTful conventions. Many client side libraries such as backbone and extjs have fairly deep support for declaring a resource with a URI and then using the different HTTP methods (GET, POST, DELETE, etc.) against it. This might sometimes lessen the work clients need to do and folks will be grateful.
Following the convention will also make your api less surprising. There are undoubtedly other conventions for API's and not every domain space is well modeled with REST.
Rereading your post, I see you want to have an api that that just gets new posts. What constitutes new? New since the last time the client called the end point? In such instances an api might accept a parameter like the last identifier that had been received (if you are using something like a auto increment field, or a mongodb id). In that case you would just use the /conversations endpoint, with an extra parameter.
Firstly, I'd stick here with the GET method, since this is exactly the point: getting data.
As for the resource name, I'd go with the same, specifying it further in the query, something like /conversations?state=new. My point here is that the resource itself is still the same but you only want a subset of it.
However, if you plan on updating other things than conversations, you can use /updates/conversations since in this case, an update can be considered as a resource, itself composed of, among other things, conversations.

How would you implement routing within an Activity for AppLinks?

So the applinks documentation states that you should specify your app's package name through the al:android:package property, and the consuming application should launch an Intent to start your app. What I feel is lacking from the documentation is a suggestion or specification on how to provide custom parameters or routing info with that Intent. It's not deep linking unless you specify some depth!
It does specify how to provide Extras through the use of the al_applink_data structure. It does not however say how the target application should provide metadata for the client to consume and send with that structure.
The only suggestion I can think of is to provide the metadata through the optional al:android:url-parameter. So for instance if I'm running a blog, I would provide the URL com.myblog://123, "123" being the ID to a blog entry.
I don't feel like this is an optimal solution. I would then have to parse the URL in order to get the argument. I feel a better solution would be to have a an applink-property named something like al:android:extras where I could give key-value pairs to consume directly. Why is it not implemented this way?
Am I doing it right if I implement metadata-passing the way I described? Is there something I'm missing with regards to the applinks spec?
The original http(s) url is given to you inside al_applink_data under the target_url key, so you can certainly pass metadata that way.
Passing it via the optional al:android:url is also OK.
Lastly, if you have cooperation from the calling app, they can certainly pass data to you via the extras blob.
The reason there's no al:android:extras is that app links was designed to be a routing protocol, and not describe semantics for your app.

Is there a way to add a "name" to an ACTION_CALL intent in android?

Here's a code snippet of what I have:
call_phone = new Intent(Intent.ACTION_CALL);
call_phone.setData(Uri.parse(parameter)); // parameter is a phone number tel:someNumber
con.startActivity(call_phone);
Since the phone number is not listed in my actual contacts - I just wanted to know if there was a way to pass a putExtra or a setName or something along those lines - that would let me make the dialer display any name I want to call - along with the number?
I have looked around on stack overflow and on the android developer website - but can't find anything specific like setting the name, setting the number is all over the place.
Sorry if this answer isn't helpful but:
It's the Phone application which catches the "ACTION_CALL" intent Action. Since this is a device specific app it may vary.
Having a quick google I can't find any information either on what extra's it accepts but I would imagine that injecting a "name" could be considered a security vulnerability and therefore wouldn't be part of the app (however I'm not sure).
Giving your app permissions and functionality to add a contact however (and therefore associating a name with a number) is entirely possible and there are many examples out there on how to interface with the Contacts ContentProvider.
Since the android specification is not mentioning a name parameter on this intent I'd discouarge using it. DOC HERE
Documentation also mentions that most apps should ACTION_DIAL, as ACTION_CALL might have some restrictions on which apps can call it...

Sending parameters from one apk to other

I am able to launch an apk from the button click of other apk. but i have to send parameters also.Like if i am giving values in a text and when i click the button then it should send the parameters to other apk as well it should start other apk. I know how to send parameters between activities in the same package. Please guide me how do i do it.
Update:
I have successfully run that apk's. I just had to put package name instead of class name.
Use
i.putExtra(param, value);//Put extra values to your intents, if you are using intents to launch another app.
You need to use content providers to share data across different applications in Android. Here
is an example as to how to use them. You need to have a URI which identifies your dataset and use the same URI in another application to retrieve data from it.
Use Intents, as explained in Android Developer Documentation or many tutorials over the internet.
You should use Bundles to pass parameters, as you can read there.
You can use files in a common directory to comminucate different apps or basicly you can have serversocket in your first app that runs the other one, so the second one can connect it and transfer data to/from first app.

Categories

Resources