简体   繁体   English

Android开关小部件不起作用

[英]Android switch widget not working

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 xml文件

<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 Java类

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. 我实际上遵循了一个使用2个按钮打开和关闭的示例,但是,在我的代码中,我改用了开关。 So the question is, how do I make the function work. 所以问题是,如何使该功能起作用。 There is no error logs at all. 根本没有错误日志。 Thank you. 谢谢。

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

Solved by reading this example, and also realising that the mainactivity.xml uses MainActivity.java. 通过阅读此示例,并认识到mainactivity.xml使用MainActivity.java来解决。 However, I used CallForwarding.java without declaring in mainactivity.xml 但是,我使用CallForwarding.java而不在mainactivity.xml中声明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM