Intents and their parameters - android

It seems like from the various tutorials ive seen people would jump from one activity to another by calling intents from the activity they are working on.
ex:intent(this, anotheractivity.class)
sometimes I see people just using the class they are going to as the parameter
ex: intent (android.content.blahblahblah)
whats the difference in functionality?
is the second made for default classes?
help is much appreciated

There are two "types" of Intents. One is called and explicit Intent in which you tell Android the specific class you are trying to reach (i.e. MyClass.class). The other case uses Android's filtering system to give you the best "match" for the Class you are targeting. When you call new Intent(String action) you are specifying an action to match with some IntentFilter. If your app shares the IntentFilter with some other app, then a dialog will pop up so the user can select which app he/she wants to use.

Related

Android intent opposite of setComponent

I know I can force an intent to use a specific application/activity by using intent.setComponent(). But is there a way to say that it should not use a specific app/activity?
Why I need this:
My application will be able to respond to several urls (like http://www.companyname.com/something/12345). If my app launches cause of an Intent to this url, I'll check if I have the needed data to handle this. (In my example, something with id 12345). If I'm not able to do something useful with the intent, I create a new Intent with the same data, so the user can try to view the content in its browser.
This is the moment my problem occurs. When I start my newly created intent, my app appears again in the list of apps that can handle this. But I already know that my app can't do anything with this intent. The user already knows that my app can't handle the intent, but still it'd be nice if my app just didn't appear in this list.
Thanks in advance.
A possible solution that works is when creating the new intent, instead of intent.setData(uri) I do intent.setDataAndType(uri, "text/html").
This causes that my app disappears from the list. At the moment it's a solution that works :) But still I'd like to know if there are better ways to achieve this result.
You may use Intent-Filters for your app and set up the URI with wildcards as you need, see here for more information. With some additional info via an action or category, you may ensure, that your app it the only one (not) matching against the intent.

Android: Proper way to reuse my app's activities?

What's the proper way for other people to reuse my Activities? Should they hard code the intent actions in their app or is it customary to provide them with a jar file enumerating my app's intent actions? Is there a less tightly-coupled way to lookup the intent actions?
First of all, take a look at openintents.org and see if there's any match to what your activity does.
Secondly, documentation is always a good idea.
Having the intent details hardcoded in their applications should work just fine. After all, the intents are part of your public interface and shouldn't change.
Your applications manifest should announce what sorts of things your activity can handle, via intent-filters. Outside users can read the manifest to determine what actions you support, and invoke them via action intents.
See intents and intent filters for more details.

LauncherActivity usage

Could you please explain me purpose and usage of LauncherActivity? Documentation says that it "Displays a list of all activities which can be performed for a given intent". I understood that it should automatically build list of all activities found in application and provide their launching. Am I right?
And how to use it? I have found no examples in the web.
Google Code shows the class code itself... It has a different constructor than is described in the Android Platform API.
public abstract class LauncherActivity extends ListActivity {
Intent mIntent;
PackageManager mPackageManager;
IconResizer mIconResizer;
Your phone can have more than one possible app which handles a given intent. One great example is opening a webpage. There's the stock WebKit based browser, you can install Firefox Mobile, Dolphin Browser, Opera Mini... When they all advertise that they can handle a given intent, how does the device know which one it should pass the intent to?
Android will use a LauncherActivity to bring up a selection list of packages where each one listed is one that knows how to do something with the given intent you provide it. When you pick one, you're picking which app you want, and the intent is routed to the matching app.
From that perspective, it's a class that's really part of the Android OS support code, part of figuring out where to distribute given intents to. It's hard to see a situation where you would need to involve yourself with it directly... you should be able to just call StartActivity(Intent), which throws the intent over the wall to the OS, and at that point the device itself should fire up LauncherActivity on its own (if it's even needed).
Completely unrelated (and horribly name-disambiguated) is your application's "Launcher Activity" (documentation) -- an activity that shows in AndroidManifest.xml with an intent filter with "android.intent.action.MAIN" and "android.intent.category.LAUNCHER" ... This is how your app advertises to the system that it wants to have an icon in the device's application list, and that a specific activity should be started when that icon's clicked. You absolutely need to do that.

Intent resolution in Android

If I want to create custom address book (which overrides my phone's default address book), and if I want it to be used by all applications, what should be my intent-filter? Does Android allow me to do such a thing considering the fact that such a third-party app could potentially be malicious?!
And, if I want to have yet another address book application, I suppose the second app also has same intent-filter, isn't it? How does the framework decide which app to pick if I click on Contacts button when making a call? In other words, how does the framework resolve intents in case,there is a conflict between multiple intent-filters?
You can replace any application on Android platform, even Home. Android documentation explains everything there is to know about Intents and Intent Filters and there is a section called Intent Resolution that answers your question. Intent Resolution section for Intent class has some additional information.
As far as I can tell Android doesn't try to resolve a conflict. It ask the user which application to run and gives them the choice to mark this Activity as the default for this Intent. They give an example about mail app here.
While Mr. Smiljanić is basically correct, there is no Contacts application in Android for you to replace. There is Dialtacts, which is the application supporting the contacts, call log, and dialer. That application cannot be replaced, mostly because the dialer cannot be replaced.
So, while you may be able to override some intent filters and get control on some contacts-related requests, you will be unable to get the contacts portion of Dialtacts overridden, which will confuse users.

Android Override Explicit Intent

My app needs to have a intent-filter that responds to a Intent that has it's component set (a explicit intent.) Here is a example.
Intent i = new Intent();
i.setClassName("com.compareeverywhere","com.compareeverywhere.ScanActivity");
startActivity(i);
Just a simple intent-filter will not do - because the Intent is made for a specific component (Activity,) it just launches that without looking for intents at all. Is there a way to do this?
Thanks, Isaac Waller
P.S: Please don't reply "No."
No.
:) That being said, imagine what would happen if Android allowed people to hijack Intents for specific components. Don't like a competitor's app? Just have yours hijack his main Activity with your own to display porn. Intents can specify specific components specifically because the authors don't want others to be able to replace them.
You have two options. If this is your own code, replace it with a generic intent, or if it belongs to someone else, contact them, and ask nicely for them to change it to a generic intent along with some good reasons why that is necessary.

Categories

Resources