Xamarin.Android: starting a new intent - xamarin.android

I was targeting android 4.4 and was starting a service like this
start.Click += delegate {
StartService(new Intent("com.xamarin.LocationService"));
start.Enabled = false;
stop.Enabled = true;
};
and it all worked fine. Now I am targeting 6.0 and find from this thread that this isn't secure, and I should do this:
Intent serviceIntent = new Intent(context,MyService.class);
context.startService(serviceIntent);
but I cannot work out what the arguments for 'new Intent()' should be. The class name is 'LocationActivity' but if I do this
serviceIntent = new Intent(this, typeof(LocationActivity));
context.startService(serviceIntent);
it compiles OK but the service doesn't actually start.
The marked answer in that thread also suggests this
Intent bi = new Intent("com.android.vending.billing.InAppBillingService.BIND");
bi.setPackage("com.android.vending");
but if I try that I find that 'Intent does not contain a definition for setPackage'.
So, can someone help me with a solution here? Thanks in advance.

If you want to start an activity you need to use StartActivity and not StartService
Then use this but change from StartService to StartActivity.
From
serviceIntent = new Intent(this, typeof(LocationActivity));
context.startService(serviceIntent);
To
serviceIntent = new Intent(this, typeof(LocationActivity));
context.StartActivity(serviceIntent);

Related

Enabling Single Sign On in TEdgeBrowser

I have some C# code that shows how to enable Single Sign On in WebView2.
The TEdgeBrowser doesn't expose any of the properties that the C# code uses. In particular the interface defined in the Winapi.WebView2 unit for ICoreWebView2EnvironmentOptions doesn't have the functions for getting or setting AllowSingleSignOnUsingOSPrimaryAccount defined in it. I believe that this is because it was created from WebView2.tlb on 07/05/2020 whereas the property was added in the version released September 10, 2020.
What options do I have? Do I need to create my own version of WebView2 from the latest tlb and then duplicate the code in the Vcl.Edge unit to get a component with the SSO option enabled?
I don't need a visual component - I'd be happy to create the browser in code.
The C# code is:
private async void Form1_Load(object sender, EventArgs e)
{
var browser = new WebView2();
var options = new CoreWebView2EnvironmentOptions();
options.AllowSingleSignOnUsingOSPrimaryAccount = true;
var environment = await CoreWebView2Environment.CreateAsync(options: options).ConfigureAwait(false);
await browser.EnsureCoreWebView2Async(environment).ConfigureAwait(false);
Invoke((MethodInvoker)(() =>
{
browser.Dock = DockStyle.Fill;
this.Controls.Add(browser);
browser.Source = new Uri(https://example.com);
}));
}
Try WebView4Delphi instead. WebView4Delphi is fully updated to the latest WebView2 version and it supports all the WebView2 interfaces.
You only have to add this line before the GlobalWebView2Loader.StartWebView2 call :
GlobalWebView2Loader.AllowSingleSignOnUsingOSPrimaryAccount := True;
The demos use the initialization section of the main unit to create GlobalWebView2Loader and set the properties. If you use the SimpleBrowser demo as a template for your application then you would have to add the previous line here.
That property in GlobalWebView2Loader is used for all the browsers that share the same ICoreWebView2Environment which is the default behavior.
In case you need to create a browser with an independent ICoreWebView2Environment then you have to set this property before the TWVBrowserBase.CreateBrowser call :
MyWVBrowser.AllowSingleSignOnUsingOSPrimaryAccount := True;
MyWVBrowser would be an instance of TWVBrowser.

Periodic Work Request running immediately - Xamarin

I am trying to create a notification to be triggered on a schedule. I want the notification to show once a day around a set time (doesn't have to be exact). So what I'm trying to do is create a PeriodicWorkRequest that runs every 24 hours, and delay the start of the work request to the time of day I want it to run. I can get it running on a schedule, but whenever I create it, it just runs right away. I found some questions saying you can use .setInitialDelay() on the builder, however this doesn't seem to be available in the Xamarin Work Manager API. I have also tried .SetPeriodStartTime() on the Builder class after finding the method in the builder class definition, but this doesn't seem to be affecting anything.
MessagingCenter.Subscribe<StartNotificationRemindersMessage>(this, "StartNotificationRemindersMessage", message => {
TimeSpan startDelay = DateTime.Now.AddMinutes(2) - DateTime.Now;
PeriodicWorkRequest notificationsWorkRequest = PeriodicWorkRequest.Builder.From<NotificationsWorker>(1, TimeUnit.Days).SetPeriodStartTime(startDelay).Build();
WorkManager.Instance.Enqueue(notificationsWorkRequest);
});
Here I'm creating the periodic work request and trying to add a 2 minute delay to the start.
And here is the NotificationsWorker class.
class NotificationsWorker : Worker
{
public NotificationsWorker(Context context, WorkerParameters workerParameters) : base(context, workerParameters)
{
}
public override Result DoWork()
{
CrossLocalNotifications.Current.Show("GCS Reminder", "Testing reminder notifications");
return Result.InvokeSuccess();
}
}
If someone can show me what I'm doing wrong, it would be much appreciated.
Thanks!
There is an extension method named SetPeriodStartTime. It's extremely hard to find any documentation on it, but I've been experimenting with it and it appears to do the trick.
E.g.
PeriodicWorkRequest workRequest = PeriodicWorkRequest.Builder
.From<YourWorkerClass>(repeatInterval, flexInterval)
.SetPeriodStartTime(TimeSpan.FromHours(1))
.Build();
setInitialDelay() is now available on the builder in the Xamarin Work Manager API for Xamarin Android
I use setInitialDelay() to make sure PeriodicWorkRequest is delayed for a while you get a WorkRequest so Just cast it to a PeriodicWorkRequest
PeriodicWorkRequest notificationsWorkRequest = (PeriodicWorkRequest)PeriodicWorkRequest.Builder.From<YourWorkerClass>(repeatInterval,repeatIntervalTimeUnit).SetInitialDelay(repeatInterval,repeatIntervalTimeUnit).Build();
WorkManager.GetInstance(Android.App.Application.Context).Enqueue(notificationsWorkRequest );
Then to use it in Xamarin.Forms I use a dependency service, messaging center, or I just define it in the Android Main activity class depending on my reason for using work manager
example would be
PeriodicWorkRequest notificationsWorkRequest = (PeriodicWorkRequest) PeriodicWorkRequest.Builder.From<NotificationsWorker>(24, Java.Util.Concurrent.TimeUnit.Hours).SetInitialDelay(24, Java.Util.Concurrent.TimeUnit.Hours).Build();

How to Communiccate between JD Edwards Enterprise one 9.0 and .Net

I have problem that how to communicate Jd Edwards Enterprise one 9.0 with .Net,and my project manager told me to look at the "fatclient" as it acts as a middle ware between these two,but there is no luck in my search,
Thanks in advance
Responding to this old post. Hopefully, this will help someone who has a similar need:
Take a look at our product LynX Business Integrator. It is Oracle Validated and it allows you to create integration processes natively in C# and publish it as a web service. So, you can write code like this:
private bool CallAddressBookBsfn(BusinessDocument businessDocument, Transaction transaction)
{
AddressBookMaster abm = businessDocument.document.input.AddressBook;
// create an instance of the Address Book Master Business function
// note the use of JDE Transactions
AddressBookMasterMBF bsfn = new AddressBookMasterMBF(transaction);
// set parameters - most of this code is auto-generated
bsfn.DpmnAddressBookNumber.InValue = (long)abm.AddressNumber;
bsfn.DpszSearchType.InValue = abm.AddressType;
bsfn.DpszAlphaName.InValue = abm.Name;
bsfn.DpszAddressLine1.InValue = abm.AddressLine1;
bsfn.DpszAddressLine2.InValue = abm.AddressLine2;
bsfn.DpszAddressLine3.InValue = abm.AddressLine3;
bsfn.DpszAddressLine4.InValue = abm.AddressLine4;
bsfn.DpszPostalCode.InValue = abm.ZipCodePostal;
bsfn.DpszCity.InValue = abm.City;
bsfn.DpszState.InValue = abm.State;
bsfn.DpszCountry.InValue = abm.Country;
bsfn.DpcActionCode.InValue = 'A';
bsfn.DpcUpdateMasterFile.InValue = '1';
// execute the business function
if (bsfn.Execute() != BusinessFunctionResult.Success)
{
// get errors
return false;
}
// assign output
businessDocument.document.output.AddressNumber = bsfn.DpmnAddressBookNumber.OutValue;
businessDocument.document.output.AddressNumberSpecified = true;
return true;
}
Take a look at our YouTube Channel at http://www.youtube.com/user/aelliuslynx and our product page at http://www.aellius.com/products/lynx-business-integrator
I am using old version 8.0 and if by "communication" you mean to be able from a .NET application to run JDE BSFN directly, then i am gonna dissapoint you but there isn't any way i know of.
Maybe things have changed in 9.0 but i doubt it.
Personally whenever i want to communicate with our JDE (AS400 based) i am using:
Frontend
-.NET Web Api services
-C# winforms apps
-ASP.NET
Backend
-Custom Dlls for Business logic and Data Access Layers.

Is it possible to use the HttpClient class in MonoDroid?

I'm trying to use the HttpClient class on a MonoDroid project but it looks like the System.Net.http namespace it's not valid.
I try to add a reference in the project to System.Net.http.dll but it doesn't seem to be available in the references list.
Any idea?
Thks
HttpClient is a .NET 4.5 class, which is not available yet in Mono for Android. Mono itself added supported for it in version 3.0, but Mono for Android is still based on Mono 2.10. I know Xamarin is working on updating Mono for Android (and MonoTouch) to Mono 3.0 now, but as far as I know there's no release date set yet.
I know it is an old thread but I just saw that Xamarin has finally given System.Net.Http in Xamarin.Android 4.8, so thought to share it with you too.
Thanks.
You can't use HttpClient (yet!), but you can still use the System.Net.HttpWebRequest object, which does actually do what HttpClient can provide convenient wrappers for (especially when hitting up a Web API controller).
Here's a sample from a current project I'm working on (it's using the monodroid port of NewtonSoft.Json, not the standard System.Runtime.Serialization.Json) :
private void AddARecord()
{
var cartesian = new Cartesian()
{
Description = "next item blah",
X = 5,
Y = 10,
Z = 15,
};
string json = JsonConvert.SerializeObject(cartesian);
var request = new HttpWebRequest(new Uri(_url)) {ContentType = "application/json", Method = "POST"};
var sw = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
sw.Write(json);
sw.Close();
request.BeginGetResponse(ProcessJsonResponseForSingleResult, request);
}
...the Web API controller I'm hitting does something arbitrary, saves the object I just sent, and then tweaks the description so I know it works. Then it sends the tweaked object back...
And then the callback ProcessJsonResponseForSingleResult looks like
private void ProcessJsonResponseForSingleResult(IAsyncResult ar)
{
var request = (HttpWebRequest)ar.AsyncState;
var response = request.EndGetResponse(ar);
using (var outputStream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.ASCII))
{
var jsonString = outputStream.ReadToEnd();
Log.Info("PJRFSR", string.Format("JSON string: {0} - deserialising...", jsonString));
var cartesian = JsonConvert.DeserializeObject<Cartesian>(jsonString);
RunOnUiThread(() => UpdateUiTextView(cartesian.Description));
}
}
Yeah, I know, it uses the BeginAsync/EndAsync pattern which I don't like any more either, but it does work if you just need to get something done.

Is there a relatively easy way for a hybrid .Net exe (console app/Windows service) to install and start itself as a service?

Suppose an executable named ConsoleOrService.exe is written in C#. It currently is a hybrid. I can just start it on the command line, or I can install it using the .Net's installutil ConsoleOrService.exe and then start the service. I would like a third option: running it on the command line like so: ConsoleOrService.exe --install and have it do all of the work.
Is this possible?
Is this hard?
How can I get started?
Thank you, and let me know if there are questions please.
It's actually quite simple. I've used it in many of my own services (in fact, ALL of my services are capable of doing their own install/uninstall. I control it with a command-line switch, such as /install or /uninstall.
The installation is performed like this:
private static void InstallService()
{
var ti = new System.Configuration.Install.TransactedInstaller();
var si = new MyServiceInstaller();
var cl = new string[] { string.Format(CultureInfo.InvariantCulture, "/assemblypath={0}", System.Reflection.Assembly.GetExecutingAssembly().Location) };
var ctx = new System.Configuration.Install.InstallContext(null, cl);
ti.Installers.Add(si);
ti.Context = ctx;
ti.Install(new Hashtable());
}
The uninstallation is the same, except that you call ti.Uninstall(null); instead of ti.Install(...);.
My MyServiceInstaller is a class that inherits from the System.Configuration.Install.Installer class (as you would normally have in a service).

Resources