如何根据父活动切换函数?我有两种情况,我想在函数之间切换。我的功能与短信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
}
}
然而,如果你想检查你现在所处的活动,那么你可以使用instanceof
属性。
if (this instanceof RegisterActivity) {
//verifyotp
} else if (this instanceof ForgotParentActivity)) {
//verifyfpass
}
在家长活动
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("from", "RegisterActivity");
startActivity(intent);
最终答案主要活动@OverrideonHandleIntent(Intent Intent) {
pref = new SessionManager(getApplicationContext());
// SqLite database handler
db = new SQLiteHandler(getApplicationContext());
// Fetching user details from sqlite
HashMap<String, String> user = db.getUserDetails();
if (intent != null) {
if (intent.hasExtra("from")) {
String from = intent.getStringExtra("from");
if (from.equals("RegisterActivity")) {
String otp = intent.getStringExtra("otp");
String phone = user.get("phone");
verifyOtp(otp, phone);
} else if (from.equals("Forgotpass")) {
String otp = intent.getStringExtra("otp");
String phone = pref.getMobileNumber();
verifyfpass(otp,phone);
}
}
}
}