Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android switch小部件不工作_Android_Broadcastreceiver_Uiswitch - Fatal编程技术网

Android switch小部件不工作

Android switch小部件不工作,android,broadcastreceiver,uiswitch,Android,Broadcastreceiver,Uiswitch,我正在创建一个带有开关的呼叫转移功能,该开关可以打开和关闭。然而,尽管我已经准备好了一切,但我的开关小部件根本不工作。我特意实现了一个toast来检查它是否工作,但不幸的是,它甚至没有显示then toast xml文件 <Switch android:id="@+id/switch1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layou

我正在创建一个带有开关的呼叫转移功能,该开关可以打开和关闭。然而,尽管我已经准备好了一切,但我的开关小部件根本不工作。我特意实现了一个toast来检查它是否工作,但不幸的是,它甚至没有显示then 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" />
我实际上遵循了一个使用两个按钮的示例,打开和关闭,然而,在我的代码中,我使用了一个开关。所以问题是,我如何使函数工作。根本没有错误日志。多谢各位

通过阅读此示例并认识到mainactivity.xml使用mainactivity.java解决了这个问题。但是,我使用CallForwarding.java时没有在mainactivity.xml中声明

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

}
}