dimanche 29 novembre 2015

if statement executed, object value unchanged [duplicate]

This question already has an answer here:

I declared a String and I have an if-statement when the broadcast receiver been called, in the if-statement, I change the reference of the String, but after the if statement executed, the reference to String changed back to the original value.

I tried to search from internet but perhaps the keyword I used is incorrect I cannot find any suitable info. I appreciate if anyone can explain it to me or show me the link for the info

Code are shown as followed

public class IncomingCall extends BroadcastReceiver {
String **previousState** = "idle";
String **ringing** = "ringing";
String **offhook** = "offhook";

@Override
public void onReceive(Context context, Intent intent) {

    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

//the first case
    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
        Toast.makeText(context, "ringing", Toast.LENGTH_SHORT).show();
        previousState = ringing;
        Toast.makeText(context, previousState, Toast.LENGTH_SHORT).show();
    }
//the second case
 if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
        Toast.makeText(context, "off hook ", Toast.LENGTH_SHORT).show();
        if (previousState == ringing){
            Toast.makeText(context, "previous is ringing ", Toast.LENGTH_SHORT).show();
                previousState = offhook;}
        if (previousState == idle){
            Toast.makeText(context, "previous is idle ", Toast.LENGTH_SHORT).show();
                previousState = offhook;}
        }

As you can see,when someone called in, the phone state change from idle to ringing then changed to offhook if user picked up the call, so after first part is executed, the second part should be executed.

After first part is executed the previousState should be refer to ringing, but it still shown as "idle", can anyone explain why it happened?

Thank you!

Aucun commentaire:

Enregistrer un commentaire