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);
}
Related
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.
Refering to this post I need to ask something else.
I have an application "A" which download and install another app "B".
I want B to "transfer" data to A and then A will use this data to do work.
I know that we can transfer data with intent.
After installing B app with A, Android provide a choice with "Ok" or "Launch" ; my question is :
Is that possible to pass data from B to A when we click on "Ok"? (So we stay in A app without launching B)
If yes, how? Is that possible to "invisible" launch B? How should I code B to get this comportement?
I know that might be hard to understand, you can try to check my previous draw (here again).
EDIT:
I use this code to launch B installation from A.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory().toString() + "/downloadedfile.apk"));
Intent.setDataAndType(uri, "application/vnd.android.package-archive");
getApplicationContext().startActivity(intent);
There are many ways to handle this, here is one that (I believe) is quite simple to implement. Since your A app [presumably] knows what it is installing:
App A: Add a BroadcastReceiver to react to the installation, though by default it is off.
Android: BroadcastReceiver on application install / uninstall
App B: Add a Service for background communication.
Note: A Service must be exported to be accessible to other apps via explicit intent, but this creates a security concern, as it is open to all other apps.
When a user of App A clicks to install App B:
Start the BroadcastReceiver with a filter set to detect the install:
stackoverflow...android-broadcastreceiver-on-application-install-uninstall
App A starts the install.
When the BroadcastReceiver detects the package has been added (the package name will be in the received intent,) it can stop the BroadcastReceiver, and can send an explicit Intent naming the Service in AppB. You can pass whatever data you need in the intent.
When the AppB service receives the intent, it can act in any way you'd like.
Service is always created using a non-null Intent, though the 'action' of explicit Intents is null.
Service.onStartCommand() might receive a null Intent if the service was re-created.
I'd fill in more of the code, but I have a day job ;)
Note:
Intent.ACTION_PACKAGE_ADDED called when a package is installed.
Intent.ACTION_PACKAGE_INSTALL was never used, and was deprecated in API 14.
http://developer.android.com/reference/android/content/BroadcastReceiver.html
http://developer.android.com/reference/android/content/Intent.html
I am developing an Answering Machine application.
Basically it has to automatically accept the call, give a voice message and start recording the call.
I have designed an application to automatically answer to calls and another application to record calls. Individually both the apps work fine. But after i merged them the application crashes.
I saw a an example here :Combine 2 android apps. I merged the apps same way but still the application crashes.
How can I properly merge my apps?
Try
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.example.package", "com.example.package.ActivityToStart");
startActivity(intent);
or
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("com.example.package");
startActivity(intent);
to invoke 2 apps. And retrieve files from their default save location.
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.
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.