我正在尝试在相同的活动中初始化/打开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;
}
}
}
因为我是新手,如果我错了,请随时纠正我:)
谢谢!
答案 0 :(得分:0)
在 initNFC 方法中使用 startActivityForResult 代替 startActivity 。还要定义2个布尔变量,如 isNfcEnabled 和 isBtEnabled 。在 onActivityResult 方法中检查这些布尔值。如果两者都启用,请执行任何操作。