Android切换小部件无效

时间:2014-02-08 07:31:22

标签: android broadcastreceiver uiswitch

我正在创建一个带有开关的呼叫转移功能,可以打开和关闭它。但是,尽管我已经准备好了所有内容,但我的交换机小部件根本无法正常工作。我故意实施一个吐司来检查它是否有效,但不幸的是,它甚至没有显示吐司。

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

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个按钮打开和关闭的示例,但是,在我的代码中,我使用的是开关。所以问题是,我该如何使功能发挥作用。根本没有错误日志。谢谢。

1 个答案:

答案 0 :(得分:0)

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

通过阅读此示例解决,并且还意识到mainactivity.xml使用MainActivity.java。但是,我使用了CallForwarding.java而未在mainactivity.xml中声明