简体   繁体   English

第二个应用的Android启动活动没有第一个应用的图标

[英]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. 现在,调用intentMainActivityAlias略高于后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.

相关问题 从首次启动android应用程序时的不同活动开始 - Start with different activity on first launch of android app 如何将应用程序的图标与第一个活动的图标分开? - How to separate the icon of the app from the icon of the first activity? Android:应用程序从一个活动跳到第二个活动,而对第一个活动没有任何操作 - Android: App jumping from one activity to second without any action on first activity 按应用程序图标后,在android中启动上次打开的活动 - Start last open Activity in android after pressing App icon 如何从两个活动启动一个 android 应用程序 - How to start an android app from two activity 从 android 手机模块启动应用程序活动 - Start app activity from android phone module 如果已从启动器图标暂停并重新启动,则应用程序将返回到第一活动 - App goes back to first activity if paused and relaunched from launcher icon 启动Second Activity而不是First android - Start Second Activity instead of First android 在Android中向“第二个活动”添加了一个“返回至第一个”按钮。 现在,导航到第二个活动时应用程序崩溃 - Added A Button To Second Activity In Android To Return To First. Now App Crashes When Navigating To Second Activity 从第一个活动到第二个活动的返回按钮使应用程序崩溃 - return button from the first activity to the second crashes the app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM