Android: Add launcher icons programmatically - android

In the manifest, it is possible to specify multiple activities:
<activity
android:name=".Activity0"
android:label="#string/app_name0">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity1"
android:label="#string/app_name1">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
These will then result in two icons, placed on the launcher. I was wondering if the application can register more icons programmatically (based on application settings / user interaction with the app). Note that they would not need to run different activities, a single activity, starting with different intents would also work. Is this possible, or does one have to use widgets?

no need to use widgets - you can just add dummy activities that start your needed activity with the right parameters and then finish - you can also exclude them from recents so the user does not see the activity at all.
You can also install a shortcut by runtime - but then you need permission: INSTALL_SHORTCUT

Related

How can I give two different names for two launch-able activities?

I noticed that
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
This part above can make icons.
So, This code will create two icons after installing one apk. But those launch different activity each-A_Activity or B_Activity
<activity
android:name=".ui.A_Activity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ui.B_Activity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
But anyway, this acts like one app after launching one of them. After launching A_Activity, it can reach the other Activities or Resources that B_Activity moves to or uses if the implementation is the similar or the same.
However, what I want to know is making two different names for each icon. Mine has two icons but the same name. Is there any way to give different app names for them? Because I wanna test two different modes at the same time after installing it.
The text underneath the icon on the HOME screen is taken from the
android:label="..."
attribute of the <activity> description in the manifest. If you don't provide this, it uses the android:label of the <application>.
So all you need to do is to provide different android:label strings for each <activity> in the manifest.

How the android app's first activity launched?

when we created a helloworld application using ADT, The "MainActivity" will be loaded, Because every program has a entry, such as the "main" function, For android apps, We can declare many activities in the file called "AndroidManifest.xml", So I wanna know how this activity launched by android framework? which is android apps "main" entry ?
Manifest tells android which activity to launch. Actually, when you click app icon, OS consults with application's manifest file and looks for the launcher activity. You can declare any activity as your launcher by writing this inside your activity tag in manifest.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Manifest always acts as interface between your application and OS. It provides all the information about your app to OS like what are the permissions, what activities, what receivers you are using in your app including your LAUNCHER ACTIVITY.
Only one activity should have the "main" action and "launcher" category...
So in the AndroidManifest.xml file, you should essentially have only one:
action android:name="android.intent.action.MAIN"
category android:name="android.intent.category.DEFAULT"
Activities will very often need to support the CATEGORY_DEFAULT so that they can be found by Context.startActivity(). So, CATEGORY_DEFAULT can appear number of times.
Android does not grab whichever one appears first in the manifest but it starts with activity having CATEGORY_LAUNCHER.
CATEGORY_LAUNCHER : The activity can be the initial activity of a task and is listed in the top-level application launcher.
because of this
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
the activity declared with intent filter
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
will be launched first.
<activity
android:name="com.example.hello.HelloActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

android) can i set a default activity that runs right after installing

in my app, there is two activity and i want to make activity1 to the starting activity after installation.
But now the RUN button (is showed right after packgae installing) is disabled.
below is the manifest file. thanks.
<activity ...1>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity ...2>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</activity>
android) can i set a default activity that runs right after installing
No activity "runs right after installing". The user has to launch it from the launcher.
below is the manifest file
No, it is not. That is not even valid XML.
Also, note that your third <intent-filter> is invalid. Not only are you missing any category (you need at least DEFAULT for activities), but ACTION_POWER_CONNECTED and ACTION_POWER_DISCONNECTED are not activity actions.
I am going to take a guess that you really mean to ask: "I have two activities, both described as ACTION_MAIN/CATEGORY_LAUNCHER, and now the Run button does not work -- what can I do?" The answer would be "either remove the ACTION_MAIN/CATEGORY_LAUNCHER <intent-filter> from one of them, or mark one of the two as disabled (android:enabled="false") and enable it later on using PackageManager."
I think the problem is the second:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
You shouldn't have 2 activities flagged as the MAIN and LAUNCHER activity.
Try removing it within activity2.
Check out: http://developer.android.com/reference/android/content/Intent.html the intentfilter s are discussed.

Android application packaging

I have two android applications- A and B.
I'm trying to package them as a single unit so that upon installation the system installs both apps. However, I could not find any reliable answer till now.
In A's manifest file I added activity tag of B, resulting in error.
Could anyone guide me on how to package two applications as a single unit?
Thanks in advance!
I do not believe this is possible. An APK corresponds to a single AndroidManifest.xml which corresponds to a single Application.
However, if you want mulitple "launchers" or icons, it is possible by exposing multiple Activities by adding IntentFilters to the ones you want (which take the launcher event). However, they are still, technically, a single application.
Update:
Here's how to expose multiple Activities. The main activity would have something similar in the AndroidManifest.xml
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Copy-paste this inside the other Activities you want to expose. For example:
<activity
android:name="com.example.app.FirstActivity"
android:label="#string/first_app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.app.SecondActivity"
android:label="#string/second_app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

How do I get multiple icons to launch different activities in one application?

I have an application with two activities and I'd like to be able to have two icons appear in the launcher, each launching the respective activity within the app.
Specifically, I want one icon to launch my main app, and another icon to launch my settings activity. Is this possible?
Here is what I've tried so far:
<activity android:label="MyApp" android:name=".MyApp">
<intent-filter>
<action android:name=".MyApp"/>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:label="Settings" android:name=".Settings">
<intent-filter>
<action android:name=".Settings"/>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
This creates two launcher icons, but they both run my main app instead of the second icon running my settings app. I've tried just having the launcher category but then I don't get an icon so it looks like I need the main action as well.
Is this the right approach or should I be declaring two applications in the manifest instead?
What you need to do is have your settings activity launch in another task. You can do this by specifying its task affinity. This is done with the attribute android:taskAffinity. By default all activities share the same task affinity that defaults to main package specified in the manifest. On your settings activity you can specify android:taskAffinity="your.own.package.SettingsTask" to have the settings activity launch in its own task.
Extra documentation.
You're definitely going in the right direction. This is what I have (truncated, because I have all of my activities in the list while I'm devving for fast access):
<activity android:name=".DeckDrill"
android:label="DeckDrill">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DeckList"
android:label="DeckList">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I think what may be happening is interference from your action elements which specify the name of your class. I'm pretty sure that actions and categories need to refer to constants. I don't know how that would result in what you're seeing, but you could try removing them. Other than that, you pretty much have what I have.

Categories

Resources