I am using ZXing nuget for scan QR code in my Xamarin Android application. I have added loader after scanned. I am calling api based on scanned data. But api takes time. I had written code for display loader it's not displaying properly on android 8 or android 9. But it's not rotating in android 7. What's wrong with my code?
My code is :
serverProgressDialog = new ProgressDialog(this);
serverProgressDialog.SetProgressStyle(ProgressDialogStyle.Spinner);
serverProgressDialog.SetMessage("Contacting server. Please wait...");
serverProgressDialog.Indeterminate = true;
serverProgressDialog.SetCancelable(false);
serverProgressDialog.Create();
serverProgressDialog.ShowEvent += ServerProgressDialog_ShowEvent;
For show :
internal void ShowServerProgressDialog()
{
serverProgressDialog.Show();
}
On ScanButton Click code :
imageBtn.Click += async delegate
{
scanner.AutoFocus();
var result = await scanner.Scan(options);
scanner.Cancel();
if (result != null)
{
SetTextboxValue(result.Text);
new Handler().PostDelayed(new Runnable(() =>
ShowServerProgressDialog()), 10);
}
else
{
HideServerProgressDialog();
}
};
On event show I am displaying dialog. Some how it's not rotating on android device os version 7.
Related
I am not getting all devices when I am trying to scan Bluetooth devices with my application. It does not show android and windows device list. I have attached screenshots to understand my problem.
Here is my code.
_centralManager = new CBCentralManager(DispatchQueue.CurrentQueue);
_centralManager.DiscoveredPeripheral += _centralManager_DiscoveredPeripheral;
_centralManager.UpdatedState += (object sender, EventArgs e) =>
{
var manager = sender as CBCentralManager;
if (manager.State == CBCentralManagerState.PoweredOn)
_centralManager.ScanForPeripherals(new CBUUID[0]);
};
Scan event:
public void _centralManager_DiscoveredPeripheral(object sender, CBDiscoveredPeripheralEventArgs e)
{
var device = e.Peripheral;
var rssi = e.RSSI;
var ads = e.AdvertisementData;
}
Note: In my app, I was show device which name is not equal to null or blank.
I had implemented GATTA server on an Android device and it worked.
First, run the app on an Android device, start the server and done.
https://github.com/androidthings/sample-bluetooth-le-gattserver/blob/master/java/app/src/main/java/com/example/androidthings/gattserver/GattServerActivity.java
In my case, I wanted to do this for android things. So I implemented the code in android things.
I am using VS 2017 to create a cross platform (UWP, Android, iOS) Xamarin app. I am trying to show all images from a folder on device as thumbnails (similar to gallery app, sample screenshot attached).
I have looked into WrapLayout sample code provided on Xamarin website (Link), but it's loading all images from internet using JSON
protected override async void OnAppearing()
{
base.OnAppearing();
var images = await GetImageListAsync();
foreach (var photo in images.Photos)
{
var image = new Image
{
Source = ImageSource.FromUri(new Uri(photo + string.Format("?width={0}&height={0}&mode=max", Device.OnPlatform(240, 240, 120))))
};
wrapLayout.Children.Add(image);
}
}
async Task<ImageList> GetImageListAsync()
{
var requestUri = "https://docs.xamarin.com/demo/stock.json";
using (var client = new HttpClient())
{
var result = await client.GetStringAsync(requestUri);
return JsonConvert.DeserializeObject<ImageList>(result);
}
}
I have also looked into Xamarin Media Plugin (Link), but it shows only one image at a time. Sample code -
await CrossMedia.Current.Initialize();
var file = await CrossMedia.Current.PickPhotoAsync();
if (file == null)
return;
MainImage.Source = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
file.Dispose();
return stream;
});
But I am unable to find a way to implement these two (or any other methods) in such a way that I can create my own gallery section in my app.
You need to create an Activity in your specific platform. This activity will be launched as an intent throught your PCL project using, for instance, Dependency Services.
In this custom Activity you should have a GridView which fills its source from the current directory if the file fits your restrictions, such a specific extension, size, etc.
Finally, to get the selected image you just send the image path or whatever you need to the PCL project with DependencyService.
I am developing a Xamarin Forms application which is basically a WebView where the user have access to some files which is supposed the user have to be able to download.
To achieve this in Android, I have this CustomRenderer for the WebView in the Android project, basically I get the cookies (files are under authentication) to send them to the DownloadManager:
public class CustomWebViewRenderer : WebViewRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
var customWebView = Element as CustomWebView;
Control.Settings.AllowContentAccess = true;
Control.Settings.AllowUniversalAccessFromFileURLs = true;
Control.Settings.DomStorageEnabled = true;
Control.Settings.JavaScriptEnabled = true;
Control.Download += OnWebViewDownload;
}
}
private void OnWebViewDownload(object sender, DownloadEventArgs e)
{
var source = AUri.Parse(e.Url);
var request = new DownloadManager.Request(source);
request.AllowScanningByMediaScanner();
var cookieString = CookieManager.Instance.GetCookie(e.Url);
request.AddRequestHeader("Cookie", cookieString);
request.AddRequestHeader("User-Agent", e.UserAgent);
request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);
request.SetDestinationInExternalPublicDir(AEnvironment.DirectoryDownloads, source.LastPathSegment);
var manager = (DownloadManager)MainActivity.Current.GetSystemService("download");
manager.Enqueue(request);
}
}
The download starts in the background and everyone is happy.
My problem started when I noticed that this behavior is not available in iOS, since the file is displayed on the webview not giving me any option to download or showing the "Open in..." dialog. In plus, after trying to implement a CustomRenderer for the iOS platform too, I see no handler, property or method in the UIWebView or the WKWebView which allows me to manage the download or share the file in another app.
I need to be able to download the file in iOS, or at least, show the "Open in" bar. Any suggestions? Thanks in advance.
Am finding hard to launch Cordova Windows App, from another native Windows App.
Using Protocol invocation, I am passing few parameters to Cordova Windows App, to see if the Cordova app identifies those parameters from the Windows Native App.
Is there anyway to pass Parameters from native Windows App to Cordova App, so that Cordova App identifies the parameters as arguments?
In native windows 8 store app I am using app protocol association to send parameters one app to another app. like
in sender app:
mainpage.xaml.cs on button click
var url = "apptest:?" + name;
Uri uri = new Uri(url);
await Launcher.LaunchUriAsync(uri);
in received app
Package.appxmanifest:
Declarations --> available declarations add --> protocol --> name = apptest
app.xaml.cs
protected override void OnActivated(IActivatedEventArgs args)
{
if (args.Kind == ActivationKind.Protocol)
{
ProtocolActivatedEventArgs protocolArgs = args as ProtocolActivatedEventArgs;
var rootFrame = new Frame();
rootFrame.Navigate(typeof(MainPage), protocolArgs);
Window.Current.Content = rootFrame;
}
Window.Current.Activate();
}
mainpage.xaml.cs
protected override void OnNavigatedTo(NavigationEventArgs e)
{
ProtocolActivatedEventArgs pa = e.Parameter as ProtocolActivatedEventArgs;
if(pa != null)enter code here
{
string qS = pa.Uri.Query;
if (!string.IsNullOrEmpty(qS))
{
Txt_name.Text = qS;
}
}
}
in this way i will take the data from sender app.
Same like is there any way to receive data from windows 10 native app to cordova app. it is very hard to find the solution. not able to find the exact piece of code.
I'm trying to implement geofencing in Windows phone 8.1. First I wanted to create a sample Project to understand how it Works, but i couldnt make it works. What I'm trying to achieve is basically, I'll set the coordinates and close the app by pressing back button and it will trigger a toast notification when the phone is in the area of interest.
I've created a blank Windows phone(silverlight) 8.1 Project(geofence_test_01) and added a Windows RT Component Project(BackgroundTask) into the same solution. Added a reference for BackgroundTask in the geofence_test_01 Project.
ID_CAP_LOCATION is enabled in the app manifest.
MainPage.xaml has only one button to start geofencing.
<Button Name="btnStart" Content="Start" Click="btnStart_Click"/>
In btnSave_Click, I call a method which creates the geofence and registers the background task.
private void btnStart_Click(object sender, RoutedEventArgs e)
{
Init_BackgroundGeofence();
registerBackgroundTask();
}
private async Task Init_BackgroundGeofence()
{
//----------------- Crating Geofence ---------------
var geofenceMonitor = GeofenceMonitor.Current;
var geoId = "building9";
var positionBuilding9 = new BasicGeoposition()
{
Latitude = 47.6397,
Longitude = -122.1289
};
var geofence = new Geofence(geoId, new Geocircle(positionBuilding9, 100),
MonitoredGeofenceStates.Entered | MonitoredGeofenceStates.Exited,
false, TimeSpan.FromSeconds(10));
geofenceMonitor.Geofences.Add(geofence);
}
private async Task registerBackgroundTask()
{
//----------------- Register Background Task ---------------
var backgroundAccessStatus =
await BackgroundExecutionManager.RequestAccessAsync();
var geofenceTaskBuilder = new BackgroundTaskBuilder
{
Name = "GeofenceBackgroundTask",
TaskEntryPoint = "BackgroundTask.GeofenceBackgroundTask"
};
var trigger = new LocationTrigger(LocationTriggerType.Geofence);
geofenceTaskBuilder.SetTrigger(trigger);
var geofenceTask = geofenceTaskBuilder.Register();
}
And finally, in BackgroundTask, I've the following code:
namespace BackgroundTask
{
public sealed class GeofenceBackGroundTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
var geofenceMonitor = GeofenceMonitor.Current;
var geoReports = geofenceMonitor.ReadReports();
var geoId = "building9";
foreach (var geofenceStateChangeReport in geoReports)
{
var id = geofenceStateChangeReport.Geofence.Id;
var newState = geofenceStateChangeReport.NewState;
if (id == geoId && newState == GeofenceState.Entered)
{
//------ Call NotifyUser method when Entered -------
notifyUser();
}
}
}
private void notifyUser()
{
var toastTemplate = ToastTemplateType.ToastText02;
var toastXML = ToastNotificationManager.GetTemplateContent(toastTemplate);
var textElements = toastXML.GetElementsByTagName("text");
textElements[0].AppendChild(toastXML.CreateTextNode("You are in!"));
var toast = new ToastNotification(toastXML);
ToastNotificationManager.CreateToastNotifier().Show(toast);
}
}
}
I get no error when building and deploying this in the emulator. I set a breakpoint in the backgroundTask but I've not seen that part of code is called yet. It never hits the breakpoint. I test it by using Additional Tools of the emulator, in Location tab, by clicking somewhere in my geofence area on the map, waiting for a while, but it never hits the breakpoint. Hope somebody can tell me what i am missing here...
I've checked these following links to build this application:
http://www.jayway.com/2014/04/22/windows-phone-8-1-for-developers-geolocation-and-geofencing/
Geofence in the Background Windows Phone 8.1 (WinRT)
Toast notification & Geofence Windows Phone 8.1
http://java.dzone.com/articles/geofencing-windows-phone-81
Thanks
You can download the project here:
https://drive.google.com/file/d/0B8Q_biJCWl4-QndYczR0cjNhNlE/view?usp=sharing
---- Some clues
Thanks to Romasz, I've checked the Lifecycle events and i see "no background tasks" even after registerBackgroundTask() is executed.... Apparently there is something wrong/missing in registerBackgroundTask() method.
I've tried to build my sample (it was easier for me to build a new one) basing on your code and it seems to be working. You can take a look at it at my GitHub.
There are couple of things that may have gone wrong in your case:
remember to add capabilities in WMAppManifest file (IS_CAP_LOCATION) and Package.appxmanifest (Location)
check the names (of namespaces, classes and so on) in BackgroundTask
check if your BackgroundTask project is Windows Runtime Componenet and is added to your main project as a reference
I know you have done some of this things already, but take a look at my sample, try to run it and maybe try to build your own from the very beginning.
Did you add your background task in the Package.appxmanifest under Declarations with the correct supported task types (Namely Location)?