gpt4 book ai didi

android - 如何根据 Activity 切换 Intent 功能

转载 作者:行者123 更新时间:2023-11-30 00:55:05 26 4
gpt4 key购买 nike

我如何根据父 Activity 切换功能我有两种情况我想在功能之间切换。我的功能与短信 otp 验证有关。

案例 1. 当用户正在注册时,功能是验证 otp 并激活用户。

case2:当用户忘记密码时,他们将生成一个新的 otp 来重置密码。

这是我的函数代码

@Override
protected void onHandleIntent(Intent intent) {

// SqLite database handler
db = new SQLiteHandler(getApplicationContext());
// Fetching user details from sqlite
HashMap<String, String> user = db.getUserDetails();


if (intent != null) {
String otp = intent.getStringExtra("otp");
String phone = user.get("phone");
verifyOtp(otp,phone);

}
}

现在我想把它变成这样

    @Override
protected void onHandleIntent(Intent intent) {

// SqLite database handler
db = new SQLiteHandler(getApplicationContext());
// Fetching user details from sqlite
HashMap<String, String> user = db.getUserDetails();


if (intent != null) {
String otp = intent.getStringExtra("otp"); //this coming from sms receiver

//from here i want to switch if parent activity is register activity below function should run


String phone = user.get("phone");
verifyOtp(otp,phone);


// if parent activity if forgot password activity the below function should run

String phone = session.getmobileno();
verifyfpass(otp,phone)

}
}

最佳答案

如果您所说的父 Activity 是指从您启动 Intent 的位置,那么您可以在 Intent 中添加另一个参数以告知从何处发送了 Intent。

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("from", "RegisterActivity");
startActivity(intent);

然后在 handleIntent 函数中,你可以只检查这个变量的值,然后执行你的函数。

if (intent.hasExtra("from")) {
String from = intent.getStringExtra("from");
if (from.equals("RegisterActivity")) {
//verifyotp
} else if (from.equals("ForgotParentActivity")) {
//verifyfpass
}
}

但是,如果您想检查您当前所在的 Activity ,则可以使用 instanceof 属性。

if (this instanceof RegisterActivity) {
//verifyotp
} else if (this instanceof ForgotParentActivity)) {
//verifyfpass
}

关于android - 如何根据 Activity 切换 Intent 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40323712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com