0

我正嘗試在同一活動中初始化/打開NFC和BT模塊。 我需要它們兩個才能繼續執行任務。2個不同活動的OnActivityResult

我明白,OnResultActivity是異步,所以我想弄清楚什麼是實現它的最好方法?

這裏的大部分代碼:

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     initBT(); 
     initNFC(); 

     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(); 
    } 


    private void initBT() { 
     if(BTModule.GetInstance().initBT().equals(Constants.eBluetoothStatus.BT_DISABLED)){ 
      Intent enableBtIntent = new Intent(BTModule.GetAdapter().ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
     } 
    } 

    private void initNFC(){ 

     mNFCState = NFCModule.initNFC(this); 
     if(mNFCState == eNFCStatus.NFC_NOT_SUPPORTED){ 
      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); 
      } 
      startActivity(nfcIntent); 
      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(resultCode == eBluetoothStatus.BT_OK.ordinal()){ 
      Toast.makeText(this, "BT is up and running", Toast.LENGTH_LONG).show(); 
      return; 
     } 

    } 

} 

由於我是新手,請隨時糾正我,如果我錯了,任何事情:)

謝謝!

回答

0

使用startActivityForResult,而不是startActivityinitNFC方法。還定義了2個布爾變量,如isNfcEnabledisBtEnabled。在onActivityResult方法檢查那些布爾值。如果兩者都啓用,請執行任何你想要的操作。

相關問題