我对Flash有疑问,如何请求用户启用蓝牙,等待它然后继续启动主要活动(如果用户允许),或者如果用户拒绝则退出应用程序,我在我的应用程序中有以下代码< / p>
// Library from Android
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.Toast;
// User defined library
import java.io.IOException;
import eu.atriaparis.support.ATRIAExceptions.BluetoothDeviceException;
import eu.atriaparis.support.R;
import eu.atriaparis.ledplayfinal.MainControllerActivity;
// Starting Splash Activity for loading other background process. before main.
// Edited Harsha S, for ATRIA Paris - 2015
public class SplashActivity extends Activity {
private static String TAG = SplashActivity.class.getName();
private static long SLEEP_TIME = 3; //Max Loading Time
private final static int REQUEST_ENABLE_BT = 22;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); // Removes title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // Removes notification bar
setContentView(R.layout.activity_splash);
// Start timer and launch main activity
IntentLauncher launcher = new IntentLauncher();
launcher.start();
// Wait for the launcher thread to complete.
}
// @Override
// public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Log.d(TAG,"onActivityResult");
// switch (requestCode) {
// case REQUEST_ENABLE_BT:
// if (resultCode == Activity.RESULT_CANCELED) {
// // User did not enable Bluetooth or an error occurred
// finish();
// }
// break;
// default:
// super.onActivityResult(requestCode, resultCode, data);
// break;
// }
// }
// Internal Class which loads all the other background activity and then starts main thread.
// ATRIA Paris - 2015
private class IntentLauncher extends Thread {
private ProgressBar progressBar;
private int progressStatus = 0;
@Override
/**
* Sleep for some time and than start new activity.
*/
public void run() {
// Progress
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
// Network
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
// For Error Handling
BluetoothDeviceException exception;
try {
// Status 1, Check device and bluetooth status
Thread.sleep(SLEEP_TIME*1000/3);
progressStatus = 20;
progressBar.setProgress(progressStatus);
// Status 2, Check Network Status
if (mBluetoothAdapter == null) {
Toast.makeText(getBaseContext(), getResources().getString(R.string.BluetoothAdapterNotFound), Toast.LENGTH_SHORT).show();
Thread.sleep(SLEEP_TIME*1000/3);
throw new BluetoothDeviceException("Bluetooth Module Not Found");
}
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
Thread.sleep(SLEEP_TIME*1000/3);
Log.d("Splash", "Bluetooth Status + "+ mBluetoothAdapter.isEnabled());
}
progressStatus = 50;
progressBar.setProgress(progressStatus);
// Status 3
Thread.sleep(SLEEP_TIME*1000/3);
progressStatus = 75;
progressBar.setProgress(progressStatus);
// Final Activity
// Load background activities
Thread.sleep(SLEEP_TIME*1000/3);
progressStatus = 100;
progressBar.setProgress(progressStatus);
// Start main activity
Intent intent = new Intent(SplashActivity.this, MainControllerActivity.class);
SplashActivity.this.startActivity(intent);
SplashActivity.this.finish();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
}
}