How do I build an intent and the Manifest so that I can invoke an activity with my application as an implicit intent.
The idea is that I am writing some general code that I wish to use in a number of applications.
This code is held in an Android library and is linked to the using application.
This general code opens an activity and does some work.
Once it is done (user clicks a button) it needs to transfer to an activity that is specific to this application.
As per my understanding on your questions,
You can declare your activity with specified implicit intent in your application's manifest file..
For example: If you want to make a application for view Image with your application and you have to use that application on your device which can allow other activity to view image using implicit intent action.VIEW so ,just
<activity android:label="#string/app_name" android:name=".MyImageViewerActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.LAUNCHER">
</category></action></intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW">
<category android:name="android.intent.category.DEFAULT">
<data android:mimetype="image/*">
</data></category></action></intent-filter>
</activity>
In the above code we can see that intent-filter has following properties.
a. action: type for which this activity will respond for.Like this activity will respond for view action.
b. category: implicit intents are divided in categories.Like this activity is in default category.
c. data: Specify more information about intent-filter. Like this activity will handle file of mimeType “image/*”.
And when you install this application in your device and click on any image file, you will see a choice dialog that offer you to select between default application and your application. Just select your application to see the image.
For more info with example look at this What is intent filter in android?
Tutorial: Registering via Intentfilter
I think you can use intent-filters. I didn't use it for such things, but I hope that will help you.
Idea is that you can call your activity not by name, but by it's action (that is setted in intent-filter in your Manifest)
Related
What needs to be done to make your app appear as an option app.
For eg, when I select an image file, android shows me a list of applications to open the file with, like Gallery,Photos etc.
I want that android also shows my app in this list.
How to achieve this? I am unable to understand which android classes to use for it? Or do I need to modify manifest file to add some specific intents?
By defining intent filter you can achieve this. You can register your Android components via intent filters for certain events.If a component does not define one, it can only be called by explicit intents. The key for this registration is that your component registers for the correct action, mime-type and specifies the correct meta-data.
Eg.
<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>
Above code will register an Activity for the Intent which is triggered when someone wants to open a webpage.
Source: http://www.vogella.com/tutorials/AndroidIntent/article.html
I'm working with activities I know how to open activities via intent, but I want to know how can I open activity via intent-filter and what is the role of intent-filter to open activities.
How many ways to open the activity?
Activity can even be launched via IntentFilter
try this out
Basically when you install your app, Android system will register the activity with corresponding action, when you declare your activity with custom action, Android system stores the activity with the respective activity. When you launch the intent with your custom action. The system will find the receiving activity and launch it it there is only one activity matching it, if there are more than one Activity receiving that action, System will ask the user to choose which activity to complete the action.
declare activity in manifest as
<activity
android:name=".YourActivity" >
<intent-filter>
<action android:name="your.custom.ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
then you can start this activity by just calling
startActivity(new Intent("your.custom.ACTION"));
An IntentFilter is used with BroadcastReceivers.
The BroadcastReceiver then gets activated when any intent that fits through the filter arrives in the system.
This is generally used to send messages between activities, between different applications, or from the server to the application.
See BroadcastReceiver documentation:
http://developer.android.com/reference/android/content/BroadcastReceiver.html
or this tutorial:
http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html
I looked up intent filters and found that they will be used when "Android finds the appropriate component to start by comparing the contents of the intent to the intent filters declared in the manifest file of other apps on the device"(http://developer.android.com/guide/components/intents-filters.html#Building)
In my manifest file, I have
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
which from reading that guide means that this activity can handle an implicit intent with action of main and category of launcher.
However what if i have multiple applications with the same intent filter in the manifest file. I know that some implicit intent will be called with action of main and category of launcher. How does the Android O.S know to choose this application?
when you have multiple activities defined with same intent filter(action=main and category=launcher), then android takes the first activity defined in the hierarchy with that intent filter (action=main and category=launcher) and will launch it when user clicks on app icon.
Could someone please explain, when will the NoteList activity be spawned with intent actions ACTION_VIEW, ACTION_EDIT, ACTION_GET_CONTENT.
I tried commenting out the code for the particular intents below from NoteList activity, and the application worked just fine
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<action android:name="android.intent.action.PICK" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
</intent-filter>
Also inside NoteList activity, i did not find any handling for the below intent actions. So why even define them??
Say some startActivityFoorResult() calls this intent, how will this intent even be resolved inside NoteList when it does not have any handler inside NoteList activity.
Also who can and calls in the example the intent actions?
Links for android NotePad example is below.
http://developer.android.com/guide/topics/intents/intents-filters.html
http://developer.android.com/resources/samples/NotePad/index.html
Thanks in advance
If you notice the last line inside the intent filter, you'll see that it specifies a mime type. This associates the app with that mime type. That means that when you open a file/url with that mime type, it will try to open it with this app. The fact that you removed the intent filter doesn't necessarily mean that the app will stop working, it will simply no longer handle this mime type.
Inside NotesList.java you will see several references to Intent.ACTION_EDIT, Intent.ACTION_PICK, etc. which are constants for "android.intent.action.EDIT" etc.
Update:
If you look at the manifest file you'll see that the min SDK version is set to 11. This is apparently a HoneyComb manifest file. I glanced at the code and it doesn't seem like the code linked directly on the website uses fragments, but I strongly suspect that the manifest was for a version that uses fragments. I know for sure there's a version out there with fragments as I've played with it at some point. Google used it for their recent Android Developer Labs. This would match up with the intent filter that you saw, as it would allow for editing/viewing/picking a note from the list which would open up the fragment to view/edit/etc. In short, I think the code and manifest are probably a little out of sync.
In my manifest file, I have given the application name as MyApp and the name of the starting activity as Main Menu.
<application android:theme="#style/theme" android:icon="#drawable/myicon" android:label="#string/app_name">
<activity android:name=".MainMenu"
android:label="#string/mainmenu_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
When I see the app icon in the default menu screen of android, the name shown is Main Menu. Why is MyApp not the name shown for the app?
Because your app can define more than one activity that shows up in the launcher. All activities that have this type of intent filter:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Will appear in the launcher, each with its own label. The fact that if you don't define a label on your activity, it's inherited from the application is basically a commodity.
Also, every activity has its own android:icon (which, again, is inherited from <application> for commodity). With this property your two launcher entries can have not only different labels, but different icons as well.
On a more general note, even "non-main" activities can handle certain types of events, and in these cases their labels and icons are relevant. For example, if one of your activities (doesn't matter which one) handles ACTION_SEND intents (like most sharing apps do -- see Facebook, Twitter, ...), then whenever the user presses a "Share" button in any application, a list of applications that can complete that certain activity is displayed to the user (in this case these applications could be Facebook, Twitter and your app). This list will contain an icon and a label (i.e. Facebook). Your entry's display will be dictated by the android:icon and android:label attributes defined by your receiving <activity>, and only if it doesn't define them they'll be taken from your <application>.
This allows you to have an entry such as "Share via MyApp" without modifying your application's name. Or, if your application also has an activity that handles ACTION_VIEW, that activity could have, for example, the label "View in MyApp".
Digging even deeper, <intent-filter> elements can have their own label and icon. Basically, Android chooses the icon and label to display like this:
If the <intent-filter> has it, pick that, stop.
If the <activity> has it, pick that, stop.
Pick the one in <application>.
This is helpful in two situations:
You don't want to change your activity's label / icon: maybe you want to use those in other places such as the title bar (and you don't want your title bar to say "Share via MyApp")
You have activities with multiple intent filters. For example, you could handle both ACTION_SEND and ACTION_VIEW intents in the same activity. In this case, you'd have two intent filters set for that activity with the labels "Share via MyApp" and "View in MyApp".
I hope I've made myself clear, these are very basic things that can be pretty hard to grasp at first, but that provide great flexibility in building apps and integrating them with the Android system and/or other apps.
Another thing to note is that you can easily have an application that doesn't have any entries in the launcher (by not having any activity with the intent filter mentioned above). These are perfectly valid apps and it's good practice when creating Service-only apps (such as widgets).
If android:label is set for activity then its value is taken. It it is omitted for activity then the android:label attribute set on application is taken instead.
You simply cannot have different labels for activity and for application as your application has one launcher for your activity with the label you set it to have. I'm not quite sure I totally understand what you are trying to achieve tho.
In the manifest, remove the android:label attribute on the launch activity. Then the activity will inherit the name specified in the application's android:label.