2016-07-06 145 views
0

我有一個名爲「A」的應用程序,其中包含一個名爲「CloginSession」的類,我在「B」應用程序中有一個名爲「B」的應用程序,我想讓CLoginSession的對象檢查用戶是否登錄或不能。我這樣做,如何在android中的其他應用程序中調用其他應用程序活動?

這裏是CLoginSession代碼: -

public class CLoginSessionManagement { 
// User name (make variable public to access from outside) 
public static final String s_szKEY_MOBILE = "agentcode"; 
// Email address (make variable public to access from outside) 
public static final String s_szKEY_PASSWORD = "pin"; 
// Sharedpref file name 
private static final String s_szPREF_NAME = "LoginData"; 
// All Shared Preferences Keys 
private static final String s_szIS_LOGIN = "IsLoggedIn"; 
private final SharedPreferences m_LOGIN_PREF; 
private final Editor m_EDITOR; 
private final Context m_CONTEXT; 


// Constructor 
@SuppressLint("CommitPrefEdits") 
public CLoginSessionManagement(Context m_CONTEXT) { 
    this.m_CONTEXT = m_CONTEXT; 
    m_LOGIN_PREF = m_CONTEXT.getSharedPreferences(s_szPREF_NAME, Context.MODE_PRIVATE); 
    m_EDITOR = m_LOGIN_PREF.edit(); 
} 


// Registeration Session Management.... 
public void setLoginData(String mobile, String pin) { 
    m_EDITOR.putBoolean(s_szIS_LOGIN, true); 
    m_EDITOR.putString(s_szKEY_MOBILE, mobile); 
    m_EDITOR.putString(s_szKEY_PASSWORD, pin); 
    m_EDITOR.commit(); 
} 

/** 
* checkLogin() session wil check user Login status 
* If false it will redirect user to Login page 
* Else won't do anything 
*/ 
public boolean checkLogin() { 
    if (!isLogin()) { 
     Intent i = new Intent(m_CONTEXT, CMainActivity.class); 
     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     m_CONTEXT.startActivity(i); 
     return true; 
    } 
    return false; 
} 

/** 
* Get stored Login session data 
*/ 
public HashMap<String, String> getLoginDetails() { 
    HashMap<String, String> user = new HashMap<>(); 
    // user name 
    user.put(s_szKEY_MOBILE, m_LOGIN_PREF.getString(s_szKEY_MOBILE, null)); 
    // user email id 
    user.put(s_szKEY_PASSWORD, m_LOGIN_PREF.getString(s_szKEY_PASSWORD, null)); 
    // return user 
    return user; 
} 

public boolean isLogin() { 
    return m_LOGIN_PREF.getBoolean(s_szIS_LOGIN, false); 
} 

/** 
* Clear session details 
*/ 
public void logoutUser() { 
    // Clearing all data from Shared Preferences 
    m_EDITOR.clear(); 
    m_EDITOR.commit(); 
    @SuppressWarnings("UnusedAssignment") final AppCompatActivity activity = (AppCompatActivity) m_CONTEXT; 
    Intent i = new Intent(m_CONTEXT, CLoginScreen.class); 
    // Closing all the Activities 
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    // Add new Flag to start new Activity 
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

    // Staring Login Activity 
    m_CONTEXT.startActivity(i); 
    ((AppCompatActivity) m_CONTEXT).finish(); 

} 

}

+0

是應用程序是在Play商店中重定向? –

+0

是............. – vishwas

回答

0

您可以調用此方法openAnApp(),您需要重定向。

public void openAnApp() 
    { 
     Boolean flag=false; 
     try{ 
      Intent intent = new Intent(Intent.ACTION_SEND); 
      intent.setType("text/plain"); 
      final PackageManager packageManager = getActivity().getPackageManager(); 
      Intent intent1 = new Intent(Intent.ACTION_MAIN, null); 
      intent1.addCategory(Intent.CATEGORY_LAUNCHER); 
      List<ResolveInfo> resInfos = packageManager.queryIntentActivities(intent1, 0); 
      ActivityInfo activity = null; 
      //getting package names and adding them to the hashset 
      for(ResolveInfo resolveInfo : resInfos) { 
      System.out.println("apap="+resolveInfo.activityInfo.packageName); 
      if(resolveInfo.activityInfo.packageName.equals("x.x.x")) //Replace with your package name 
      { 
       flag = true; 
       activity = resolveInfo.activityInfo; 
       break; 
      } 
     } 
      if (flag) { 
      // final ActivityInfo activity = app.activityInfo; 
       final ComponentName name = new ComponentName(activity.applicationInfo.packageName,activity.name); 
       intent = new Intent(Intent.ACTION_MAIN); 
       intent.addCategory(Intent.CATEGORY_LAUNCHER); 
       intent.setComponent(name); 
       getActivity().startActivity(intent); 
       //startActivity(Intent.createChooser(intent , "Send image using..")); 
       } else { 
        Uri uri=Uri.parse("market://details?id=x.x.x&hl=en"); 
       Intent goToMarket=new Intent(Intent.ACTION_VIEW,uri); 
       try{ 
        startActivity(goToMarket); 
       }catch(ActivityNotFoundException e){      
        Toast.makeText(getActivity(),"Couldn't launch the market",Toast.LENGTH_SHORT).show(); 
       } 
       } 
      } catch (Exception e) { 
       Toast.makeText(getActivity(), "Something went wrong", Toast.LENGTH_LONG).show(); 
      e.printStackTrace(); 
     } 
    } 
0

自定義操作添加意圖過濾<intent-filter>第三活動後:

<intent-filter> 
    <action android:name="com.abc.xyz.TAG"/> 
    <category android:name="android.intent.category.DEFAULT" /> 
</intent-filter> 

您可以使用適當的意圖啓動活動:

startActivity(new Intent("com.abc.xyz.TAG")); 
相關問題