Reputation: 234
I developed an Android app which read a config file.
Now, I would like load my app with another file config.
In fact, I would like create 2 shortcuts of my app:
Is it possible?
If yes, how get the parameters in my code?
Thanks for your help
Upvotes: 4
Views: 11853
Reputation: 6269
This is easy to do on Android 7.1 Nougat API level 25 or higher.
There is a dedicated Shortcut API that lets you define shortcuts to you activity with specific Intent
where you can add extras like this:
<intent
android:action="android.intent.action.LAUNCH_WITH_CONFIG"
android:targetPackage="com.example.myapplication"
android:targetClass="com.example.myapplication.ComposeActivity" >
<extra android:name="configFile" android:value="filepathconfig1" />
</intent>
The code above is base on an example here.
EDIT BASED ON COMMENT: You can also add and remove shortcuts dynamically as described here. You will need some UI in you app to manage the shortcuts if you want to change them as a user, without recompiling.
To access the extra in you code, just do this in you activity class:
String configPath = getIntent().getStringExtra("configFile");
How ever, if you need to support Android versions before 7.1, it gets more complicated.
There are two options I can think of: The first one is using <activity-alias> as described here.
Note however that the author of that question states that it is not guaranteed to work on all devices.
Another option is to create two dummy launcher activities that will be invisible and will finish()
immediately after starting you real main activity with the correct parameter.
This is the ugliest hack in my opinion but also most reliable and universal one for this problem.
In your manifest, set activity theme to android:theme="@android:style/Theme.Translucent.NoTitleBar"
In this activity's onCreate
add code like this:
Intent i = new Intent(this, RealMainActivity.class);
i.puStringExtra("configFile", "filepathconfig1");
startActivity(i);
finish();
I see from your comment you want this to work dynamically, without recompiling the APK, but Android launchers do not support such a feature. At least, I never heard of one that does.
It is possible to create such a launcher though.
The only way currently to start you activity with dynamic parameters is from adb:
adb shell am start com.myactivity --es configFile configFilePath
For more details read this.
Upvotes: 6
Reputation: 815
You have two options here :
Use shortcuts https://developer.android.com/guide/topics/ui/shortcuts/
If you want two different icons. Define two main launch activities like below and define your config files in the activities.
<activity
android:name="your.package.Activity1"
android:label="Activity1">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="your.package.Activity2"
android:label="Activity 2">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Upvotes: 0