I need to use a third-party application in my project (view 3d models). Is it possible to somehow open the application I need when I click a button in my application? Maybe as a program to run its executable file?
- No - no wonder the system is arranged as a set of weakly interacting sandboxes. If an application (any component of it) “wants” to be launched from the outside, it is clearly about this “speaks” in its manifest - what intents it accepts. And at the Android level there is no such thing as an “executable file”. - AseN
|
2 answers
You don’t start the user’s application on your own, this should be done by you yourself, but you can implement an application selection dialog that can process your request using an implicit intent. Thus, you can make a request to launch a third-party application. But it is important that the application you want to launch explicitly notifies Android, through its manifesto, that it can be launched from outside.
- And how can I check if there is a notification in his manifest? I don’t have the code for this application ... - Aleksey Timoshchenko
- oneYou can use the queryIntentActivities () method. It will return to you a list of activities that can process your request. - ZigZag
|
In the end, I did it like this
public void reeee(View view) { String path = "/storage/emulated/0/Android/data/com.example.android.camera2basic.demo/files/default/AvatarModelDir/Anna.dae"; File file = new File(path); //checking if the File exists if(file.exists()) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/octet-stream"); boolean result = isIntentAvailable(getApplicationContext(), intent); if (result){ startActivity(intent); } } } public static boolean isIntentAvailable(Context context, Intent intent) { List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); return !list.isEmpty(); } Specifying the path to the file to open and the correct MIME type (in my case, it is application/octet-stream ), everything works for me
- In the release version, the cycle from the
isIntentAvailable()method seems to be best eliminated, since for the user he does not carry anything but useless resources for him. - pavlofff - @pavlofff do you mean
startActivity()right away? And what if there is not a single application that can open this file? So I have a check ... - Aleksey Timoshchenko - Not the check itself, but the
forloop itself in it, which does nothing but list the contents of the list to the log, in the user version this is superfluous, in my opinion - pavlofff - @pavlofff aa you about it, yes I agree. This I just did for myself - Aleksey Timoshchenko
|