我有一个创建蓝牙连接的复选框。问题是当出现蓝牙权限请求对话框并且我选择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();
}
}
}
});
答案 0 :(得分:2)
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode ==REQUEST_ENABLE_BT) {
if (resultCode == RESULT_CANCELED) {
turnBtOnOff.setSelected(false);
}
}
}
答案 1 :(得分: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;
}
}
}
答案 2 :(得分: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
//
}
}
}
祝你好运
答案 3 :(得分: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
}
}
}
答案 4 :(得分:0)
覆盖onActivityResult()
方法:
@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
}
答案 5 :(得分: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,因为它不是要执行的操作,而是用户定义的活动。