1

我有一个创建蓝牙连接的复选框。问题是当蓝牙权限请求对话框出现并且我选择No时,该复选框仍然处于选中状态。

我如何从这个活动中获取请求代码,如果我得到,请关闭复选框RESULT_CANCELED

CheckBox turnBtOnOff=(CheckBox)findViewById(R.id.checkBox1);
turnBtOnOff.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if (isChecked)
            {
                if(!mBluetoothAdapter.isEnabled())
                {
                    Intent enableBtIntent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);  
                    //myAddress=mBluetoothAdapter.getAddress();
                    //Toast.makeText(getBaseContext(), myAddress, Toast.LENGTH_SHORT).show();   
                    }
                }
                else
                {
                    if(mBluetoothAdapter.isEnabled())
                    {
                        mBluetoothAdapter.disable();
                    }
                }
            }
        });
4

6 回答 6

2
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode ==REQUEST_ENABLE_BT) {
        if (resultCode == RESULT_CANCELED) {
            turnBtOnOff.setSelected(false);
        }
    }
}
于 2012-03-27T10:53:29.293 回答
0

Override the onActivityResult() method:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
        //check for the requestcode here and check/uncheck the checkbox 

}
于 2012-03-27T10:46:47.137 回答
0

这有点旧,但我今天遇到了同样的问题,这是我在 android 4.4.2 上工作的解决方案

@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {      
    if(resultCode==RESULT_FIRST_USER){
        //custom request is BLUETOOTH_REQUEST_DISCOVERABLE
                        if(requestCode==BLUETOOTH_REQUEST_DISCOVERABLE){
                            //Do something
                        }
                    }
 }

结果不是 RESULT_OK,因为它不是要执行的操作,而是用户定义的活动。

于 2015-03-19T18:48:02.023 回答
0
 protected void onActivityResult(int requestCode, final int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case REQUEST_ENABLE_BT: {
            if (resultCode == RESULT_CANCELED) {
                ......
            }
            break;
        }

    }
 }   
于 2012-03-27T10:43:43.397 回答
0

尝试这个 :

private static int RESULT_OK=111;
private static int  PICK_CONTACT_REQUEST=112;

protected void onActivityResult(int requestCode, int resultCode,
             Intent data) {
         if (requestCode == PICK_CONTACT_REQUEST) {
             if (resultCode == RESULT_OK) {
                 // ....do your code here
                 // 

             }
         }
     }

祝你好运

于 2012-03-27T10:44:02.910 回答
0

您需要覆盖onActivityResult以从活动中获取结果:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //if 12345 is the int value you used to start bluetooth activity then
    if(requestCode == 12345){
        if(resultCode == RESULT_OK){
            //do whatever you want
        }
        else{
            //do whatever you want
        }
    }
}
于 2012-03-27T10:44:22.173 回答