如何停止重复请求启用蓝牙
How to stop repeated request to enable bluetooth
每当我 start/resume 我的 android phone 应用程序时,它都会提示我启用蓝牙两次,无论我在第一个提示消息中选择什么。我的代码有什么问题?
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();
}
}
谢谢。
注意下面的代码,关键是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()
将完成您的 Activity,每次当您点击您的应用程序时,当您第一次点击 deny
时,并且然后你点击 deny
第二 time.Every 时间你得到这个:onCreate -> onResume
。所以你得到两次启用蓝牙的请求!
每当我 start/resume 我的 android phone 应用程序时,它都会提示我启用蓝牙两次,无论我在第一个提示消息中选择什么。我的代码有什么问题?
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();
}
}
谢谢。
注意下面的代码,关键是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()
将完成您的 Activity,每次当您点击您的应用程序时,当您第一次点击 deny
时,并且然后你点击 deny
第二 time.Every 时间你得到这个:onCreate -> onResume
。所以你得到两次启用蓝牙的请求!