【问题标题】:Android switch widget not workingAndroid 开关小部件不起作用
【发布时间】:2014-02-08 07:31:22
【问题描述】:

我正在创建一个呼叫转接功能,并带有一个可以打开和关闭它的开关。但是,尽管我已经准备好了所有东西,但我的开关小部件根本无法正常工作。我特意实现了一个 toast 来检查它是否正常工作,但不幸的是,它甚至没有显示然后 toast。

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类

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

}
}

我实际上遵循了一个使用 2 个按钮(开和关)的示例,但是,在我的代码中,我使用的是开关。所以问题是,我如何使该功能起作用。根本没有错误日志。谢谢你。

【问题讨论】:

    标签: android broadcastreceiver uiswitch


    【解决方案1】:

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

    通过阅读这个例子解决了,也意识到mainactivity.xml使用了MainActivity.java。但是,我使用 CallForwarding.java 没有在 mainactivity.xml 中声明

    【讨论】:

      猜你喜欢
      • 2015-10-28
      • 1970-01-01
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多