2016-10-29 54 views
1

如何切換功能取決於父活動我有兩種情況下,我想要在function.my函數之間切換與短信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) 

    } 
} 

回答

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 
} 
+0

感謝代碼 –

0

在父活動

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

最終的答案主要活動 @覆蓋 保護無效onHandleIntent(意向意圖){

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


      } 


     } 
    }