[英]Android start activity of second app having no icon from first app
I am having two apps. 我有两个应用程序。 First app has an activity from which I want to launch an activity from the second app. 第一个应用程序有一个活动,我想从第二个应用程序中启动一个活动。 I am using the following code: 我正在使用以下代码:
Intent launchIntent = m_context.getPackageManager().getLaunchIntentForPackage(m_packageName);
if (launchIntent != null) {
m_context.startActivity(launchIntent);
}
This code is working very fine to launch the activity from the second app but I want to have the second application without any icon. 这段代码可以很好地从第二个应用程序启动活动,但是我希望第二个应用程序没有任何图标。 I am using following code in MainActivity
of the second application to remove icon: 我在第二个应用程序的MainActivity
中使用以下代码来删除图标:
PackageManager p = getPackageManager();
//Removing app icon
ComponentName componentName = new ComponentName(this, com.tools.html2pdf.MainActivity.class);
p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
This code successfully removes the launcher icon but then activity from my first application is unable to launch the activity from second app. 此代码成功删除了启动器图标,但是我第一个应用程序中的活动无法从第二个应用程序中启动活动。
Can any one help me in this regard? 在这方面有人可以帮助我吗? I want to launch activity of an app having no icon from activity of another application. 我想从另一个应用程序的活动中启动一个没有图标的应用程序的活动。
When you disable the component
like you have done, that component
can't be launched in any way. 当像完成操作一样禁用该component
,该component
将无法以任何方式启动。 However, interesting thing is that other components (non-disabled activities) of your second application are still launchable. 但是,有趣的是,第二个应用程序中的其他组件(非禁用活动)仍然可以启动。
So, you can create an alias of your MainActivity
in the second application which will be used for your purpose. 因此,您可以在第二个应用程序中创建MainActivity
的别名,该别名将用于您的目的。 Let's call alias as MainActivityAlias
. 让我们将别名称为MainActivityAlias
。
From your first application, call the intent
on MainActivity
. 在第一个应用程序中,调用MainActivity
上的intent
。 The code for disabling the component
will be executed and nothing will open. 禁用component
的代码将被执行,并且不会打开任何内容。 However, the icon will be gone because this component
is disabled and everything related to this component
(ie icon) is gone too. 但是,图标将消失,因为该component
已禁用,并且与此component
相关的所有component
(即图标)也消失了。
Now, call the intent
on MainActivityAlias
just after above intent
in the first application. 现在,调用intent
上MainActivityAlias
略高于后intent
在第一应用。 This is just a copy of MainActivity
but it does not have any code for disabling and thus, it is enabled and launchable. 这只是MainActivity
的副本,但没有任何禁用代码,因此可以启用和启动。
Some Side Notes : 一些注意事项:
1) Both activities should have an <intent-filter>
with android.intent.action.MAIN
. 1)这两个活动都应具有android.intent.action.MAIN
的<intent-filter>
。
2) Your MainActivity
should be the launcher activity
and thus should have android.intent.category.LAUNCHER
in the manifest
. 2)您的MainActivity
应该是启动器activity
,因此manifest
应该有android.intent.category.LAUNCHER
。
3) Inside MainActivity
, you have to check where the call is coming from. 3)在MainActivity
内部,您必须检查呼叫的来源。 If the call is from the first application, then execute the code to disable icon which you mentioned in the question. 如果调用来自第一个应用程序,则执行代码以禁用您在问题中提到的图标。 If the call is coming from launcher icon, then open MainActivityAlias
using intent. 如果呼叫来自启动器图标,则使用意图打开MainActivityAlias
。 You can know where the call is coming from like this . 您可以像这样知道呼叫来自何处。
Note - This is just an idea. 注意-这只是一个想法。 I have not tested it. 我还没有测试。
If you don't want the second app to have an app icon, just remove the <intent-filter>
with ACTION=MAIN and CATEGORY=LAUNCHER for the root Activity
in the second app. 如果您不希望第二个应用程序具有应用程序图标,只需删除第二个应用程序中根Activity
的<intent-filter>
ACTION = MAIN和CATEGORY = LAUNCHER。 When the app is installed, if there is no <intent-filter>
with ACTION=MAIN and CATEGORY=LAUNCHER, there will be no app icon shown. 安装应用程序后,如果没有<intent-filter>
且ACTION = MAIN和CATEGORY = LAUNCHER,则不会显示任何应用程序图标。
Your app can still launch the second app, but not with the method you've described, since Android doesn't know which is the "launch" Activity
. 您的应用仍可以启动第二个应用,但不能使用您描述的方法启动,因为Android不知道哪个是“启动” Activity
。 Assuming you know the package and class name of the Activity
you want to launch in the second app, you can launch it like this: 假设您知道要在第二个应用程序中启动的Activity
的包和类名,则可以这样启动它:
Intent launchIntent = new Intent();
launchIntent.setClassName("second.package.name", "fully.qualified.class.name.of.MainActivity");
// add and Intent flags if necessary here
launchIntent.addFlags(Intent.FLAG_ACTIVITY_...);
startActivity(launchIntent);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.