Question

I am creating a call forwarding feature with a switch that turns it on and off. However, though I have everything in place, my switch widget is not working at all. I have purposely implemented a toast to check if it is working but unfortunately, it is not even displaying then toast.

xml file

<Switch
    android:id="@+id/switch1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/redirect_call"
    android:layout_alignRight="@+id/view1"
    android:text="@string/call_forwarding_switch" />

java class

public class CallForwarding extends Activity implements OnCheckedChangeListener {

Switch switch1;

@Override
protected void onCreate(Bundle savedInstanceState) 
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  switch1 = (Switch) findViewById(R.id.switch1);

  if (switch1 != null) {
      switch1.setOnCheckedChangeListener(this);    
  }


 // switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      switch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
            if (buttonView.isChecked()){
                //
                Toast.makeText(CallForwarding.this, "Call Forwarding is    activated",Toast.LENGTH_LONG).show();
                callforward("*21*91231231#"); // 0123456789 is the number you want to forward the calls.; 

                }
            else{
                Toast.makeText(CallForwarding.this, "Call Forwarding is deactivated",Toast.LENGTH_LONG).show();
                callforward("#21#");

            }
        }
 });

 }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

 private void callforward(String callForwardString)
    {
        PhoneCallListener phoneListener = new PhoneCallListener();
        TelephonyManager telephonyManager = (TelephonyManager)
         this.getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);

        Intent intentCallForward = new Intent(Intent.ACTION_CALL);
        Uri mmiCode = Uri.fromParts("tel", callForwardString, "#");
        intentCallForward.setData(mmiCode);
        startActivity(intentCallForward);
    }



 private class PhoneCallListener extends PhoneStateListener 
 {
        private boolean isPhoneCalling = false;        

        @Override
        public void onCallStateChanged(int state, String incomingNumber) 
        {
            if (TelephonyManager.CALL_STATE_RINGING == state)
            {
                // phone ringing
            }

            if (TelephonyManager.CALL_STATE_OFFHOOK == state) 
            {
                // active
                isPhoneCalling = true;
            }

            if (TelephonyManager.CALL_STATE_IDLE == state) 
            {
                // run when class initial and phone call ended, need detect flag
                // from CALL_STATE_OFFHOOK
                if (isPhoneCalling)
                {
                    Intent i = getBaseContext().getPackageManager()
                            .getLaunchIntentForPackage(getBaseContext().getPackageName());
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                    isPhoneCalling = false;
                }
            }
        }
 }



@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
    // TODO Auto-generated method stub

}
}

I actually followed an example that uses 2 buttons, on and off, however, in my code, I am using a switch instead. So the question is, how do I make the function work. There is no error logs at all. Thank you.

Was it helpful?

Solution

http://www.panayiotisgeorgiou.net/android-switch-button-example/

Solved by reading this example, and also realising that the mainactivity.xml uses MainActivity.java. However, I used CallForwarding.java without declaring in mainactivity.xml

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top