如何在android中的其他应用程序中调用其他应用程序活动?
问题描述:
我有一个名为“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();
}
}
答
您将需要使用内容提供商,这样你就可以通过应用程序之间的数据
检查此链接的实现 - https://developer.android.com/guide/topics/providers/content-provider-basics.html
答
您可以调用此方法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();
}
}
答
自定义操作添加意图过滤<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"));
是应用程序是在Play商店中重定向? –
是............. – vishwas