Android start activity of second app having no icon from first app - android

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:
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. 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. Let's call alias as MainActivityAlias.
From your first application, call the intent on MainActivity. The code for disabling the component will be executed and nothing will open. However, the icon will be gone because this component is disabled and everything related to this component (i.e icon) is gone too.
Now, call the intent on MainActivityAlias just after above intent in the first application. This is just a copy of MainActivity but it does not have any code for disabling and thus, it is enabled and launchable.
Some Side Notes :
1) Both activities should have an <intent-filter> with android.intent.action.MAIN.
2) Your MainActivity should be the launcher activity and thus should have android.intent.category.LAUNCHER in the manifest.
3) Inside MainActivity, you have to check where the call is coming from. 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. 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. When the app is installed, if there is no <intent-filter> with ACTION=MAIN and CATEGORY=LAUNCHER, there will be no app icon shown.
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. 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:
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);

Related

How to hide an app and give it the right to be access from another app?

I hope anyone can help me or give me a clear solution,
I have an app that I don't want to be opened directly from the user, so I need to hide its icon.
and in the other hand, I want to access this application from another app (App2), so I wrote this code in the function of onClick of a certain button of App2:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage(package name of the first app );
startActivity(launchIntent);
Before hiding the App1 , everything was working perfectly No errors but after hiding it using the following code, it crashes:
PackageManager packageManager = this.getPackageManager();
ComponentName componentName = new ComponentName(this,FingerActivity.class);
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
and the following error appear in Android Studio:
Starting: Intent { act=android.intent.action.MAIN
cat=[android.intent.category.LAUNCHER]
cmp=com.neurotec.samples.multibiometric/.fingers.FingerActivity }
Error type 3 Error: Activity class
{com.neurotec.samples.multibiometric/com.neurotec.samples.multibiometric.fingers.FingerActivity}
does not exist.
any idea?
any idea?
You disabled the component. You cannot start the component from any other app.
I have an app that I don't want to be opened directly from the user, so I need to hide its icon.
The simplest way for it to not have an icon is for you to not give it an icon. Change your <intent-filter> for that activity to be something custom for you, rather than having it have the MAIN/LAUNCHER values that causes home screens to put an icon for it in the launcher.

Application button to open current/new instance of another app

I have two applications, A and B. In app A I've added a button which opens app B.
When the button is clicked I want to open App B if it's not already running, otherwise I want to bring the app to the front.
This is the code I use:
Intent intent = getPackageManager()
.getLaunchIntentForPackage(
"com.myapp.something");
if (intent != null) {
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
The code seems to work, but the problem is that if I open app B from app A (using this code) and then click directly the icon of App B, I get two instances of the app, which is undesired.
How can I open the app, as the code does, but also get the same instance even if the app's icon is clicked?
Add Intent.FLAG_ACTIVITY_CLEAR_TOP

Custom Action Intent Opens my App in the same context as the app that I clicked From

I have searched a lot on this.
But, I have my code working to open my app using a custom intent URL. This URL is usually delivered via email.
When I click on the link, it opens my app fine and everything seems to be working; however, it opens in the context of the email application.
For example, if I click on the link from Gmail, when I open multitasking, I have to click on Gmail to return back to the app that just opened.
I would think it should open my app and I can continue using Gmail while my other app is running.
Any thoughts on this?
Make your URL look like this:
intent:#Intent;launchFlags=0x10000000;component=com.mycompany.myapp/com.mycompany.myapp.MyActivity;end
This URL contains the launchFlag for Intent.FLAG_ACTIVIY_NEW_TASK, so this will launch your app in a separate task (outside of the email client or browser or whatever).
EDIT: Add additional details based on OP's comment
You say that you are using a URL like this: http://com.my.app/5058749
In that case you must have used an Intent filter to get Android to open your app by specifying an <intent-filter> on a certain <activity> in your manifest. There are several things you can do to deal with the problem of the launched Activity ending up in the same task as the Activity that launched it:
1) If the Activity is always intended to be the root (starting, first) Activity of a task, you can put the following code in onCreate() after the call to super.onCreate():
if (!isTaskRoot()) {
// Activity was launched into another task. Restart it in its own task
Intent intent = new Intent(this, this.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
return;
}
2) You can set the launch mode of this Activity to singleTask in the manifest by adding
android:launchMode="singleTask"
to the <activity> definition. This will cause the Activity to be launched in its own task but this launch mode has other consequences that are more subtle and so you need to be careful about using it. In general I don't like to suggest this because it tends to create more problems than it solves.
3) You can determine if your app was launched from the browser or email client by examining the Intent used to start it in onCreate() (The Intent will have the data set to the URL when launched via the browser or email client). You can then decide if you want to restart it in its own task by using the code I've supplied in option 1 above.
Add Intent.FLAG_ACTIVITY_NEW_TASK and Intent.FLAG_ACTIVITY_CLEAR_TOP flags(intent.SetFlags() to your intent. Your actitivty will be opened in a new task and this activity will be the root of your new stack.
This is the default behavior with android, but to override it, you need to pass
Intent.FLAG_ACTIVITY_NEW_TASK
Intent.FLAG_ACTIVITY_CLEAR_TOP
with your intent.

Android - Call Main Class Activity

I would like to go back to the Main Activity at a certain point in my application. I tried calling the Activity in an Intent as usual, however since the Activity name for the main class is called "android.intent.action.MAIN", which is the general name of every application's main activity, a "Complete Action Using:" menu pops up with every possible application on the phone as an option. I do not want this; what I want is that the main activity for my application loads up.
How can I achieve this?
Thank you in advance!
You can also specify a specific Activity to launch like so:
Intent nextActivityIntent = new Intent(this, MyActivity.class);
startActivity(nextActivityIntent);
As per the Intent documentation, the ACTION_MAIN flag is used to specify that you want to launch an application using the application's main entry point. You generally do not use it in your app unless you are trying to open another app.

Disable activity from another activity?

I am using the following code to remove my app from the launcher:
if (!dialercode.getText().toString().equals("")) {
getPackageManager().setComponentEnabledSetting(
getComponentName(),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
However, that also stops that activity being launched through other means (secret code).
So, I have setup a separate Launcher activity that will be disabled instead (all the Launcher activity does is launch the main activity).
However, I don't know how to go about disabling the Launcher.java activity via the main activity - IE how do I get the component name for the Launcher activity when I'm in a different activity?
I discovered the solution, instead of using getComponentName() use:
new ComponentName(context, Activity.class) where Activity is the name of the Activity you wish to disable. You can use this in any activity in your application.

Categories

Resources