[Xamarin.Android]: Cleartext HTTP traffic to not permitted

If your Android application gives the following error, when trying to use a HttpClient:

Java.IO.IOException: 'Cleartext HTTP traffic to 192.168.0.10 not permitted'

Set usesClearTraffic property to true in the AndroidManifest.xml:

<application android:label="MobileSolution.Android" 
	     android:theme="@style/MainTheme"
             android:usesCleartextTraffic="true">
		
</application>

The restriction was introduced in Android 8. More explanation: https://koz.io/android-m-and-the-war-on-cleartext-traffic/

This content has 3 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

Xamarin Android: Dismiss lock screen or show activity on lock screen

To gain an Activity to be showed on the lock screen, or bypass the phone’s lock screen (aka Keyguard) for important notification handling, for example an Incoming VOIP call, you need to do the following:

Set the permission for disabling keyguard. Add the following permission to the AndroidManifest.xml:

  <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>

Add flags to the Activity’s Window in order to show the activity on the lock screen, or to bypass the lock screen.

this.Window.AddFlags(WindowManagerFlags.DismissKeyguard | WindowManagerFlags.ShowWhenLocked);

Dismiss the keyguard if needed. (CrossCurrentActivity plugin is used to get the current activity):

KeyguardManager keyguardManager = (KeyguardManager)CrossCurrentActivity.Current.Activity?.GetSystemService(Context.KeyguardService);
if (keyguardManager != null)
{	
	keyguardManager.RequestDismissKeyguard(CrossCurrentActivity.Current.Activity, null);
}

This content has 4 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

Xamarin Android: Native image scaling, images are pixelated

If you use Android’s native image scaling, from ldpi, mdpi, hdpi, to xxxhdpi, and have all the image resource in the right directory of your Xamarin.Android project folder, and find the pictures displaying pixelated: Make sure, you are correctly set the Build action for each images to AndroidResource. When adding an existing item to the resources folder from Windows version of VisualStudio’s browse dialog, the Build action will be set to BundleResource, and won’t appear in the Resources.designer.cs file, so it will be unavailable to use from code. If the picture is displaying, but it is pixelated, you may check the higher DPI version image files, have the correct buid action

This content has 4 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.

Xamarin Android: “Program type already present”

AndroidX migráció után a következő hibaüzenettel nem fordul le az alkalmazás:

D8 : error : Program type already present: android.support.v4.app.INotificationSideChannel$Stub$Proxy

Próbáltam mindent, kitöröltem a lokális NuGet Cache-t, az összes bin és obj mappát, Cleanelni a Solutiont, Restoreolni a NuGet-eket. Nem járt eredménnyel semmi sem. Aztán nyitottam egy hibát az AndroidX repo-n (https://github.com/xamarin/AndroidX/issues/100) , ahol a megszokotthoz képest lassan reagáltak, akkor sem tudtak érdemben segíteni, így magamnak kellett megoldást találni a problémára.

Elsőre feltelepítettem egy virtuális gépre friss Windows 10-et, rá pedig egy 2019-es Visual Studio Community-t. Lecloneoztam a repositoryt, megfordítottam, és sikeresen lefordult. Itt már gondoltam, hogy valami specifikus hiba lesz. Pár hétre jegelve lett a projekt, majd végül a megoldása az lett, hogy a host gépen teljesen eltávolítottam fizikailag az Azure DevOps GIT alapú repositoryt, majd újra leszedtem a remote-ról, és működött.

This content has 4 years. Some of the information in this post may be out of date or no longer work. Please, read this page keeping its age in your mind.