I declared a String previousState and when the broadcast receiver onReceive is called, 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 part
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 part
if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
Toast.makeText(context, "off hook ", Toast.LENGTH_SHORT).show();
Toast.makeText(context, previousState, Toast.LENGTH_SHORT).show();
if (previousState.equals(ringing)){
Toast.makeText(context, "previous is ringing ", Toast.LENGTH_SHORT).show();}
}
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?
The problem is not about comparing the string, the problem is I want to change the reference of the string, it is not a duplicated question.
Thank you!