Problem with Xamarin autostart application - c#

I am currently developing an app to use a barcode scanner, one of the requirements is that app autostart with the device.
so, google on that I found some interest thinks, but when I implemented it fails epicly, the app does not starts.
this is mainfest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="AppConsulta.Droid" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
<uses-sdk android:minSdkVersion="23" android:targetSdkVersion="24" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:label="AppConsulta.Android" android:icon="#drawable/Logo_Jumbo_Cencosud">
<activity android:name="MainActivity" />
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
and this is my receiver
namespace AppConsulta.Droid
{
[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionBootCompleted }, Priority = (int)IntentFilterPriority.LowPriority)]
public class BootReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Intent serviceStart = new Intent(context, typeof(MainActivity));
serviceStart.AddFlags(ActivityFlags.NewTask);
context.StartActivity(serviceStart);
}
}
}
using adb.exe I manage to extract the log of the device
and it shows this
10-22 16:33:00.412 2007 2007 E AndroidRuntime: FATAL EXCEPTION: main
10-22 16:33:00.412 2007 2007 E AndroidRuntime: Process: AppConsulta.Droid, PID: 2007
10-22 16:33:00.412 2007 2007 E AndroidRuntime: java.lang.RuntimeException: Unable to instantiate receiver AppConsulta.Droid.BootReceiver: java.lang.ClassNotFoundException: Didn't find class "AppConsulta.Droid.BootReceiver" on path: DexPathList[[zip file "/data/app/AppConsulta.Droid-1/base.apk"],nativeLibraryDirectories=[/data/app/AppConsulta.Droid-1/lib/arm, /data/app/AppConsulta.Droid-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]

Related

Ping Permission denied

When I try to ping a device from my MAUI App built for Android I get a SocketException(Permission Denied). When i build for Windows it works just fine.
I can not find any Permission other than the ones I already have that could be required. Does anyone know what could be causing this?
Tested with Android 11 and Android 12 on physical devices.
EDIT:
Network-connection works fine through HttpClient.
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" />
<application android:allowBackup="true" android:icon="#mipmap/appicon" android:roundIcon="#mipmap/appicon_round" android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Code:
var sender = new System.Net.NetworkInformation.Ping();
var options = new System.Net.NetworkInformation.PingOptions();
options.DontFragment = true;
string data = "testdata";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 100;
PingReply reply = sender.Send(address, timeout, buffer, options);
Exception:
System.Net.Sockets.SocketException 'Permission denied'

Xamarin form At most one element <application> with namespace '' was expected

I am getting this error as th manifest file contain 2 application element and I know that.
Every time I remove the other application element it gets back again. The element item is the network_security_config.xml file I have set the Build Action = AndroidResource Do not Copy. How can I make sure application containing network_security_config gets created in the existing application element.
<?xml version="1.0" encoding="UTF-8"?>
-<manifest android:installLocation="auto" package="Me.Scheduler" android:versionName="1.0" android:versionCode="1" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:targetSdkVersion="28" android:minSdkVersion="21"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
-<application android:name="android.app.Application" android:debuggable="true" android:icon="#mipmap/icon" android:allowBackup="true" android:label="Scheduler.Android">
-<activity android:name="crc64c2f66de974599db5.MainActivity" android:icon="#mipmap/icon" android:label="Scheduler" android:theme="#style/MainTheme" android:configChanges="orientation|screenSize">
-<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:name="crc64a98abb514ffad9f1.KeepAliveService"/>
<receiver android:name="crc64a0e0a82d0db9a07d.BatteryBroadcastReceiver" android:label="Essentials Battery Broadcast Receiver" android:exported="false" android:enabled="true"/>
<receiver android:name="crc64a0e0a82d0db9a07d.EnergySaverBroadcastReceiver" android:label="Essentials Energy Saver Broadcast Receiver" android:exported="false" android:enabled="true"/>
<receiver android:name="crc64a0e0a82d0db9a07d.ConnectivityBroadcastReceiver" android:label="Essentials Connectivity Broadcast Receiver" android:exported="false" android:enabled="true"/>
-<provider android:name="xamarin.essentials.fileProvider" android:exported="false" android:grantUriPermissions="true" android:authorities="Me.Scheduler.fileProvider">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="#xml/xamarin_essentials_fileprovider_file_paths"/>
</provider>
<receiver android:name="crc643f46942d9dd1fff9.PowerSaveModeBroadcastReceiver" android:exported="false" android:enabled="true"/>
<provider android:name="mono.MonoRuntimeProvider" android:exported="false" android:authorities="Me.Scheduler.mono.MonoRuntimeProvider.__mono_init__" android:initOrder="1999999999"/>
<!--suppress ExportedReceiver-->
-<receiver android:name="mono.android.Seppuku">
-<intent-filter>
<action android:name="mono.android.intent.action.SEPPUKU"/>
<category android:name="mono.android.intent.category.SEPPUKU.Me.Scheduler"/>
</intent-filter>
</receiver>
</application>
<application android:networkSecurityConfig="#xml/network_security_config"/>
</manifest>
I would assume you are actually changing the Android Manifest in bin folder. I guess you access it by double tapping on error you getting? I was making the same mistake. That Manifest is auto generated from Manifest in Properties folder.
Make that change in an actual android manifest in Properties folder, and see if problem persists.

Permission Denial: requires android.permission.READ_PHONE_STATE

I'm trying to detect phone calls in my android app but I receive the following message when receiving a call:
08-23 15:16:04.685 Vodafone VFD 600 Warning 850 BroadcastQueue Permission Denial: receiving Intent { act=android.intent.action.PHONE_STATE flg=0x10 (has extras) } to com....LogCalls requires android.permission.READ_PHONE_STATE due to sender android (uid 1000)
08-23 15:16:04.549 Vodafone VFD 600 Warning 850 BroadcastQueue Permission Denial: receiving Intent { act=android.intent.action.PHONE_STATE flg=0x10 (has extras) } to com....LogCalls requires android.permission.READ_PRIVILEGED_PHONE_STATE due to sender android (uid 1000)
My AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com...." android:installLocation="preferExternal">
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="27" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<application android:label="myapp" android:icon="#drawable/logo">
</application>
</manifest>
And my broadcast receiver:
[BroadcastReceiver]
[IntentFilter(new[] {TelephonyManager.ActionPhoneStateChanged,Intent.ActionNewOutgoingCall })]
public class LogCalls : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
if (intent.Action == TelephonyManager.ActionPhoneStateChanged)
{
Console.WriteLine("state changed");
}
}
}
What I am missing ?
Firstly, third-party apps are not permitted to acquire the READ_PRIVILEGED_PHONE_STATE permission. See Privileged Permission Whitelisting:
Privileged applications are system applications located in the /system/priv-app directory on the system image. Historically, device implementers had little control over which signature|privileged permissions could be granted to privileged apps. Starting in Android 8.0, implementors can explicitly whitelist privileged apps in the system configuration XML files in the /etc/permissions directory. Apps not explicitly listed in these XML files are not granted privileged permissions.
Secondly, when your app is running on API 23 and above, you'll need to first ask the user to grant you the READ_PHONE_STATE permission at runtime, as it is considered a "dangerous" permission (see Permissions Overview).
You'll need to follow the instructions at Request App Permissions to request the permission from the user at runtime, and only once that permission is granted can your BroadcastReceiver receive the intents.

Xamarin Maps Crashing in Android Emulator

I'm having a bit of an issue with the Xamarin.Forms.Maps and the exceptions are not propagated up into the debugger in a meaningful way to help me diagnose the issue.
I can get it to work, everytime I reinstall Visual Studio. It will work a couple times then eventually it will crash 100% of the time as long as the Map item is in the xaml. I am not remaking the project between running, just reinstalling Visual Studio. I'm running Visual Studio 17 Community.
The error just says:
'Your app has entered a break state, but there is no code to show because all threads were executing external code (typically system or framework code).'
I have a valid API Key, and even if I comment out ALL code relating to the Map it eventually happens other than the Xaml that will create it. If I reinstall visual studio I can manipulate the map the first few times I invoke the debugger before it fails.
I wish I could give more information, but I'm pretty new to Xamarin development so I'm a bit unsure why reinstalling Visual Studio fixes the issue, temporarily. Any insight would be much appreciated, as I've spent many hours trying to diagnose and reinstall, but have no idea what the issue could be given the error doesn't really say much of anything. I'd love to provide more information if someone has more ideas for me to check out.
In general, it also fails to deploy eventually also : / which is why I tried an install originally. I'm not sure if this is related.
MainPage.xaml.cs
using System;
using System.Linq;
using System.Text;
using Xamarin.Forms;
using Plugin.Geolocator;
using System.Threading.Tasks;
using System.Collections.Generic;
using Plugin.Geolocator.Abstractions;
namespace LawnDroneCompanion
{
public partial class MainPage : ContentPage
{
private List<Position> RecordedPositions = new List<Position>();
public MainPage()
{
InitializeComponent();
}
private void StopGeoLocatorListening()
{
BeginTrackingRoute.Text = "Begin Tracking";
BeginTrackingRoute.BackgroundColor = Color.Green;
CrossGeolocator.Current.StopListeningAsync();
}
private async void StartGeoLocatorListening(object obj, EventArgs e)
{
if (CrossGeolocator.Current.IsListening)
{
StopGeoLocatorListening();
return;
}
BeginTrackingRoute.Text = "Stop Tracking";
BeginTrackingRoute.BackgroundColor = Color.Red;
CrossGeolocator.Current.DesiredAccuracy = 10;
///This logic will run on the background automatically on iOS, however for Android and UWP you must put logic in background services. Else if your app is killed the location updates will be killed.
await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(1), 1, true, new Plugin.Geolocator.Abstractions.ListenerSettings
{
ActivityType = Plugin.Geolocator.Abstractions.ActivityType.Other,
AllowBackgroundUpdates = true,
DeferLocationUpdates = true,
DeferralDistanceMeters = 1,
DeferralTime = TimeSpan.FromSeconds(1),
ListenForSignificantChanges = true,
PauseLocationUpdatesAutomatically = false
});
Position StartLocation = await CrossGeolocator.Current.GetLastKnownLocationAsync();
//MyMap.MoveToRegion(Xamarin.Forms.Maps.MapSpan.FromCenterAndRadius(new Xamarin.Forms.Maps.Position(StartLocation.Latitude, StartLocation.Longitude), Xamarin.Forms.Maps.Distance.FromMiles(0.2)));
CrossGeolocator.Current.PositionChanged += GeoLocateHandler;
}
private void DrawNewLocation(Plugin.Geolocator.Abstractions.Position position)
{
if(RecordedPositions.Count > 1000)
{
return;
}
RecordedPositions.Add(position);
//MyMap.MoveToRegion(Xamarin.Forms.Maps.MapSpan.FromCenterAndRadius(new Xamarin.Forms.Maps.Position(position.Latitude, position.Longitude), MyMap.VisibleRegion.Radius));
}
private void GeoLocateHandler(object sender, PositionEventArgs e)
{
Device.BeginInvokeOnMainThread(() =>
{
DrawNewLocation(e.Position);
});
}
}
}
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:LawnDroneCompanion"
xmlns:maps="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps"
x:Class="LawnDroneCompanion.MainPage">
<ContentPage.Content>
<StackLayout>
<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<!-- If I comment this out it works, this comment isn't in my real code -->
<maps:Map x:Name="MyMap" IsShowingUser="True" MapType="Hybrid"/>
</StackLayout>
<Button x:Name="BeginTrackingRoute" HorizontalOptions="FillAndExpand" Text="Begin Tracking" Clicked="StartGeoLocatorListening" BackgroundColor="Green" TextColor="WhiteSmoke" BorderRadius="0"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.LawnDroneCompanion" android:installLocation="auto">
<uses-sdk android:minSdkVersion="25" />
<!-- Google Maps for Android v2 requires OpenGL ES v2 -->
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Allow the application to access Google web-based services. -->
<!-- Google Maps for Android v2 will cache map tiles on external storage -->
<application android:label="LawnDroneCompanion.Android">
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="{IMAGINE-MY-KEY-HERE}" />
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
</application>
</manifest>

Unity not opening plugin activity, package seems incorrect

I am attempting to use this plugin to open an android image picker, however it errors when launched.
The calling script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using LukeWaffel.AndroidGallery;
public class LoadImageWithMagic : MonoBehaviour {
public Image image;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void OpenGallery() {
Debug.Log ("It clicked");
AndroidGallery.Instance.OpenGallery (GalleryCallback);
}
public void GalleryCallback() {
image.material.mainTexture = AndroidGallery.Instance.GetTexture ();
AndroidGallery.Instance.ResetOutput ();
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unity3d.player"
xmlns:tools="http://schemas.android.com/tools"
android:installLocation="preferExternal"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"/>
<application
android:theme="#style/UnityThemeSelector"
android:icon="#drawable/app_icon"
android:label="#string/app_name"
android:debuggable="true">
<activity android:name="com.unity3d.player.UnityPlayerActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
<activity android:name= "com.lukewaffel.androidgallery.Gallery"></activity>
</application>
</manifest>
Error from logcat:
08-25 09:53:09.029 3458-3474/com.TNOAB.FindMyPet I/Unity: It clicked
(Filename: ./artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
08-25 09:53:09.036 3458-3474/com.TNOAB.FindMyPet I/Unity: [Singleton] An instance of LukeWaffel.AndroidGallery.AndroidGallery is needed in the scene, so '(singleton) LukeWaffel.AndroidGallery.AndroidGallery (UnityEngine.GameObject)' was created with DontDestroyOnLoad.
(Filename: ./artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
08-25 09:53:09.060 1485-1737/system_process I/ActivityManager: START u0 {cmp=com.TNOAB.FindMyPet/com.lukewaffel.androidgallery.Gallery} from uid 10061 on display 0
08-25 09:53:09.082 3458-3474/com.TNOAB.FindMyPet I/Unity: AndroidJavaException: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.TNOAB.FindMyPet/com.lukewaffel.androidgallery.Gallery}; have you declared this activity in your AndroidManifest.xml?
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.TNOAB.FindMyPet/com.lukewaffel.androidgallery.Gallery}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1777)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1501)
at android.app.Activity.startActivityForResult(Activity.java:3745)
at android.app.Activity.startActivityForResult(Activity.java:3706)
at android.app.Activity.startActivity(Activity.java:4016)
at android.app.Activity.startActivity(Activity.java:3984)
at com.lukewaffel.androidgallery.UnityBinder.OpenGallery(UnityBinder.java:12)
at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
at com.unity3d.player.UnityPlayer.c
It appears that the issue is Unity is prepending my package to the package of the plugin, causing it to not be found. I have attempted to change my package to the package of the plugin and it still had the same error.
I have tried reinstalling the app and still cannot get the gallery to open.
I have discovered that the issue was all of the plugin files were placed into a subdirectory, so unity could not find the AndroidManifest.xml. After moving all of the items out of the subdirectory, the Gallery opened.
As a note to anyone copying the above code, I then discovered an unrelated error, image.material.mainTexture = AndroidGallery.Instance.GetTexture (); should be image.sprite = AndroidGallery.Instance.GetSprite ();

Categories