Activity.finish()在库中调用,但活动永远不会关闭

时间:2014-10-31 14:25:34

标签: android android-intent android-activity bluetooth

我正在使用此库https://github.com/akexorcist/Android-BluetoothSPPLibrary来解决Android 4.2.x上有关蓝牙的所有问题。哪个效果很好。但是,我使用内置的DeviceList活动来选择要连接的设备。

选择设备时,库会调用:

    // The on-click listener for all devices in the ListViews
    private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
        public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
            // Cancel discovery because it's costly and we're about to connect
            if(mBtAdapter.isDiscovering())
                mBtAdapter.cancelDiscovery();

            String strNoFound = getIntent().getStringExtra("no_devices_found");
            if(strNoFound == null) 
                strNoFound = "No devices found";
            if(!((TextView) v).getText().toString().equals(strNoFound)) {
                // Get the device MAC address, which is the last 17 chars in the View
                String info = ((TextView) v).getText().toString();
                String address = info.substring(info.length() - 17);

                // Create the result Intent and include the MAC address
                Intent intent = new Intent();
                intent.putExtra(BluetoothState.EXTRA_DEVICE_ADDRESS, address);

                // Set result and finish this Activity
                setResult(Activity.RESULT_OK, intent);
                DeviceList.this.finish();   
            }                 
        }
    };

然后返回到我创建意图的应用程序活动。这一切都很可爱!但是,DeviceList活动永远不会关闭,因此我将其留在最前端,这并不能帮助我显示我创建的活动的数据。

我已经稍微调整了代码,添加了onBackPressed,这又将finish()活动,但你必须按两次并快速连续,这似乎很奇怪,正如你所看到的,它没有任何异常:

    public void onBackPressed(){
        finish();
    }

我已经尝试了几乎所有通过SO搜索找到的答案,包括用完全限定的类名替换.finish(参见上面的@ DeviceList.this.finish()) - 我也知道调用完成只是真的要求活动可以关闭,操作系统将从那里接管,但它有点怀疑...

为了完整起见,这里是创建意图的活动的onActivityResult:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){     
        if(requestCode == BluetoothState.REQUEST_CONNECT_DEVICE){
            if(resultCode == Activity.RESULT_OK){
                bt.connect(data);
                bt.setOnDataReceivedListener(new OnDataReceivedListener(){
                    public void onDataReceived(byte[] data, String message){
                        try {
                            Log.i("SC", "Incoming data=" + IOUtils.toString(data));
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }); 
            }
        }else if(requestCode == BluetoothState.REQUEST_ENABLE_BT){
            if(resultCode == Activity.RESULT_OK){
                bt.setupService();
                bt.startService(BluetoothState.DEVICE_OTHER);
            }else{
                // clearly pressed back or no devices selected
            }
        }
    }

首先开始活动的意图:

            Intent intent = new Intent(getApplicationContext(), DeviceList.class);
            startActivityForResult(intent, BluetoothState.REQUEST_CONNECT_DEVICE);

编辑请求:

 protected void onDestroy() {
    super.onDestroy();
    // Make sure we're not doing discovery anymore
    if (mBtAdapter != null) {
        mBtAdapter.cancelDiscovery();
    }

    // Unregister broadcast listeners
    this.unregisterReceiver(mReceiver);
    this.finish();
}

非常感谢任何帮助。感谢。

0 个答案:

没有答案