Launching third-party applications from mobile 1C

Alexander Biryukov

02.04.2021 12 min

There is a special command in the 1C programming language that allows the developer to call third-party applications from the 1C program code. This command is called "RunApp". And what is most interesting, this command is also available in the mobile version of 1C.
And of course, there are drawbacks - in the mobile version of 1C, this command can open a file only by association, that is, we send a link to some file, for example, PDF, as a command parameter, and the mobile device decides with which application to open this file. You can read more about this command in the Help, but now let's try to check in practice how it works.

Let's create a new 1C configuration and immediately indicate that it will be used on mobile devices.

Then let's create one Сommon form:

To make this form open when our application starts, let's add it to the home page:

So, we are prepared for further work.


Let's go back to our form and create a new command there and immediately place this command on the form:


We do not pay attention to the appearance of the buttons, our goal now is to understand how to call external applications.

This is what the source code for this command looks like:

&AtClient

Procedure Command1(Command)

     RunApp("mailto:mail@mail.com");


EndProcedure

Now we save our application and publish it on the webserver (or immediately build the finished .apk file). I will not describe how to do this, we talked about it earlier in our blog.

After that, let's launch our application on a device (or in an emulator) and press the "Command1" button. On my device, the result looks like this:


I choose to open with Gmail (there are simply no other mail programs on my device) and this is what happens:


It seems we did it! If you fill in the email subject and send it now, it will reach the addressee!


Let's modify our source code as follows:

&AtClient

Procedure Command1(Command)

     RunApp("mailto:mail@mail.com?subject=Message from us&body=Hello World!");


EndProcedure


and try to run it. Here's what we should get:

I think we're doing pretty well, isn't it?

What other features does the RunApp command have?

The developer can create a call with the desired phone number:

&AtClient

Procedure Command1(Command)

     RunApp("tel: +59999999999");

EndProcedure

or send SMS:

&AtClient

Procedure Command1(Command)

     RunApp("sms: +59999999999");

EndProcedure

or open the file:

&AtClient

Procedure Command1(Command)

     RunApp("file:///sdcard/some_file.txt");

EndProcedure

or here's an interesting example - opening a map at predetermined coordinates:

&AtClient

Procedure Command1(Command)

     RunApp("geo:51.47697994218494, -0.0005109304913892233?z=18");

EndProcedure


I’ll not visualize the result of this last command, leave you the pleasure to look at this object yourself.

Ok, but let's say we want to open a specific application. The "RunApp" command will not help us here, since in the mobile version of the 1C language for this command, only a link to a file can be passed as a parameter, and the operating system of the mobile device decides which application to use to open the file.

How to be? But we have a way out. In 1C there is a special object "MobileDeviceApplicationRun", which is just intended to run third-party applications on mobile devices.

One of the advantages of this object is that you can pass parameters to it for the called application, and you can also receive a response from the application for subsequent processing.

I want to note right away that this object is available only for the mobile version of 1C, so all calls to this object must be framed with special preprocessor instructions "#IF MobileAppClient Then ... #EndIf"

Well, let's add a new command to our form and write the following program code for this command:

&AtClient

Procedure Command2(Command)

     #IF MobileAppClient Then

               newRunApp = New MobileDeviceApplicationRun();

               newRunApp.Action = "android.intent.action.SEND";

               newRunApp.Data = "mail@mail.com";

               newRunApp.Type = "text/plain";

               newRunApp.AdditionalData.Add("android.intent.extra.SUBJECT","Message from us");

               newRunApp.AdditionalData.Add("android.intent.extra.TEXT","Hello World again!");

               newRunApp.Package = "com.google.android.gm";

               newRunApp.Run(True);

     #EndIf

EndProcedure


And let's check this code in action right away! Let's save our 1C application, update the publication on the webserver, open the application on a mobile device and press the "Command2" button. And here's the result:

The application that we needed was immediately opened!

Now let's look at another example of how you can run a third-party application from a mobile 1C. Again, create a new command and write the following program code for it:

&AtClient

Procedure Command3(Command)

     #IF MobileAppClient Then

         newRunApp = New MobileDeviceApplicationRun("android.intent.action.VIEW", 

         "android.intent.action.VIEW", "http://maps.google.com/maps?saddr=" + 

         XMLString(48.858385) + "," + XMLString(2.294499));

     #EndIf

EndProcedure


In this example, we create a MobileDeviceApplicationRun object and, right at the time of creation, we pass into it the coordinates of two objects between which we need to build a route. Note that the code does not explicitly state which application will handle this call.

Let's run this code on 1C and see the result:



As you can see, the system prompts us to choose from several applications, the one that should handle this call. Let's say we want to build our route in Google maps, what needs to be added to the source code for this?

As always, it's all simple, just add one line to our sources. As a result, the code will look like this:

&AtClient

Procedure Command3(Command)

     #IF MobileAppClient Then

            newRunApp = New MobileDeviceApplicationRun("android.intent.action.VIEW", "http://maps.google.com/maps?saddr="; +

            XMLString(51.476979) + "," + XMLString(0) +

            "&daddr=" + XMLString(48.858385) + "," + XMLString(2.294499));

            newRunApp.Package = "com.google.android.apps.maps";

            newRunApp.Run(False);

     #EndIf


EndProcedure


In this case, we indicate a specific application (Package), which should start and process our route.

All this is great, but you may ask - where can a developer get the parameters to launch a third-party application from 1C, something else, for example, Evernote? 

Now I will show you how to do it.

Let's create another command and try to invoke Evernote (it should naturally be installed on your device). But let's not just call this application, but create a new note directly from 1C.

What do we need to know to do this? Firstly, the name of the application itself, as it is registered in the Android system. This is the so-called "Package". Second, the name of the action we want to invoke. This is "Action".

We can find both "Package" and "Action" in the application manifest for Android.

To view the manifest, you can use any application that can show data to applications already installed on your device. For example, I use "ManifestViewer" for this purpose.

This is what the manifest for Evernote looks like. I am showing only part of this file and the parameters of interest to us are highlighted in red.


           


Knowing these two parameters will allow us to launch Evernote and open a new blank note (by the way, you can check this yourself).

But I remind you that we want to create a new note entirely from 1C. This means that we need to pass some additional parameters at the time of creating a note. It's time to turn to the Evernote documentation to understand what parameters need to be passed additionally.

We can find the information we need at this link: https://dev.evernote.com/doc/articles/android_intents.php

We are interested in "com.evernote.action.CREATE_NEW_NOTE", and from the documentation we see that we can additionally transfer the title of a new note (parameter "EXTRA_TITLE") and, of course, the text of the note itself (parameter "EXTRA_TEXT").

Also, note that the "EXTRA_TITLE" parameter should be set to the following value - "android.intent.extra.TITLE". Link: https://developer.android.com/reference/android/content/Intent.html#EXTRA_TITLE

Now that we know all the necessary information, we can start programming. The source code could be like this:

&AtClient

Procedure Command4(Command)

     #IF MobileAppClient Then

               newRunApp = New MobileDeviceApplicationRun();

               newRunApp.Package = "com.evernote";

               newRunApp.Action = "com.evernote.action.CREATE_NEW_NOTE";

               newRunApp.AdditionalData.Add("android.intent.extra.TITLE","Title from 1C");

               newRunApp.AdditionalData.Add("android.intent.extra.TEXT","Hello World from 1C");

               newRunApp.Run(True);

     #EndIf


EndProcedure


And of course, let's immediately check our program in operation! Launch the application, press button 4 and:

As we wanted, a new Evernote card opened with our title and text.

And of course, in the way I described, you can launch any of the applications installed on your device. To do this, you just need to look at the AndroidManifest.xml file of this application.

Well, it's probably time to take stock. We saw how you could interact with the 1C program and any other application installed on your mobile device.

Dumping the configuration(.dt file), which was created in the process of creating this article


Stay tuned, there is still a lot more to come!


Be the first to know tips & tricks on business application development!

A confirmation e-mail has been sent to the e-mail address you provided .

Click the link in the e-mail to confirm and activate the subscription.