2015-11-03 30 views
0

每当我开始/恢复我的android手机应用程序,它会提示我启用我的蓝牙两次,无论我选择第一个提示信息。我的代码有什么问题?如何停止重复请求以启用蓝牙

public class MainActivity extends AppCompatActivity { 

    // BLE management 
    private static BluetoothManager btManager; 
    private static BluetoothAdapter btAdapter; 

    // Set the enable bluetooth code 
    private final static int REQUEST_ENABLE_BT = 0; 


    // String for LogCat documentation 
    private final static String DEBUG_TAG= ""; 

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

     btManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); 
     btAdapter = btManager.getAdapter(); 

     if (btAdapter != null) { 
      if (!btAdapter.isEnabled()) { 
       // Request Bluetooth Adapter to be turned on 
       Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
       startActivityForResult(enableIntent, REQUEST_ENABLE_BT); 
      } 
     } 

     else 
     { 
     Log.i(DEBUG_TAG, "No bluetooth available"); 
     } 



     Button launchReminderButton = (Button) findViewById(R.id.button); 
     launchReminderButton.setOnClickListener(new OnClickListener() { 
                @Override 
                public void onClick(View v) { 
                // Launch Reminder 
                // Used Context's startActivity() method 

      // Create an intent stating which Activity you would like to 
      // start 
      Intent reminder = new Intent(MainActivity.this, Reminder.class); 

      // Launch the Activity using the intent 
      startActivity(reminder); 
     } 
               } 
     ); 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 

     // check for Bluetooth enabled on each resume 
     if(btAdapter != null && !btAdapter.isEnabled()){ 
      Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableIntent, REQUEST_ENABLE_BT); 
     } 
    } 



    @Override 
    public void onPause() { 
     super.onPause(); 


    } 

    @Override 
    public void onStop() { 
     super.onStop(); 

    } 

    @Override 
    public void onRestart() { 
     super.onRestart(); 


    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 

     btAdapter = null; 
    } 

    @Override 
    protected void onActivityResult(int request_enable_bt, int result_enable_bt, Intent data) 
    { 
     super.onActivityResult(request_enable_bt, result_enable_bt, data); 

     if(result_enable_bt == RESULT_OK) { 
       Toast.makeText(this, "Turned On", Toast.LENGTH_SHORT).show(); 
     } 

     else if (result_enable_bt == RESULT_CANCELED) 
     { 
      Toast.makeText(this, "Didn't Turn On", Toast.LENGTH_SHORT).show(); 
      finish(); 
     } 
    } 

谢谢。

回答

1

,请注意下面的代码,关键是finish()你这里使用:

@Override 
protected void onActivityResult(int request_enable_bt, 
     int result_enable_bt, Intent data) 
{ 
    super.onActivityResult(request_enable_bt, result_enable_bt, data); 

    if (result_enable_bt == RESULT_OK) 
    { 
     Toast.makeText(this, "Turned On", Toast.LENGTH_SHORT).show(); 
    } 

    else if (result_enable_bt == RESULT_CANCELED) 
    { 
     Toast.makeText(this, "Didn't Turn On", Toast.LENGTH_SHORT).show(); 
     finish(); 
    } 
} 

当您启动应用程序时,系统会提醒你打开你的蓝牙,因为你的要求的蓝牙在onCreate method.When你deny请求,并且该方法onActivityResult回电,你会遇到下面的代码:

else if (result_enable_bt == RESULT_CANCELED) 
    { 
     Toast.makeText(this, "Didn't Turn On", Toast.LENGTH_SHORT).show(); 
     finish(); 
    } 

finish()将完成您的活动,每当您点击您的应用程序,当您第一次点击deny,然后第二次点击deny。每次获得此设置时:onCreate -> onResume。因此,您得到启用蓝牙两次的请求!

+0

非常感谢! –

+0

没关系!请用复选标记选中我的答案,谢谢! – ifeegoo

+0

解决这类问题很容易,可以一步一步调试。 – ifeegoo