willemjan92
willemjan92

Reputation: 67

String became empty after it is filled

I'm creating an app with Google Cloud Messaging. After I opened the app my main activity (LoginActivity) registers the app and receives the "GCM Registration Token", I put the following line in RegistrationIntentService.

sharedPreferences.edit().putString("token", token).apply();

The activity gets this token and displays a toast with the token. But after I push a button (btn_login) the token is emptied...
This is the code in LoginActivity

private BroadcastReceiver mRegistrationBroadcastReceiver;
private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
private boolean isReceiverRegistered;
private Context context;
static String token;
Button btn_login;
EditText txt_GebruikersNaam, txt_Wachtwoord;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    context = getApplicationContext();
    btn_login = (Button)findViewById(R.id.btn_login);
    txt_GebruikersNaam= (EditText)findViewById(R.id.txt_GebruikersNaam);
    txt_Wachtwoord= (EditText)findViewById(R.id.txt_Wachtwoord);

    btn_login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String gebruikersnaam = txt_GebruikersNaam.getText().toString();
            String wachtwoord = txt_Wachtwoord.getText().toString();
            if(!gebruikersnaam.equals("") || !wachtwoord.equals("")) {
                // HERE THE TOKEN IS EMPTY 
                String json = createJsonPost(gebruikersnaam, wachtwoord, token);
            }

        }
    });
    token = getToken();
}

 private String getToken(){
    final String[] token = {""};
    mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
            boolean sentToken = sharedPreferences.getBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false);
            if (sentToken) {
                CharSequence text = sharedPreferences.getString("token",null);
                Toast.makeText(context, text.toString(), Toast.LENGTH_SHORT).show();
                token[0] = text.toString();
            } else {
                Toast.makeText(context, "Geen verbinding", Toast.LENGTH_SHORT).show();
            }
        }
    };

    // Registering BroadcastReceiver
    registerReceiver();

    if (checkPlayServices()) {
        // Start IntentService to register this application with GCM.
        Intent intent = new Intent(this, RegistrationIntentService.class);
        startService(intent);
    }
    return token[0];
}

private void registerReceiver(){
    if(!isReceiverRegistered) {
        LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
                new IntentFilter(QuickstartPreferences.REGISTRATION_COMPLETE));
        isReceiverRegistered = true;
    }
}

private String createJsonPost(String Naam, String Wachtwoord, String token){
    JSONObject jsonObject = new JSONObject();
    JSONObject jsonData = new JSONObject();

    jsonData.put("user", Naam);
    jsonData.put("pass", Wachtwoord);
    jsonData.put("token", token);

    jsonObject.put("authorization", jsonData);
}

Upvotes: 0

Views: 57

Answers (2)

Elye
Elye

Reputation: 60081

This is because your token = getToken(); is run before onReceive(Context context, Intent intent) is triggered. You should set your token in the onReceive(Context context, Intent intent) function instead.

Upvotes: 1

Seishin
Seishin

Reputation: 1477

Your "token" String may not be saved yet. Just call .commit() instead of the apply() method of the SharedPreferences.

Upvotes: 0

Related Questions