【已解决】Android中运行startActivityForResult后但是onActivityResult不执行

【问题】

折腾:

【记录】编写Android中的蓝牙模块驱动和底层HART设备

期间,代码:

public class MainActivity extends FragmentActivity implements
  ActionBar.TabListener {

.......
......

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  testBluetooth();
 
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
。。。。。。
 }


 private final int REQUEST_ENABLE_BT = 1;
 private void testBluetooth() {
  BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  if (mBluetoothAdapter == null) {
      // Device does not support Bluetooth
   Toast.makeText(getApplicationContext(), "Device does not support Bluetooth", Toast.LENGTH_LONG).show();
  }
  else{
   //android.bluetooth.BluetoothAdapter@211120b0
   Toast.makeText(getApplicationContext(), "Device support Bluetooth", Toast.LENGTH_SHORT).show();
   
   if (!mBluetoothAdapter.isEnabled()) {
       Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
       startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
       
       Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    // If there are paired devices
    if (pairedDevices.size() > 0) {
     //ArrayAdapter mArrayAdapter = new ArrayAdapter();
     
        // Loop through paired devices
        for (BluetoothDevice device : pairedDevices) {
            //mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
         Toast.makeText(getApplicationContext(), device.getName() + "\n" + device.getAddress(), Toast.LENGTH_SHORT).show();
        }
    }
   }
  }
 
 }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == REQUEST_ENABLE_BT)
        {
         if(resultCode == RESULT_OK){
          Toast.makeText(getApplicationContext(), "Enabled Bluetooth now", Toast.LENGTH_LONG).show();
         }
         else{
          Toast.makeText(getApplicationContext(), "Not enable Bluetooth !", Toast.LENGTH_LONG).show();
         }
        }
    }

但是onActivityResult不执行。

【解决过程】

1.参考:

总结:调用startActivityForResult,onActivityResult无响应的问题_雨点点_新浪博客

去试试。没太看懂。

2.看了一堆,包括:

activity – Android – startActivityForResult immediately triggering onActivityResult – Stack Overflow

区分Activity的四种加载模式 | Marshal’s Blog

都说是加载模式的问题。

但是去看了看,此处自己的AndroidManifest.xml中的activity中根本没有android:launchMode的配置。

估计默认就是正常的standard的。

3.参考:

总结:调用startActivityForResult,onActivityResult无响应的问题_雨点点_新浪博客

中也确保了第二个参数是大于0的。

4.把测试代码testBluetooth从当前的onCreate的内部的前部移到后面了。还是不行。

5.后来的后来,确定了原因了:

当前只是把代码放在另外某个,本身就有问题的activity中,所以有问题。

换到一个正常的activity后,就可以正常显示了。

后来关于蓝牙这部分的代码为:

public class ScanActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
。。。。。。
  testBluetooth();
 }
 private final int REQUEST_ENABLE_BT = 1;
 private BluetoothAdapter mBluetoothAdapter;
 private void testBluetooth() {
  mBluetoothAdapter= BluetoothAdapter.getDefaultAdapter();
  if (mBluetoothAdapter == null) {
      // Device does not support Bluetooth
   Toast.makeText(getApplicationContext(), "Device does not support Bluetooth", Toast.LENGTH_LONG).show();
  }
  else{
   //android.bluetooth.BluetoothAdapter@211120b0
   Toast.makeText(getApplicationContext(), "Device support Bluetooth", Toast.LENGTH_SHORT).show();
   
   if (!mBluetoothAdapter.isEnabled()) {
       Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
       startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
   }
   else{
    scanBtDevices();
   }
  }
 }
 private void scanBtDevices(){
     Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); //[BC:85:1F:96:99:C9, 00:06:66:4C:75:FE]
  // If there are paired devices
  if (pairedDevices.size() > 0) {
   //ArrayAdapter mArrayAdapter = new ArrayAdapter();
   
      // Loop through paired devices
      for (BluetoothDevice btDev : pairedDevices) {
          //mArrayAdapter.add(btDev.getName() + "\n" + btDev.getAddress());
       Toast.makeText(getApplicationContext(), btDev.getName() + "\n" + btDev.getAddress(), Toast.LENGTH_LONG).show();
      }
  }
 }

仅供参考。

 

【总结】

此处,确保当前的activity正常,然后onActivityResult就可以正常被执行到了。



One Thought on “【已解决】Android中运行startActivityForResult后但是onActivityResult不执行

  1. 还有一种情况不执行:
    在Fragment中startActivityForResult(),由其是开启了一个Dialog用的是getActivity().startActivityForResult()

    Fragment的OnActivityResult不会执行,只是粗心造成的

发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量