如何在android中的其他应用程序中调用其他应用程序活动?

时间:2016-07-06 06:46:22

标签: android

我有一个名为" A"其中包含一个名为" CloginSession"我有一个名为" B" in" B" app我想制作CLoginSession的对象并检查用户是否登录。我可以这样做吗

这里是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();

}

}

3 个答案:

答案 0 :(得分:0)

您需要使用内容提供商,以便在应用之间传递数据

点击此链接进行实施 - https://developer.android.com/guide/topics/providers/content-provider-basics.html

答案 1 :(得分: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();
        }
    }

答案 2 :(得分: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"));