Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.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 在库中调用了Activity.finish(),但Activity从未关闭_Android_Android Intent_Android Activity_Bluetooth - Fatal编程技术网

Android 在库中调用了Activity.finish(),但Activity从未关闭

Android 在库中调用了Activity.finish(),但Activity从未关闭,android,android-intent,android-activity,bluetooth,Android,Android Intent,Android Activity,Bluetooth,我使用这个库来解决Android 4.2.x上蓝牙的所有问题。这项工作非常出色。但是,我正在使用内置的DeviceList活动来选择要连接的设备 选择设备后,库将调用以下命令: // The on-click listener for all devices in the ListViews private OnItemClickListener mDeviceClickListener = new OnItemClickListener() { public v

我使用这个库来解决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();   
            }                 
        }
    };
我已经尝试了通过SO搜索找到的几乎所有答案,包括用完全限定的类名替换.finish,请参见上面的@DeviceList.this.finish-我也知道调用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();
}

非常感谢您的帮助。谢谢。

是否可以查看devicelist活动的onPause和onDestroy方法?像您这样覆盖onBackPressed是没有用的。onBackPressed的默认实现就是这样的:它调用finish。@DavidWasser-Yup,我知道。我只是想看看我能做些什么来结束活动。这只是为了我试过的-所以看起来我不是另一个-这不起作用。。。我自己也没做过什么,这是一个典型的人@jvrodrigues-没有onPause,但是我已经编辑了onDestroy。谢谢:在你的onDestroyMethod中没有必要有finish,除了我认为它没有错之外。您是否可能正在创建多个DeviceList活动?
 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();
}