0
I would like to know how to pass arguments for running an android app, or even create a configuration file to be consulted when launching the app.
0
I would like to know how to pass arguments for running an android app, or even create a configuration file to be consulted when launching the app.
1
You can do it with SharedPreferences.
With this, you can create a preferences screen or settings for your application. You will be able to load the preference at any point of application execution.
SharedPreferences, allows data persistence in the key-value
, that is, for each key or identifier(string) you will have to associate a value to this (Boolean, float, whatever, ...).
The SharedPreferences is widely used to store information from application settings. But it is not recommended that you store user information (e. g: 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 keyPreferences, use:
sPreferences.edit().putBoolean("preferences_app_button_red", false).apply();
0
To pass data of a Activity to another, you can attach to the Intent an object Bundle, which will contain the data you send. Then the new Activity will have to extract the data from the 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);
Bundle b = this.getIntent().getBundle();
String dado = b.getString(/*id*/);
EDIT
To start the Activity initial with parameters, you will need to start the Activity without parameters first. Put the parameters in a Bundle, and then launch the Activity initial with the parameters.
To split the boot, 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 do the process of passing parameters.
0
You can make this setting in your Application
. To Application
is the first thing it performs, 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", "");
...
}
}
Don’t forget to add your Application
in his AndroidManifest.xml
:
<application
android:name=".MyApplication"
...
Browser other questions tagged android parameters
You are not signed in. Login or sign up in order to post.