Pregunta

I want to change application configuration for the connection server where I got two options: Test, Production. This is set using a static string inside of one of my Helper classes.

Now I want to make this change from outside the application, using another icon in the system. The reason for that is that I don't want the user be able to do so (And I don't want it to be a part of my application). Only the development team that has to check the application in the field could add this icon and make this change.

So I don't want to create some kind of widget that will get installed with my application.

Is there a way to do some thing like that? If so how can this be done? Should I make a whole new application for that?

Thanks.

¿Fue útil?

Solución 2

I have ended up using a Url Scheme, for this task, more information can be found here:

android custom url scheme..?

And the code is, in my Manifest file in the main Activity I provided the following intent-filter:

  <intent-filter>
     <action android:name="android.intent.action.VIEW" />
     <category android:name="android.intent.category.DEFAULT" />
     <category android:name="android.intent.category.BROWSABLE" />
     <data android:scheme="myapp" android:host="com.myhost" />
  </intent-filter>

And in the activity it self I do this:

    Intent intent = getIntent();
    String value = null;
    if (intent.getData() != null)
    {
        value = intent.getData().getQueryParameter("server"); 
    }
    if (value != null)
    {
        Log.d(TAG, "with scheme value: "+ value);
        if (value.equals("my_test_server_address"))
        {
            Toast.makeText(this, "Server set to Test" , Toast.LENGTH_LONG).show();
        }
        else if (value.equals("my_production_server_address"))
        {
            Toast.makeText(this, "Server set to Production" , Toast.LENGTH_LONG).show();
        }
        else
        {   
            Toast.makeText(this, "Server set to Address: "+ value , Toast.LENGTH_LONG).show();
        }
        Consts.BASE_URL = Uri.parse(value);
    }
    else
    {
        Log.d(TAG, "value was null");
    }

Finally to start you application with this intent filter you need to create an HTML file, with the following code:

<a href="myapp://com.myhost?server=my_test_server_address">test</a>
<a href="myapp://com.myhost?server=my_production_server_address">production</a>

Otros consejos

you can set this option in SharedPreferences and create an activity for the development team to set it, with the LAUNCHER option in the manifest, so it'll have a launch icon.

what you can do to hide it from your users is removing this activity from the manifest for release builds.

if you're using Android Studio / Gradle, you can use different AndroidManifest.xml for different build types, see How to tell Gradle to use a different AndroidManifest from the command line?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top