How to pass arguments to running an android application?

0

I would like to know how to pass arguments to running an android app, or even create a configuration file to be queried when launching the application.

    
asked by anonymous 11.03.2015 / 14:31

3 answers

1

You can do this with Shared Preferences .

This will allow you to create a preferences or settings screen for your application. You'll be able to load preference at any application runtime.

Introduction

Shared Preferences allows you to persist data in key-value , that is, for each key or identifier ( string ) you will have to associate a value with this ( boolean, float, whatever, ... ).

In which cases should I use?

Shared Preferences is often used to save application settings information. But it is not recommended that you save user information (eg: profile system, etc ... )

How to use?

public class MyClass extends Activity {
    SharedPreferences sPreferences = null;

    @Override
    public void onCreate (Bundle cicle) {
        super.onCreate(cicle);
        setContentView(R.layout.main_layout); // Coloque seu Layout aqui!

        /**
        * @SharedPreferences
        * @Desc: Quando a activity for criada, o código abaixo vai 'pegar' a preferência "preferences_app_button_red"
        *  Para quando a activity resumir, ele verificar seu valor.
        */
        sPreferences = getSharedPreferences("preferences_app_button_red", MODE_PRIVATE); 
    }

    @Override
    public void onResume () {
        super.onResume();

        /** 
         * @Desc: este código verifica se o valor da chave "preferences_app_button_red" é verdadeiro ou falso.
         * @True: Vai mostrar uma toast dizendo que o botão vai ser vermelho...
         * @False: O botão não tem uma configuração de cor vermelha.
         */
        if (sPreferences.getBoolean("preferences_app_button_red", true)) {
            Toast.makeText(getApplicationContext(), "É vermelho", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "Não é vermelho... :/", Toast.LENGTH_LONG).show();
        }
    }
}

To insert a value into a shared Preferences key, use:

sPreferences.edit().putBoolean("preferences_app_button_red", false).apply();
    
11.03.2015 / 15:06
0

To pass data from one Activity to another, you can attach an bundle object to the Intent object, which will contain the data you will send. Then the new activity will have to extract the bundle data.

Sending bundle

Intent intent = new Intent(ActivityA.this, ActivityB.class);
Bundle b = new Bundle();
b.putString(/*id*/,/*dado*/); //Meramente exemplo
intent.putExtra("Bundle", b);
startActivity(intent);

Receiving and extracting data from the bundle

Bundle b = this.getIntent().getBundle();
String dado = b.getString(/*id*/);

EDIT

To start the initial activity with parameters, you will have to start the activity without parameters first. Put the parameters in a Bundle, and then launch the initial activity with the parameters.

To split the initialization, you can create a control field in the bundle and check the existence of the field. If it exists, you can proceed with initialization, otherwise, make the process of passing parameters.

    
11.03.2015 / 14:41
0

You can make this setting on your Application . Application is the first thing that executes, before any Activity .

Within your Application you can, for example, load / persist information from your SharedPreferences :

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        SharedPreferences reader = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        String algumaString = reader.getString("algumaKey", "");
        ...

    }
}

Do not forget to add your Application to your AndroidManifest.xml :

<application
    android:name=".MyApplication"
    ...
    
11.03.2015 / 15:07