【问题标题】:OnActivityResult for 2 different activites2 个不同活动的 OnActivityResult
【发布时间】:2015-09-21 11:08:22
【问题描述】:

我正在尝试在同一活动中初始化/打开 NFC 和 BT 模块。 在继续任务之前,我需要同时启用它们。

我知道 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;
        }

    }

}

由于我是新手,如果我有任何错误,请随时纠正我:)

谢谢!!

【问题讨论】:

    标签: android android-intent android-activity onactivityresult


    【解决方案1】:

    initNFC 方法中使用 startActivityForResult 而不是 startActivity。还要定义 2 个布尔变量,例如 isNfcEnabledisBtEnabled。在 onActivityResult 方法中检查这些布尔值。如果两者都启用,请随心所欲。

    【讨论】:

      猜你喜欢
      • 2013-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-04
      相关资源
      最近更新 更多