Sending Android Intents from Delphi (Part 1)

As you probably know, I work for the italian embarcadero representative (www.bittime.it), so last week, I’ve been in Milan (Italy) with the Delphi Product Manager Marco Cantù, to show the new Delphi XE5 for Android (and iOS…).

Users (old and new) were enthusiastics. The Delphi-WAY combined with the Android openness and flexibility, is really a good way to develop mobile apps for the business customers.

So, after the initial demos some users were interested in sending Android Intents from their Delphi app.

I’ve builded some demos about Intents, so I showed those demos to them (in the next weeks there will be many Delphi for Android training in Italy, so I’ve build those demo in advance)

In Delphi XE5 you can call java classes from the Google SDK. So I started investigating hot to do this.

There are a lot of possibilities, and I’m still doing research, but this is a simple Intent send demos.

The Intents demo app
The Intents demo app

 

This is the code under the first button.

procedure THeaderFooterForm.Button1Click(Sender: TObject);
var
  Intent: JIntent;
begin
  // JAVA SDK ANDROID HELP SAYS...
  // String url = "http://www.example.com";
  // Intent i = new Intent(Intent.ACTION_VIEW);
  // i.setData(Uri.parse(url));
  // startActivity(i);

  Intent := TJIntent.Create;
  Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
  Intent.setData(TJnet_Uri.JavaClass.parse
    (StringToJString('http://www.danieleteti.it')));
  MainActivity.startActivity(Intent);
end;

Simple, isn’t it?

With this piece of code, you cann open your device browser to a web site (my blog in the snippet).

Click on the first button and you will be redirected to this blog.

Open browser with intent
Open browser with intent

 

Intents are REALLY powerfull

With the next snippet, you can send piece of data (e.g. text) to other apps. You dont have to know the apps in advance, Android will do the match with the Implicit Intent. (It is similat to the TShowShareSheet action).

procedure THeaderFooterForm.Button2Click(Sender: TObject);
var
  Intent: JIntent;
begin
  // Intent intent = new Intent(Intent.ACTION_SEND);
  // intent.setType("text/plain");
  // intent.putExtra(android.content.Intent.EXTRA_TEXT, "Android Rocks!!!");
  // startActivity(intent);

  Intent := TJIntent.Create;
  Intent.setType(StringToJString('text/plain'));
  Intent.setAction(TJIntent.JavaClass.ACTION_SEND);
  Intent.putExtra(TJIntent.JavaClass.EXTRA_TEXT,
    StringToJString('Delphi Rocks!!!'));
  MainActivity.startActivity(Intent);
end;
Chose wich apps shoud be launched to handle the intent (and the text)
Chose wich apps shoud be launched to handle the intent (and the text)

 

Now, if you choose Twitter, you will get this screen.

Intent handle by twitter app
Intent handle by twitter app

 

I’ve demos about placing a call, sending an SMS, show a PDF file, accessing different kind of storage etc.
In the next part of this mini series I’ll show some other snippets.

There is still many areas to explore…

  • Services
  • BroadcastReceiver
  • Widget
  • Bluetooth
  • I/O
  • …?

I’ll keep you informed…

7 thoughts on “Sending Android Intents from Delphi (Part 1)

  1. Ok, added a column with the paoilptuon size, taken from here:It’s a small sample size, but with this dataset, Delphi seems to be very popular in Slovenia. 29 out of every million inhabitants has visited. Only 2 out of every 100 million Chinese managed to surpass the great firewall. Country Visitors Population Per millionSlovenia (SI) 59200313629,45Norway (NO) 614676305 13,04Sweden (SE) 103907405511,35New Zealand (NZ) 424252277 9,88Netherlands (NL) 16316783092 9,71Denmark (DK) 465515575 8,34Singapore (SG) 344701069 7,23Germany (DE) 58782282988 7,13Finland (FI) 355255068 6,66Australia (AU) 134215157546,23Austria (AT) 478214160 5,72Belgium (BE) 5710423493 5,47Bulgaria (BG) 347148785 4,76Canada (CA) 14933759742 4,41Switzerland (CH) 317623438 4,07United Kingdom (GB) 191623484473,06Czech Republic (CZ) 30102017072,94Greece (GR) 30107499432,79France (FR) 154647683892,38Italy (IT) 116580906812,00United States (US)5793102328631,87Spain (ES) 83465059631,78Poland (PL) 65384636891,69Ukraine (UA) 42454155960,92South Africa (ZA) 35491091070,71Russian Federation (RU) 92 921393902050,66Brazil (BR) 1232011033300,61China (CN) 3113301412950,02

  2. Hi Roger,Thanks again for your input, I really aarpecipte it. I tried mosquitto (downloaded the XP binary and used the Delphi MQTT library to write a publisher and subscriber) and got it working within an hour or so. I think I’ll need some more time with both to make a good choice; one of the issues I need to get clear for myself is broker vs. broker-less messaging system; I guess I can do without a broker, but I’m sure there will also be arguments for having a broker I’ll start a post soon in which I will discuss this your comments are welcome!(regarding the subscriptions: you’re right, I don’t see your email in the subscriptions; strange. Maybe you used the mobile interface? That’s the only thing I can think of to cause this)

  3. Hi Roger,Thanks again for your input, I really aarpecipte it. I tried mosquitto (downloaded the XP binary and used the Delphi MQTT library to write a publisher and subscriber) and got it working within an hour or so. I think I’ll need some more time with both to make a good choice; one of the issues I need to get clear for myself is broker vs. broker-less messaging system; I guess I can do without a broker, but I’m sure there will also be arguments for having a broker I’ll start a post soon in which I will discuss this your comments are welcome!(regarding the subscriptions: you’re right, I don’t see your email in the subscriptions; strange. Maybe you used the mobile interface? That’s the only thing I can think of to cause this)

  4. ciao Daniele,
    credi sia possibile con xe5 (android mobile) rilevare dei tag NFC ?
    In Java è piuttosto facile ma per Delphi non trovo documentazione in giro!
    Il tuo blog ha acceso un po di luce visto che parli della possibilità di richiamare classi java dell’SDK di google da Delphi…… per caso hai qualche code snippet da mostrarmi?
    Mi basterebbe rilevare la presenza del tag e leggere il contenuto formattato NDEF
    grazie anticipatamente e complimenti

  5. ciao Daniele,
    credi sia possibile con xe5 (android mobile) rilevare dei tag NFC ?
    In Java è piuttosto facile ma per Delphi non trovo documentazione in giro!
    Il tuo blog ha acceso un po di luce visto che parli della possibilità di richiamare classi java dell’SDK di google da Delphi…… per caso hai qualche code snippet da mostrarmi?
    Mi basterebbe rilevare la presenza del tag e leggere il contenuto formattato NDEF
    grazie anticipatamente e complimenti

  6. Could you please post the code to open a PDF file. Im trying to do this:

    path := ‘/mnt/sdcard/temp/’;
    filePath := TPath.Combine(path, ‘recibo.pdf’) ;
    Intent := TJIntent.Create;
    Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
    Intent.setDataAndType(StrToJURI(filePath), StringToJString(‘application/pdf’));
    SharedActivity.startActivity(Intent);

    I set the permisions to write and read the external memory, and also double check if the file exists, but it gave me the message: “PDF File could not be openend”.

    Regards

  7. Could you please post the code to open a PDF file. Im trying to do this:

    path := ‘/mnt/sdcard/temp/’;
    filePath := TPath.Combine(path, ‘recibo.pdf’) ;
    Intent := TJIntent.Create;
    Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
    Intent.setDataAndType(StrToJURI(filePath), StringToJString(‘application/pdf’));
    SharedActivity.startActivity(Intent);

    I set the permisions to write and read the external memory, and also double check if the file exists, but it gave me the message: “PDF File could not be openend”.

    Regards

Comments are closed.