如何切换意图功能取决于活动

时间:2016-10-29 21:11:35

标签: android

我如何切换功能取决于父活动我有两种情况,我想在function.my函数与sms 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)

  }
}

2 个答案:

答案 0 :(得分:1)

如果您通过父活动表示启动意图的位置,那么您可以在意图中添加另一个参数,以告知意图的发送位置。

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
}

答案 1 :(得分:0)

在父活动中

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


            }


        }
    }

最终答案主要活动      @覆盖         protected void onHandleIntent(Intent intent){

{{1}}