Calling an app from another app - android

In this app I'm developing I need to load/call another app that is already installed on the phone. It's an application for my own personal use only, so no need to check if the other app is installed - I know it is.
I've googled this problem for hours, but I can't find anything that works. Mostly because the guidelines for finding package name and class name are really bad.
Via cmd and adb I was able to find that the info regarding the application I'd like to call is:
package:/data/app/com.soundcloud.android-1.apk=com.soundcloud.android
(that's exactly what it said in the cmd window.)
I tried something like this:
Intent i = new Intent();
i.setClassName("/data/app/com.soundcloud.android-1.apk", "com.soundcloud.android");
startActivity(i);
But my app just crashes instead. I used the above code because someone said that this could call an app:
Intent i = new Intent();
i.setClassName("<package_name>","<Class Name(with package name)>");
startActivity(i);
Does anyone know what to really write?
P.S.: my own app does not need any information about what's happening in the called app.

Use the PackageManager to get an Intent for the package:
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("com.example.package");
startActivity(intent);

The documentation is
here.
I think in your example, com.soundcloud.android is in fact the package name, so that should be the first argument. For the second one, you still need to figure out the class to use.
If you don't have the code, you can check how to find out the class from the apk with this.

Related

Start OpenVPN through intent from another APP

Hello fellow Stackoverflowers,
a few days ago I found this neat little function to start and connect OpenVPN from another app using intents.
private void startVPN(){
Intent openVPN = new Intent("android.intent.action.VIEW");
openVPN.setPackage("net.openvpn.openvpn");
openVPN.setClassName("net.openvpn.openvpn", "net.openvpn.openvpn.OpenVpnClient");
openVPN.putExtra("net.openvpn.openvpn.AUTOSTART_PROFILE_NAME","10.10.10.10 [10.10.10.10]");
startActivityForResult(openVPN,0);
}
Now my question is:
Do I only need to have OpenVPN (connect or for android) installed or do I need to create something like a .jar libary to use it?
You are calling startActivity, therefore you will need whatever app that includes that net.openvpn.openvpn package and Intent filter.
setClassName is calling into that package for a particular class that will ultimately take some Intent extras.
So no JAR file, yes to needing the app installed. If you don't have the app installed, I think the onActivityResult will come back with other than OK response code.
the intent you created only opens openVpn application and for that the openVpn application should be installed on the device or this code will crash.you can check if the intent will resolved by this code
// Verify that the intent will resolve to an activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(sendIntent);
}

Im looking to start a specific application using intent in android studio

I am looking to start a specific application(Dictionary app) from my app. Using intents, how would I go about launching that specific app and use it to look up the word.
There are implicit intents and explicit intents, you want an explicit intent to get your desirable.
Here is how you can do it.
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example", "com.example.MyExampleActivity"));
startActivity(intent);
in the setComponent method your Dictionary app information should go in.
CommonsWare addition to this answer,
Using an explicit Intent to talk to a third-party app is rarely the right thing to do. This code will break if the activity is not exported, or requires permissions, or the developer refactors the code and changes the class name or package, etc.
If the author of the app is documenting that your recipe is the correct way to work with that activity on that app, then that is fine, as the developer presumably intends to support this use case.

is it possible to call more packages in single project,without installing the other packages in emulator/phone.want single apk

is it possible to call more packages in single project,without installing the other packages in emulator/phone,want single apk file..I Kept this code...but is possible when the package is available in emulator/phone..Please suggest me...if that package is not available in emulator/phone
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName(
"com.abc.def.packname",
"com.abc.def.packname.MyActivity"));
startActivity(intent);
To be able to use an Intent to launch any Activity, there must be an Activity available to Handle it. This applies for both direct intents that target one particular Activity, and more general intents which would result in a Chooser dialog, like the share intent.
So in short, yes, the package you are referencing must be installed as well, or else your app will crash. If you have access to the source code of the other project, you could combine them into one. If this isn't possible, you could request that the user installs the other app when your app starts.

notify installation complete from service

I have this piece of code
private void initiateInstallation() {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File("/sdcard/example.apk"));
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
that from within my service installs an application named example.apk
I want after the installation is finished to run an activity which notifies the user about the installation.I did that except the activity appears before the installation finishes.
The problem is that within a service I cannot use startActivityForResult. So, I need a way around this so that I can start my notification activity(or for the sake of example just print something out with Toast within the service) only AFTER the installation is complete.
I already tried some answers from other questions like "alternative to startActivityforResult in services" but still I couldn't figure this out.
I also put the code so that maybe there may be something done in there.
Thanks in advance ... any suggestions are welcome.
You could listen to the PACKAGE_ADDED broadcast intent: http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_ADDED
As far as I know, these are sent after the installation is done, and you can listen to those from the service.
Just note that if the application was already installed, you will get ACTION_PACKAGE_CHANGED (as far as I know).
Also you must know the package name as well, not just the apk name, since the intent will contain the package name.
The answer given by #Pal Szasz is technically correct (as far as I know ;-) ).
However, based on the information given in your question, I assume you only wish to show a notification (no further programmatically actions are to be performed). If my assumptions are correct I would respectfully advise you NOT to show such a notification. And this is why:
The Android system already has a standard means of passing notifications to the user. The status bar will in this case already show you a message saying that the new app is successfully installed (or not installed in case of an error). If you implement yet another notification channel you will most likely confuse or irritate your users by diverging from the standard, expected behaviour.
Taking this beyond the borders of sanity one could also argue for the fact that you in some sense also would contribute to the fragmentation of Android (in a very small scale, but nevertheless).

Android - Access class in phone.apk

Hi guys this is just my first post,
is it possible to access an activity which is in Phone.apk in a non-rooted phone?
I've tried something like this
Intent i = new Intent(Intent.ACTION_MAIN);
i.setClassName("com.android.phone", "com.android.phone.PhoneApp");
startActivity(i);
but my app always stops unexpectedly.
There is no activity named com.android.phone.PhoneApp in the Android firmware, let alone in the Android SDK.
Moreover, you should not be attempting to start activities in the com.android package, as they are not part of the Android SDK and may or may not exist on any given device or Android version.

Categories

Resources