3

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:

  • myApp filepathconfig1
  • myApp filepathconfig2

Is it possible?
If yes, how get the parameters in my code?

Thanks for your help

bunbun
  • 2,595
  • 3
  • 34
  • 52
lg0173
  • 208
  • 2
  • 8
  • 20

2 Answers2

4

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.

Lev M.
  • 6,088
  • 1
  • 10
  • 23
  • Then you can use the ShortcutManager https://developer.android.com/reference/android/content/pm/ShortcutManager to dynamically create and delete shortcuts from within you own app. You can create a shortcut editor in your app then you won't have to recompile it to add path of new file. Full explanation on how to manage shortcuts dynamically is in the link in my answer. – Lev M. Nov 30 '18 at 09:42
  • OK I look a sample [here](https://medium.com/@andreworobator/implementing-android-app-shortcuts-74feb524959b) – lg0173 Nov 30 '18 at 09:57
0

You have two options here :

  1. Use shortcuts https://developer.android.com/guide/topics/ui/shortcuts/

  2. 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>
    
arbrcr
  • 815
  • 4
  • 7
  • Option 2 : but it's in the code and not dynamic. If I want add a third config I have to recompilated and I don't want... – lg0173 Nov 30 '18 at 09:15
  • what are you trying to build. how do you want to provide the config dynamically – arbrcr Nov 30 '18 at 09:18
  • I want build apk, install apk, create 2 to n differents shorcuts and my app read a different config every time ... perhaps it's not possible ... – lg0173 Nov 30 '18 at 09:28