Android switch between apps - android

I have two apps and I want to switch between the apps after clicking on a button.
I use this code:
PackageManager manager = getPackageManager();
Intent i = manager.getLaunchIntentForPackage("at.mycompapp");
if (i == null) {
return;
}
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
My problem is that the app is started new, but when the app is already running, it should just switch to this app without restarting it.

I am now using custom scheme to get to the right point in my apps.

Related

Android Studio startActivity

I was searching by myself but failed. Can I start an application like Google translator in the background? Here below-listed program runs the Translator very well, but at the same time, my app goes to the background. I would like to have my app foreground while the Translator is started in the background. Many thanks in advance!
Intent app_to_launch = getPackageManager().
getLaunchIntentForPackage("com.google.android.apps.translate");
if (app_to_launch != null) {
startActivity(app_to_launch);
}
Updated:
Following the subject. One thing is observed. Here below-listed code is working fine:
Intent app_to_launch = getPackageManager().getLaunchIntentForPackage("com.google.android.apps.translate");
if (app_to_launch != null) {
startActivity(app_to_launch);
}
try{Thread.sleep(1000);}catch (Exception e){};
Intent intent = new Intent(MainActivity.this, WordsStatus.class);
startActivity(intent);
But if I remove the 1 sec pause (Thread.sleep) there is no sign that the Google translator has been run. It seems like the launching of the second Intend (my application starting) suppresses the first launch (Google translator starting). If I restore the 1-sec pause everything works as it should be.
Sure just do this:
Intent app_to_launch = getPackageManager().getLaunchIntentForPackage("com.package.of.background.app");
if (app_to_launch != null) {
startActivity(app_to_launch);
}
Intent app_to_launch = getPackageManager().getLaunchIntentForPackage("com.package.of.forground.app");
if (app_to_launch != null) {
startActivity(app_to_launch);
}
(The second app launch doesn't need to use the package manager, you can use anyone of starting an Activity, i.e. if you want to start your own app's internal activity, then just use the normal approach startActivity(new Intent(this, MyActivity.class))
That will start two apps, and the second one starting will move the first one second in the stack.
Note that apps don't really "run in the background" any app that isn't showing on the screen is usually in the paused state, (it may have started a background service to do some work though).

Calling startActivity from my app to return to Pokemon Go causes Pokemon Go to restart

I'm wondering if there is another way to switch to a different app? The built-in task switcher doesn't cause Pokemon Go to restart. Is there a way to invoke that?
I have been using this to switch from my app and open Pokemon Go
PackageManager manager = context.getPackageManager();
Intent intent = manager.getLaunchIntentForPackage("com.nianticlabs.pokemongo");
intent.addCategory(Intent.CATEGORY_LAUNCHER);
A few months ago, this started Pokemon Go to open to a black screen. The workaround for this was to close Pokemon go, and start Pokemon Go by switching to it through my app. The first time would start with Pokemon Go's loading screen as expected, but would properly switch between apps after that.
The latest release of Pokemon Go seems to have fixed the black screen problem by always restarting Pokemon Go everytime it is switch to. I found this other intent filter in their AndroidManifest.xml and it works, but it also causes the app to restart.
Uri uri = Uri.parse("http://pokemongolive.com/launchapp");
Intent pokemonGoIntent = new Intent(Intent.ACTION_VIEW, uri);
if (pokemonGoIntent.resolveActivity(getPackageManager()) != null)
startActivity(pokemonGoIntent);
Is another way to switch to another app? Even when my app is loaded and running, the built-in task switcher doesn't cause Pokemon Go to restart.
I finally managed to find a solution, after a whole day of testing.
It works for me:
Intent poGoIntent = activity.getPackageManager().getLaunchIntentForPackage("com.nianticlabs.pokemongo");
poGoIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(poGoIntent);
Let me know if it helps!
After taking a look at some log files, I saw some suspicious messages. I found the corresponding API calls and this seems to be working for me.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName("com.nianticlabs.pokemongo", "com.nianticproject.holoholo.libholoholo.unity.UnityMainActivity"));
intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(intent);

how to open my app by clicking any button in other installed apps

i like to open my own app by clicking any button in another installed app in my phone.
Example: when i click a delivery button in any shopping app then my app will open or my app icon will pop up in bottom.
( The same method used in insta download app for download Instagram picture, when we copy url of a instagram image the insta download app will pop up automatically )
is it possible??? if anyone knows please help me
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("com.example.yourApp");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
}
If you know the data and the action, you simply should add these information to your intent instance before starting it.
I am assuming you have access to the AndroidManifest of the other app, you can see all needed information there LIKE
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.package.address","com.package.address.MainActivity"));
startActivity(intent);

Android Auto - launch third party app from within my app?

Is it possible to launch any third party application from my application on Android Auto
I couldn't find anything mentioned on this anywhere.
Note: Please note "Android Auto" (Car) words here. I am not asking for android mobile application.
Idea of android auto is completely different from what you are trying to do.
Android auto provides a platform where it has done the basic things with a good user interface making sure not to distract user much.
All that you need to do is provide services which this platform can use.
As of now you can provide Music and Messaging services which are compatible with android auto.
You can launch applications code something like
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.example.youpackage", "com.example.LauchActivity");
startActivity(intent);
And if you want get all possible application list for launch.code :
Declare you intent and add value you want pass
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isIntentSafe = activities.size() > 0;
if (isIntentSafe) {
startActivity(mapIntent);
}
And another way to start you specific application
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("com.example.package");
startActivity(intent);

dont allow to Open own application from another app

I have seen some of example where other application can start my application by package name. Due to security reason I want to prevent this kind of access for other application.
I want to prevent this (Open another application from your own (intent)) kind of acess
Edit
For Example, If thirdparty application knows my application's package name they can launch my app from their application like this way,
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("app package name");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
}
Now to prevent this, i have added export = "false" in my launching activity as well as added permission to lauching activity. Now due to this, it is preventing thirdparty app to launch my application but android OS launcher is also not able to launch application.
I imagine if you don't provide the launch intent in your Android manifest, other apps (including your homescreen) won't be able to launch your app.
You can try these attributes in manifest:
http://developer.android.com/guide/topics/manifest/activity-element.html#exported
http://developer.android.com/guide/topics/manifest/service-element.html#exported
http://developer.android.com/guide/topics/manifest/receiver-element.html#exported
http://developer.android.com/guide/topics/manifest/provider-element.html#exported
As also mentioned in those links, you can try another approach by using permission with the protectionLevel = signature
http://developer.android.com/guide/topics/manifest/permission-element.html#plevel
I hope below solution by comparing package name will help you protecting your activity to be launched by other app.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ComponentName componentName = this.getCallingActivity();
if(componentName == null) {
finish();
} else if("<intended package name>".equals(componentName.getPackageName())) {
finish();
} else {
String data = getIntent().getDataString();
...
}
}
}
Above solution is base on below assumption:
Only one app with a particular package name can exist on Google Play.
If a user tries to install an app whose package name already exists on the device, the installation either will fail or will overwrite the previously installed app.

Categories

Resources