How to access to the ServiceManager with Xamarin Android - c#

I'm developing an Android app using Xamarin.Android. I have to access to the ServiceManager in order to retrieve a specific IBinder. The ServiceManager is accessible and visible by Java applications, but I cannot see it on my Visual Studio project with the referenced Mono.Android dll.
Relative Java code:
IBinder b = ServiceManager.getService("<service-name>");
How can I get the same behaviour on C# with Xamarin.Android library?

try in your activity this: var a = Application.GetSystemService ("name-service");
Good luck

Related

Xamarin Android Archive Library (aar) binding. SharedPreferences issue

I try to create a xamarin binding project for aar library. But if I add interface or class which extends SharedPreferences, it is not compiled. How to fix this?
See screenshot with logs below
I also created a project to demonstrate the issue.
https://github.com/3akat/SharedPrefsXamarinBindingIssueDemo

Getting an embedded resource from a shared project

I'm working on a Xamarin Forms app which has two projects, an Android App and an iOS app. All the other code is stored in shared projects.
Solution looks like this:
MyApp.Android (Android project), references MyApp.Base
MyApp.iOS (iOS project), references MyApp.Base
MyApp.Base (Shared project)
I am using the following code to read an SVG image from the shared project:
using (var stream = GetType().Assembly.GetManifestResourceStream("MyApp.Base.image.svg"))
{
// do work here...
}
This works perfectly when the image is in the Android or iOS project, but I want the image to be shared so I put it in the shared project.
GetType().Assembly return MyApp.Android, thus it cannot find the image. I suppose I'm overlooking something but I haven't been able to find a solution.
Can anyone point me in the right direction? Thanks!
You are using the Shared Project NOT the PCL or .Net Standard library. So, all of your Shared Project contents will get merged into the Platform specific project when you Compile them. That means - even if you Embed your resource in your MyApp.Base that will get merged into .iOS/.Droid project.
I suggest you to learn more about Shared Project vs PCL or .Net Standard library.
Below code; I did't tested but this is the direction that you should follow:
#if __IOS__
var resourcePrefix = "MyApp.iOS.";
#endif
#if __ANDROID__
var resourcePrefix = "MyApp.Android.";
#endif
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(SharedPage)).Assembly;
Stream stream = assembly.GetManifestResourceStream
(resourcePrefix + "image.svg");
For more information please look into this page : https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/files?tabs=macos#embedding-in-shared-projects

C# Xamarin: Failed to override ShouldOverrideUrlLoading(WebView, IWebResourceRequest)

I've a class which implements Android.Webkit.WebViewClient in my C# Xamarin project. However, the following error show up after I built the project with a fresh-install Microsoft Visual Studio 2017 in another machine:-
'CustomWebViewClient.ShouldOverrideUrlLoading(WebView, IWebResourceRequest)': no suitable method found to override
I guess it's due to the installed AVD API Level as the previous overload was deprecated in Android Nougat but I'm not sure how to fix it. Can someone throw me some light?
Additional info:-
Installed AVD (Android 6.0 | API Level 23)
Overload bool ShouldOverrideUrlLoading(WebView, string) (this should be marked obsolete but it's not the case when I check the class definition in Mono.Android.dll)
You have to put the ShouldOverrideUrlLoading override into CustomWebViewClient class, if you want to have access to your webview from this class the override has the webview in the parameters.
Alright I think I've found the solution. I need to download the latest API Level via Android SDK Manager as the project was set to compile using the latest platform by default which is missing in the new machine.

Autofac null service resolve exception

I have a 3 project in my solution:
Portable Library (for ios, android and winphones), library project(for windows phone) and windows phone test project. My library project got reference to portable library project, and my windows phone test project got reference to portable and winphone library project. In my portable library project I got some service interface which I implement in library winphone project. In my portable project I want have some class that have fields with service that I implement in windows phone library and use this class in windows phone test project. To do this i try use Autofac library, to register types in my windows phone library( to do this I made class with static ctor) and try to resolve service in my portable library class which I register in my windows phone library, but I got null exception and I don't know how to fix that, and it is possible to do things which I actually trying to do ?
My class from portable library project, after call Resolve method I go exception.
public IDeviceInformation DeviceInformation { get; }
public Logger()
{
DeviceInformation = AutofacContainer.Instance.Resolve<IDeviceInformation>();
}
Class from library project
public class AutoFacRegister
{
static AutoFacRegister()
{
var builder = new ContainerBuilder();
builder.RegisterType<WindowsDeviceInformation>()
.As<IDeviceInformation>();
AutofacContainer.Instance = builder.Build();
}
}
I'd recommend against using a static constructor to create your container. I suspect that the constructor is never called since the AutoFacRegister class is not used anywhere and therefore AutofacContainer.Instance will always be null.
I would suggest to use modules to achieve your goal.

Xamarin iOS Dependency services always return null object in iOS application

I use Xamarin Studio and work on iOS Dependency Services
Here I have tried to resolve the runtime issue in case of getting the object through dependency services.
We have tried the same in sample projects as below:
1) Created a PCL project and kept the interfaces there. Using Xamarin.Forms ver 1.2.2.6243.
2) Created an iOS class library (using classic API's) which has the implementation of above
interface.(references added for above)
3) Created an iOS project which is starting point and it has invoked the Dependency Service method
to get the object of implemented class. (references added for both of above)
4) Tried to get the object of above class and call some interface methods implemented in iOS library
project.
Results : I am getting runtime issue as Object returned every time by dependency services was null and not able to call any API’s implemented in iOS library.
Please suggest if we are missing any thing or done some thing wrong here.
put the attribute above namespace.
[assembly: Xamarin.Forms.Dependency(typeof (SQLite_iOS))]
namespace DataBinding.iOS
...
use the full name for 'Dependency', i.e. 'Xamarin.Forms.Dependency(...)'

Categories

Resources