0
我想讓用戶在啓動應用程序之前啓用NFC和藍牙。 但是,當我使用startActivityForResult和** onActivityResult **時,它仍然保持運行狀態。onActivityResult沒有停止程序運行
下面的代碼:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initBT();
initNFC();
StartApp();
}
private void initBT() {
mBTState = BTModule.GetInstance().initBT(); //trying to get the BT adapter
if(mBTState.equals(eBluetoothStatus.BT_DISABLED)){ //if adapter is not enabled
Intent enableBtIntent = new Intent(BTModule.GetAdapter().ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
private void initNFC(){
mNFCState = NFCModule.initNFC(getApplicationContext());
if(mNFCState == eNFCStatus.NFC_NOT_SUPPORTED){
UIUtils.getInstance().Toast(this, "NFC is not suppoeted for this device");
//Toast.makeText(this, "NFC is not suppoeted for this device", Toast.LENGTH_LONG).show();
}
else if(mNFCState == eNFCStatus.NFC_DISABLED){
Intent nfcIntent;
if(android.os.Build.VERSION.SDK_INT >= 16){
nfcIntent = new Intent(android.provider.Settings.ACTION_NFC_SETTINGS);
}
else{
nfcIntent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
}
startActivityForResult(nfcIntent,REQUEST_ENABLE_NFC);
UIUtils.getInstance().Toast(this, "Please enable NFC");
//Toast.makeText(this, "Please enable NFC", Toast.LENGTH_LONG);
}
else{
Toast.makeText(this, "NFC is up and running", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_ENABLE_BT){
if(resultCode == RESULT_CANCELED)
{
Toast.makeText(this, "This app needs bluetooth", Toast.LENGTH_LONG).show();
finish();
}
else if (resultCode == RESULT_OK) {
Toast.makeText(this, "Bluetooth enabled", Toast.LENGTH_LONG).show();
}
}
else if(requestCode == REQUEST_ENABLE_NFC){
if(resultCode == RESULT_OK){
Toast.makeText(this, "NFC enabled", Toast.LENGTH_LONG).show();
}
else if(resultCode == RESULT_CANCELED)
{
Toast.makeText(this, "This app needs NFC to be enabled", Toast.LENGTH_LONG).show();
finish();
}
}
}
private void StartApp(){
Intent nextIntent;
if(SaveSharedPreference.getUserName(MainActivity.this).length() == 0)
{
nextIntent = new Intent(MainActivity.this, LoginActivity.class);
}
else
{
nextIntent = new Intent(MainActivity.this, MainMenuActivity.class);
}
startActivity(nextIntent);
finish();
}
請讓我知道我做錯了。
謝謝!