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.
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.
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.
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, ... ).
Shared Preferences is often used to save application settings information. But it is not recommended that you save user information (eg: profile system, etc ... )
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();
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.
Intent intent = new Intent(ActivityA.this, ActivityB.class);
Bundle b = new Bundle();
b.putString(/*id*/,/*dado*/); //Meramente exemplo
intent.putExtra("Bundle", b);
startActivity(intent);
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.
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"
...