I am developing an Android application with Delphi XE5, and I would like to know how I can open a URL in the default browser, and a PDF file with the default reader.
Developing for Windows, I used ShellExecute, but for Android and iOS what should I use?
For these kind pf task you can use the Intent class which is represented in Delphi by the JIntent interface.
Try these samples
Open a URL
uses
Androidapi.JNI.GraphicsContentViewText,
FMX.Helpers.Android;
procedure TForm3.Button1Click(Sender: TObject);
var
Intent: JIntent;
begin
Intent := TJIntent.Create;
Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
Intent.setData(StrToJURI('http://www.google.com'));
SharedActivity.startActivity(Intent);
end;
Open a PDF File
uses
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.JavaTypes,
FMX.Helpers.Android;
procedure TForm3.Button1Click(Sender: TObject);
var
Intent: JIntent;
begin
Intent := TJIntent.Create;
Intent.setAction(TJIntent.JavaClass.ACTION_VIEW);
Intent.setDataAndType(StrToJURI('filepath'), StringToJString('application/pdf'));
SharedActivity.startActivity(Intent);
end;
n00b here can't work out how to add a comment to the set of comments already posted against the previous answer, but I use this, which is another variation on the theme, using constructor parameters:
procedure LaunchURL(const URL: string);
var
Intent: JIntent;
begin
Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW,
TJnet_Uri.JavaClass.parse(StringToJString(URL)));
SharedActivity.startActivity(Intent);
end;
Related
Delphi 10.2.2 Firemonkey Android Question: How can I open a URL in Android's web browser from my application?
I tried to understand the concepts here: How can I open a URL in Android's web browser from my application? but the information didn't help me any. I am unable to wrap my brain around how they can get it to work compared to the code I currently have.
I've looked here too: http://docwiki.embarcadero.com/CodeExamples/Tokyo/en/FMX.Android_Intents_Sample
I've also looked here: https://developer.android.com/guide/components/intents-common.html?hl=en
I've tried this code:
function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;
var
Intent: JIntent;
begin
// There may be an issue with the geo: prefix and URLEncode.
// will need to research
Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW,
TJnet_Uri.JavaClass.parse(StringToJString(System.NetEncoding.TNetEncoding.URL.Encode(URL))));
try
TAndroidHelper.Activity.startActivity(Intent);
exit(true);
except
on e: Exception do
begin
if DisplayError then ShowMessage('Error: ' + e.Message);
exit(false);
end;
end;
end;
This is how I use that function:
OpenURL('https://www.patreon.com/phonelosers/overview/', true)
And it keeps giving me an error:
Error: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=https://www.patreon.com/phonelosers/overview/ }
I tried this code too:
function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;
var
Intent: JIntent;
begin
// There may be an issue with the geo: prefix and URLEncode.
// will need to research
Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW,
TJnet_Uri.JavaClass.parse(StringToJString(System.NetEncoding.TNetEncoding.URL.Encode(URL))));
if TAndroidHelper.Activity.getPackageManager.queryIntentActivities(Intent, TJPackageManager.JavaClass.MATCH_DEFAULT_ONLY).size > 0 then
begin
try
TAndroidHelper.Activity.startActivity(Intent);
exit(true);
except
on e: Exception do
begin
if DisplayError then ShowMessage('Error: ' + e.Message);
exit(false);
end;
end;
end
else
begin
ShowMessage('Intent.resolveActivity <= 0');
end;
end;
which gives me "Intent.resolveActivity <= 0"
It doesn't matter which android phone I use. I have a Moto G Play, Samsung S8+, and a Moto Z2 Force which run Android versions 6.0.1, 7.0, 8.0. All the phones have chrome web browser installed and I can use it.
I look all over the web and the code below is what everyone is using to supposedly do what I need it to do. I looked at both Delphi and Android programming information.
Please help solve this Delphi 10.2.2 Firemonkey Android problem!
After using a different coding method:
procedure OpenURL(const URL: string);
var
LIntent: JIntent;
Data: Jnet_Uri;
begin
LIntent := TJIntent.Create;
Data := TJnet_Uri.JavaClass.parse(StringToJString(URL));
LIntent.setData(Data);
LIntent.setAction(StringToJString('android.intent.action.VIEW'));
TAndroidHelper.Activity.startActivity(LIntent);
end;
I figured out I forgot "System.NetEncoding.TNetEncoding.URL.Encode" and just removing that code from the source fixed the issue so that this code:
function OpenURL(const URL: string; const DisplayError: Boolean = False): Boolean;
var
Intent: JIntent;
begin
// There may be an issue with the geo: prefix and URLEncode.
// will need to research
Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW,
TJnet_Uri.JavaClass.parse(StringToJString(URL)));
try
TAndroidHelper.Activity.startActivity(Intent);
exit(true);
except
on e: Exception do
begin
if DisplayError then ShowMessage('Error: ' + e.Message);
exit(false);
end;
end;
end;
now works!
Since this System.NetEncoding.TNetEncoding.URL.Encode has caused the issue, I'm wondering if I do need to encode my url special, what should I use?
use this code :
var
URL: string;
Intent: JIntent;
begin
URL := 'https://www.google.com';
Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW,
TJnet_Uri.JavaClass.parse(StringToJString(URL)));
SharedActivity.startActivity(Intent);
end
I'm using Delphi 10.1 Berlin for developing android mobile application. And in that I need to check if the location sensor is not switched ON, then I need to redirect to the Location sensor Settings in android mobile. How can I able to implement using Delphi? I have seen example using JAVA but not found for Delphi. And Thanks in Advance.
You could try some code like the following. Here is a helper unit:
unit LocationU;
interface
function IsGPSProviderEnabled: Boolean;
function IsNetworkProviderEnabled: Boolean;
procedure LaunchLocationSettings;
implementation
uses
System.SysUtils,
Androidapi.Helpers,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.Location,
Androidapi.JNI.Provider;
function IsProviderEnabled(const Provider: JString): Boolean;
var
LocationManagerObj: JObject;
LocationManager: JLocationManager;
begin
LocationManagerObj := TAndroidHelper.Context.getSystemService(
TJContext.JavaClass.LOCATION_SERVICE);
LocationManager := TJLocationManager.Wrap(LocationManagerObj);
Result := LocationManager.isProviderEnabled(Provider);
end;
function IsGPSProviderEnabled: Boolean;
begin
Result := IsProviderEnabled(TJLocationManager.JavaClass.GPS_PROVIDER);
end;
function IsNetworkProviderEnabled: Boolean;
begin
Result := IsProviderEnabled(TJLocationManager.JavaClass.NETWORK_PROVIDER);
end;
procedure LaunchLocationSettings;
begin
TAndroidHelper.Activity.startActivity(
TJIntent.JavaClass.init(TJSettings.JavaClass.ACTION_LOCATION_SOURCE_SETTINGS));
end;
end.
and here is some code that calls it:
uses
FMX.Helpers.Android,
Androidapi.Helpers,
Androidapi.JNI.Widget,
LocationU;
procedure TMainForm.FormCreate(Sender: TObject);
begin
if not IsGPSProviderEnabled and not IsNetworkProviderEnabled then
begin
CallInUiThread(
procedure
begin
TJToast.JavaClass.makeText(
TAndroidHelper.Context,
StrToJCharSequence('Location services not enabled - launching settings'),
TJToast.JavaClass.LENGTH_SHORT).show
end);
LaunchLocationSettings;
end;
end;
I use Delphi 10 Seattle update1 and I have an android service what I start from host app but I do not know how can I stop the service from the host application. Could anyone tell me, please?
You are starting the service using the
TLocalServiceConnection.StartService() method. Embarcadero does not provide a corresponding TLocalServiceConnection.StopService() method, so you will have to call Android's Context.stopService() method directly.
Here is the source code for TLocalServiceConnection.startService() from $(BDS)\source\rtl\android\System.Android.Service.pas:
class procedure TLocalServiceConnection.StartService(const AServiceName: string);
var
LIntent: JIntent;
LService: string;
begin
LIntent := TJIntent.Create;
LService := AServiceName;
if not LService.StartsWith('com.embarcadero.services.') then
LService := 'com.embarcadero.services.' + LService;
LIntent.setClassName(TAndroidHelper.Context.getPackageName(), TAndroidHelper.StringToJString(LService));
TAndroidHelper.Activity.startService(LIntent);
end;
You can replace TAndroidHelper.Activity.startService() with TAndroidHelper.Activity.stopService():
var
LIntent: JIntent;
begin
LIntent := TJIntent.Create;
LIntent.setClassName(TAndroidHelper.Context.getPackageName(), TAndroidHelper.StringToJString('com.embarcadero.services.LocationService'));
TAndroidHelper.Activity.stopService(LIntent);
end;
I have managed to register a custom protocol handler in my XE5 Android app by modifying the Androidmanifest.template.xml file. My application pops up properly whenever a myapp://myurl URL is clicked on.
The problem is, I need to get the URL that was clicked on when the app is launched in order to bring up the correct part of the app. Can anyone help me figure out how to get this?
Based on this example, try something like this:
uses
...,
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.Net,
FMX.Helpers.Android;
procedure TMainForm.FormCreate(Sender: TObject);
var
intent: JIntent;
uri: Jnet_Uri;
uriStr: String;
begin
intent := SharedActivity.getIntent;
if intent <> nil then
begin
if TJIntent.JavaClass.ACTION_VIEW.equals(intent.getAction) then
begin
uri := intent.getData;
uriStr := JStringToString(uri.toString);
// use uriStr as needed...
end;
end;
end;
How do I access the Calendar and Events on Android using Delphi XE5.
To access the calendar you can use the Calendar class which is represented by the JCalendar class in Delphi.
You can find a set of samples here
And this is a Delphi sample
uses
Androidapi.JNI.GraphicsContentViewText,
FMX.Helpers.Android,
Androidapi.JNI.JavaTypes;
procedure TForm1.Button1Click(Sender: TObject);
var
Intent: JIntent;
Calendar: JCalendar;
begin
Calendar := TJCalendar.JavaClass.getInstance;
Intent := TJIntent.Create;
Intent.setType(StringToJString('vnd.android.cursor.item/event'));
intent.putExtra(StringToJString('beginTime'), Calendar.getTimeInMillis());
intent.putExtra(StringToJString('allDay'), true);
intent.putExtra(StringToJString('rrule'), StringToJString('FREQ=YEARLY'));
intent.putExtra(StringToJString('endTime'), Calendar.getTimeInMillis()+3600*1000);
intent.putExtra(StringToJString('title'), StringToJString('Hello from Delphi'));
SharedActivity.startActivity(Intent);
end;
in XE5 they started with PlatformServices and put Pickers Service into it: http://docwiki.embarcadero.com/Libraries/XE5/en/FMX.Pickers.IFMXPickerService
probably this piece of code will be usable for you:
var
PickerService: IFMXPickerService;
begin
if PlatformServices.Current.SupportsPlatformService(
IFMXPickerService, Interface(PickerService))
then
FDateTimePicker := PickerService.CreateDateTimePicker;
... // or
FListPicker := PickerService.CreateListPicker;