2015-10-11 57 views
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(); 
} 

請讓我知道我做錯了。

謝謝!

回答

0

如果我正確地理解了你,那麼在調用initBT()之後,似乎期待在第一個Activity中執行,直到啓動的Activity返回。這不是發生了什麼事。啓動活動異步發生。這意味着,當您撥打startActivityForResult()時,該任務將排隊,並且您的代碼將繼續運行,直到暫停爲止。在你的情況下,這可能是onCreate()返回後,這意味着initBT(),initNFC()StartApp()方法都將在第一個活動暫停第一個結果時調用。

解決方法是暫緩調用下一個方法,直到先前的返回。也就是說,將呼叫轉移到和StartApp()onActivityResult()的相應塊中。