一个按钮中的两个功能单击

时间:2014-01-10 10:43:33

标签: android

我在android中启动了一点程序,我在菜单按钮中有2项。我只需点击第一个菜单项就调用两个函数enableBluetooth()和findDevice()。我想尽快调用findDevice()函数enableBluetooth()函数完成,同样点击。这可能吗?

这是我的代码

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {

    case R.id.retrieve:
        try {
            if (!mBluetoothAdapter.isEnabled()) {
                 enableBluetooth();

                 findDevice();

            }
} catch (Exception e) {

            Toast.makeText(getApplicationContext(),
                    "cTrcker: Unable to retrive data. Try      again",
                    Toast.LENGTH_LONG).show();

        }
        return true;

2 个答案:

答案 0 :(得分:1)

通过使用此

,您可以发送意图以启用blutooth
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    
if (!mBluetoothAdapter.isEnabled()) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
 }

在此之后,onActivity结果是此intent的回调方法。然后在OnActivityResult中,如果启用“您可以调用FindDevice()”方法,或者如果不是,则可以检查蓝牙可用性启用后,您可以向用户表示尚未启用蓝牙或您想要的内容。

答案 1 :(得分:0)

为什么不将findDevice()放在enableBluetooth()函数中?

您可以设置全局boolean常量并在enableBluetooth()函数中检查此常量,如果boolean已设置为true,则意味着您的应用已启用蓝牙,继续直接呼叫findDevice()