Request camera permissions xamarin forms - xamarin.forms

I have an app that needs permissions for the camera, this is well implemented, but asks for permission on the home screen (splash). What I would like is to appear after logging in or on a specific page (Is this possible?). And as I could implement the same for IOS, I thank you very much for your help and have a great day.
Thanks for you help.
Here my code
MainActivity.cs
const int requestCameraId = 0;
const int requestStorageId = 1;
const int requestId = 2;
readonly string[] permissions =
{
Android.Manifest.Permission.Camera,
Android.Manifest.Permission.ReadExternalStorage,
Android.Manifest.Permission.WriteExternalStorage,
Android.Manifest.Permission.Internet,
Android.Manifest.Permission.ForegroundService,
Android.Manifest.Permission.RequestCompanionUseDataInBackground,
Android.Manifest.Permission.RequestCompanionRunInBackground,
Android.Manifest.Permission.StatusBar,
Android.Manifest.Permission.Vibrate,
Android.Manifest.Permission.Flashlight
};
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
switch (requestCode)
{
case requestCameraId:
{
if (grantResults[0] == (int)Android.Content.PM.Permission.Granted)
{
Toast.MakeText(this, "Permiso concedido para la camara", ToastLength.Short).Show();
}
else
{
//Permission Denied :(
Toast.MakeText(this, "Permiso denegado para la camara", ToastLength.Short).Show();
}
}
break;
case requestStorageId:
{
if (grantResults[0] == (int)Android.Content.PM.Permission.Granted)
{
Toast.MakeText(this, "Permiso concedido para el almacenamiento", ToastLength.Short).Show();
}
else
{
//Permission Denied :(
Toast.MakeText(this, "Permiso denegado para el almacenamiento", ToastLength.Short).Show();
}
}
break;
}
}
async Task GetCameraPermissionAsync()
{
const string permission = Manifest.Permission.Camera;
if (CheckSelfPermission(permission) == (int)Android.Content.PM.Permission.Granted)
{
//TODO change the message to show the permissions name
Toast.MakeText(this, "Permisos para la camara listos", ToastLength.Short).Show();
return;
}
if (ShouldShowRequestPermissionRationale(permission))
{
//set alert for executing the task
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.SetTitle("Permisos necesarios");
alert.SetMessage("La aplicación necesita acceder a la camara para tomar una fotografía del trabajo terminado");
alert.SetPositiveButton("Conceder permiso", (senderAlert, args) =>
{
RequestPermissions(permissions, requestCameraId);
});
alert.SetNegativeButton("Cancelar", (senderAlert, args) =>
{
Toast.MakeText(this, "Cancelado", ToastLength.Short).Show();
});
Dialog dialog = alert.Create();
dialog.Show();
return;
}
}
async Task GetStoragePermissionAsync()
{
const string permission = Manifest.Permission.ReadExternalStorage;
if (CheckSelfPermission(permission) == (int)Android.Content.PM.Permission.Granted)
{
//TODO change the message to show the permissions name
Toast.MakeText(this, "Permisos para leer carpetas listos", ToastLength.Short).Show();
return;
}
if (ShouldShowRequestPermissionRationale(permission))
{
//set alert for executing the task
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.SetTitle("Permisos necesarios");
alert.SetMessage("La aplicación necesita acceder a sus archivos para subir una imagen con el trabajo terminado");
alert.SetPositiveButton("Conceder permiso", (senderAlert, args) =>
{
RequestPermissions(permissions, requestStorageId);
});
alert.SetNegativeButton("Cancelar", (senderAlert, args) =>
{
Toast.MakeText(this, "Cancelado", ToastLength.Short).Show();
});
Dialog dialog = alert.Create();
dialog.Show();
return;
}
}
async Task GetPermissionsAsync()
{
await GetCameraPermissionAsync();
await GetStoragePermissionAsync();
RequestPermissions(permissions, requestId);
}
async Task TryToGetPermissions()
{
if ((int)Build.VERSION.SdkInt >= 23)
{
await GetPermissionsAsync();
return;
}
}
protected async override void OnCreate(Bundle savedInstanceState)
{
await TryToGetPermissions();
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// RequestPermissions(permissions, requestId);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
Xamarin.Forms.Application.Current.On<Xamarin.Forms.PlatformConfiguration.Android>().UseWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize);
CreateNotificationFromIntent(Intent);
//notificationServiceIntent = new Intent(this.BaseContext, typeof(PDANotificationService));
//StartService(notificationServiceIntent);
WireUpLongRunningTask();
var message = new StartLongRunningTaskMessage();
MessagingCenter.Send(message, "StartLongRunningTaskMessage");
}

As #Jason said, your code requests permission on main Activity create, so as soon as you open your app, it will ask for all the permissions you listed. You need to request permission separately.
Say camera permission:
if (CheckSelfPermission(Manifest.Permission.Camera) != (int)Permission.Granted)
{
//request permission
}else
{
//call camera
}
And OnRequestPermissionsResult will be called after user granted/denied the permission request, you can check the result and call camera if the permission was granted.
Please refer to this for detail workflow about permission.

Related

Background location service stops working after a few minutes [closed]

Closed. This question is not written in English. It is not currently accepting answers.
Stack Overflow is an English-only site. The author must be able to communicate in English to understand and engage with any comments and/or answers their question receives. Don't translate this post for the author; machine translations can be inaccurate, and even human translations can alter the intended meaning of the post.
Closed 2 days ago.
Improve this question
Estoy realizando una app en xamarin forms y xamarin.android para el tracking de una servicio, ya tengo creado mi servicio y la peticion de la ubicacion, el problema radica en que el dispositivo deja de obtener la ubicacion luego de un tiempo cuando se encuentra suspendido o en segundo plano, esto me pasa solo en mi dispositivo fisico ya que con el emulador no se me presenta este error
Service
[Service]
public class AndroidLocationService : Service
{
CancellationTokenSource _cts;
public const int SERVICE_RUNNING_NOTIFICATION_ID = 10001;
public static string NAME = "com.swfactorygroup.bongoadm.WakefulIntentService";
private static volatile PowerManager.WakeLock lockStatic = null;
[MethodImpl(MethodImplOptions.Synchronized)]
private static PowerManager.WakeLock GetLock(Context context)
{
if (lockStatic == null)
{
PowerManager manager = (PowerManager)context.GetSystemService(Context.PowerService);
lockStatic = manager.NewWakeLock(WakeLockFlags.Partial, NAME);
lockStatic.SetReferenceCounted(false);
}
return (lockStatic);
}
public override IBinder OnBind(Intent intent)
{
return null;
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
_cts = new CancellationTokenSource();
Notification notification = new NotificationHelper().GetServiceStartedNotification();
StartForeground(SERVICE_RUNNING_NOTIFICATION_ID, notification);
PowerManager.WakeLock wakeLock = GetLock(this.ApplicationContext);
wakeLock.Acquire();
Task.Run(() => {
try
{
var locShared = new GetLocationService();
locShared.Run(_cts.Token).Wait();
}
catch (Android.OS.OperationCanceledException)
{
}
finally
{
if (_cts.IsCancellationRequested)
{
var message = new StopServiceMessage();
Device.BeginInvokeOnMainThread(
() => MessagingCenter.Send(message, "ServiceStopped")
);
}
PowerManager.WakeLock wakeLock = GetLock(this.ApplicationContext);
if (wakeLock.IsHeld)
{
try
{
wakeLock.Release();
}
catch (Exception ex)
{
Log.Error(Class.SimpleName, "Exception when releasing wakelock", ex);
//Log exception when releasing wakelock
}
}
}
}, _cts.Token);
return StartCommandResult.Sticky;
}
public override void OnDestroy()
{
if (_cts != null)
{
_cts.Token.ThrowIfCancellationRequested();
_cts.Cancel();
}
base.OnDestroy();
}
}
public class GetLocationService
{
readonly bool stopping = false;
SQLiteHelper _bd = App.BDSQLite;
public GetLocationService()
{
}
public async Task Run(CancellationToken token)
{
await Task.Run(async () => {
while (!stopping)
{
token.ThrowIfCancellationRequested();
try
{
await Task.Delay(10000);
var request = new GeolocationRequest(GeolocationAccuracy.Best);
var location = await Geolocation.GetLocationAsync(request);
if (location != null)
{
var message = new LocationMessage
{
Latitude = location.Latitude,
Longitude = location.Longitude
};
Device.BeginInvokeOnMainThread(() =>
{
MessagingCenter.Send(message, "Location");
});
}
}
catch (Exception ex)
{
Device.BeginInvokeOnMainThread(() =>
{
var errormessage = new LocationErrorMessage();
MessagingCenter.Send(errormessage, "LocationError");
});
}
}
return;
}, token);
}
}
I tried using a PowerManager.WakeLock, but it is not poorly implemented, it seems that the device will kill the process after a few minutes

Xamarin.Forms Speakerphone NOT Activating on Android 10, 11, 12

I have App that has been working for years on Android 10, 11, 12 to Turn On/Off SpeakerPhone for Inbound and Outbound calls, but now it works sometimes on Inbound calls but never on Outbound calls. Here is a simple BroadcastReciever and MainActivity.
[BroadcastReceiver(Enabled = true, Exported = true)]
[IntentFilter(new[] { TelephonyManager.ActionPhoneStateChanged })]
public class PhonecallReceiver : BroadcastReceiver
{
public override async void OnReceive(Context context, Intent intent)
{
Toast.MakeText(context, "Received intent!", ToastLength.Long).Show();
var state = intent.GetStringExtra(TelephonyManager.ExtraState);
if (intent.Action == TelephonyManager.ActionPhoneStateChanged)
{
if (state == TelephonyManager.ExtraStateRinging)
{
//Task.Run(async () =>
//{
// await Task.Delay(1);
// Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
// {
// MainActivity.audioManager = (AudioManager)Application.Context.GetSystemService(Context.AudioService);
// if (!MainActivity.audioManager.SpeakerphoneOn)
// { MainActivity.audioManager.SpeakerphoneOn = true; }
// var mode = Mode.Normal;// | Mode.Ringtone | Mode.InCommunication | Mode.Normal;
// MainActivity.audioManager.SetRouting(mode, Route.Speaker, Route.All);
// });
//});
MainActivity.audioManager.SpeakerphoneOn = true;
}
else if (state == TelephonyManager.ExtraStateOffhook)
{
MainActivity.audioManager.SpeakerphoneOn = true;
}
else if (state == TelephonyManager.ExtraStateIdle)
{
MainActivity.audioManager.SpeakerphoneOn = false;
}
}
}
}
In the MainActivity
public static AudioManager audioManager { get; set; }
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
var permissions = new string[]
{
Manifest.Permission.ReadPhoneState,
Manifest.Permission.ModifyAudioSettings,
Manifest.Permission.ModifyPhoneState
};
ActivityCompat.RequestPermissions(this, permissions, 123);
audioManager = (AudioManager)Application.Context.GetSystemService(Context.AudioService);
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 123 && grantResults.Length > 0 && grantResults[0] == Permission.Granted)
{
RegisterReceiver(new PhonecallReceiver(), new IntentFilter(TelephonyManager.ActionPhoneStateChanged));
}
}
The above code used to Turn On/Off SpeakerPhone for Inbound/Outbound calls. Now it does not Turn ON the Speakerphone for Outbound Call and sometimes work for Inbound call. My problem is occurring on LG-K51 Android 12 mobile phone that I have Factory Reset.
Thanks

How to make phone calls using Xamairn forms?

I'm new in xamarin and I want make a phone call directly (without opening the dialler). I tried with this example but it doesn't work.
Click Please help
public class PhoneCall_Droid : IPhoneCall
{
public void MakeQuickCall(string PhoneNumber)
{
try
{
var uri = Android.Net.Uri.Parse(string.Format("tel:{0}", PhoneNumber));
var intent = new Intent(Intent.ActionCall, uri);
Xamarin.Forms.Forms.Context.StartActivity(intent);
}
catch (Exception ex)
{
new AlertDialog.Builder(Android.App.Application.Context).SetPositiveButton("OK", (sender, args) =>
{
//User pressed OK
})
.SetMessage(ex.ToString())
.SetTitle("Android Exception")
.Show();
}
}
}
there are two error in your code above:
1.Xamarin.Forms.Forms.Context could not get the correct context.
you could defined a static variable in MainActiviy like :
public static MainActivity Instance;
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
Instance = this;
LoadApplication(new App());
}
you also could use the Current Activity Plugin,you could refer to Current Activity
2.After Android6.0 you should requests the runtime permissions and the official doucument
here is a simple example:
[assembly: Xamarin.Forms.Dependency(typeof(PhoneCall_Droid))]
namespace App18.Droid
{
class PhoneCall_Droid: IPhoneCall
{
public void MakeQuickCall(string PhoneNumber)
{
try
{
if(ActivityCompat.CheckSelfPermission(MainActivity.Instance, Android.Manifest.Permission.CallPhone) != Android.Content.PM.Permission.Granted ){
ActivityCompat.RequestPermissions(MainActivity.Instance, new string[] {Android.Manifest.Permission.CallPhone }, 1);
return;
}
else
{
var uri = Android.Net.Uri.Parse(string.Format("tel:{0}", PhoneNumber));
var intent = new Intent(Intent.ActionCall, uri);
MainActivity.Instance.StartActivity(intent);
}
}
catch (Exception ex)
{
new AlertDialog.Builder(MainActivity.Instance).SetPositiveButton("OK", (sender, args) =>
{
//User pressed OK
})
.SetMessage(ex.ToString())
.SetTitle("Android Exception")
.Show();
}
}
}
}
and you also could to use the nugetpackage Plugin.Permissions to request runtime permissions(Permission.Location)
refer to Plugin.Permissions
finally you could call like
DependencyService.Get<IPhoneCall>().MakeQuickCall(phonenumber);

Xamarin Forms: Load a contentpage when tap push notification android

I completed receiving test notification from FCM console. Now I am trying to open a page when tapping the notification. Any ideas about how to achieve this? I have searched the internet but can't find a working solution. I am also able to send the notification through the postman.
I handle the notification tapping in following way. The page loading is handled in App.xaml.cs.
On OnCreate():
//Background or killed mode
if (Intent.Extras != null)
{
foreach (var key in Intent.Extras.KeySet())
{
var value = Intent.Extras.GetString(key);
if (key == "webContentList")
{
if (value?.Length > 0)
{
isNotification = true;
LoadApplication(new App(domainname, value));
}
}
}
}
//Foreground mode
if (FirebaseNotificationService.webContentList.ToString() != "")
{
isNotification = true;
LoadApplication(new App(domainname, FirebaseNotificationService.webContentList.ToString()));
FirebaseNotificationService.webContentList = "";
}
//Normal loading
if (!isNotification)
{
LoadApplication(new App(domainname, string.Empty));
}
On FirebaseNotificationService:
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class FirebaseNotificationService : FirebaseMessagingService
{
public static string webContentList = "";
public override void OnMessageReceived(RemoteMessage message)
{
base.OnMessageReceived(message);
webContentList = message.Data["webContentList"];
try
{
SendNotificatios(message.GetNotification().Body, message.GetNotification().Title);
}
catch (Exception ex)
{
Console.WriteLine("Error:>>" + ex);
}
}
public void SendNotificatios(string body, string Header)
{
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new Android.App.Notification.Builder(this, Utils.CHANNEL_ID)
.SetContentTitle(Header)
.SetSmallIcon(Resource.Drawable.icon)
.SetContentText(body)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(0, notificationBuilder.Build());
}
else
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new Android.App.Notification.Builder(this, Utils.CHANNEL_ID)
.SetContentTitle(Header)
.SetSmallIcon(Resource.Drawable.icon)
.SetContentText(body)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent)
.SetChannelId(Utils.CHANNEL_ID);
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
return;
}
var channel = new NotificationChannel(Utils.CHANNEL_ID, "FCM Notifications", NotificationImportance.High)
{
Description = "Firebase Cloud Messages appear in this channel"
};
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
notificationManager.Notify(0, notificationBuilder.Build());
}
}
I don't know what's your actual Firebase implementation, but this might help you.
There is a nice package for Firebase in Xamarin Forms that we use in our production App made by CrossGeeks team. Its working great and has all handlers for your needs. This works with iOS and Android and you don't need to write platform specific code, only configuration and some code in AppDelegate.cs and MainActivity.cs
https://github.com/CrossGeeks/FirebasePushNotificationPlugin/blob/master/docs/FirebaseNotifications.md#notification-events
I wrote a simple PushNotificationService that processes automatic refresh and/or pushes new pages considering push notif data.
When app is closed and user clicks on the notification, I store the push notif data using Akavache.
CrossFirebasePushNotification.Current.OnNotificationOpened += async (s, p) =>
{
if (App.AppBeenResumed)
{
await BlobCache.UserAccount.InsertObject("pushNotifData", p.Data);
}
else
{
await ProcessReceivedPushNotification(p.Data);
}
};
And on the landing page of the app, I check if there is an existing push notif data in the OnAppearing method of the page.
protected override void OnAppearing()
{
base.OnAppearing();
App.AppBeenResumed = false;
HandlePushNotificationIfExists();
}
private async void HandlePushNotificationIfExists()
{
IDictionary<string, object> pushNotifData;
try
{
pushNotifData = await BlobCache.UserAccount.GetObject<IDictionary<string, object>>("pushNotifData");
}
catch (KeyNotFoundException)
{
pushNotifData = null;
}
if (pushNotifData == null) return;
await BlobCache.UserAccount.InvalidateAllObjects<IDictionary<string, object>>();
await PushNotificationService.ProcessReceivedPushNotification(pushNotifData);
}
In the ProcessReceivedPushNotification you can do whatever you want ... push directly the page or whatever... call another service that will do the job of pushing a new page and some business process.
Note that App.AppBeenResumed is a static bool to determine if App has been started or resumed to handle correctly the process of treatment of the push notif (process it instant or store it in blobcache to treat it later when landing page is Appearing).
In MainActivity.cs :
protected override void OnCreate(Bundle bundle)
{
...
LoadApplication(new App(true));
}
In the App.cs :
public App(bool beenResumedOrStarted)
{
...
AppBeenResumed = beenResumedOrStarted;
...
}
protected override void OnResume()
{
AppBeenResumed = false;
}
protected override void OnSleep()
{
//iOS states are not the same so always false when device is iOS
AppBeenResumed = Device.RuntimePlatform != Device.iOS;
}

Can't retrieve Firebase InstanceId Token after updating to newest library

Okay so after updating to the newest firebase messaging library, I can't seem to retrieve the token. I t said in the docs that it should not be run in the main thread but when I tried, it still didn't work. So I simply removed it. What could I be doing wrong?
Below is my try/catch code.
String device_token;
try {
device_token = FirebaseInstanceId.getInstance().getToken(R.string.sender_id, "FCM");
student_token_reference = FirebaseDatabase.getInstance().getReference().child("MakeUpArtists_Info").child(uid);
student_token_reference.child("device_token").setValue(device_token).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
DatabaseReference check_variablesRef = FirebaseDatabase.getInstance().getReference().child("MakeUpArtists_Info").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("Verification");
check_variablesRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String id_card = dataSnapshot.child("id_card").getValue().toString();
String image = dataSnapshot.child("image").getValue().toString();
if (id_card.equals("Not yet") && image.equals("Not yet")) {
Toast.makeText(LoginActivity.this, "Please finish uploading your documents", Toast.LENGTH_SHORT).show();
Intent id_card_intent = new Intent(LoginActivity.this, IDcard.class);
startActivity(id_card_intent);
finish();
} else if (id_card.equals("Received") && image.equals("Received")) {
Intent intentMain = new Intent(LoginActivity.this, MainActivity.class);
intentMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intentMain);
finish();
} else if (id_card.equals("Received") && image.equals("Not yet")) {
Toast.makeText(LoginActivity.this, "Please upload your profile picture", Toast.LENGTH_SHORT).show();
Intent success_intent = new Intent(LoginActivity.this, DisplayProfile.class);
startActivity(success_intent);
finish();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
} catch (IOException e) {
e.printStackTrace();
}
Any help would be greatly appreciated!
The Firebase Release Notes for the June 28 release recommend using FirebaseInstanceId.getInstance().getInstanceId(). Here is an example (as you requested in comments) of how to do that:
FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(
new OnCompleteListener<InstanceIdResult>() {
#Override
public void onComplete(Task<InstanceIdResult> task) {
if (task.isSuccessful()) {
final InstanceIdResult iidResult = task.getResult();
final String token = iidResult.getToken();
Log.d(TAG, "token=" + token);
// process token as you need...
} else {
Log.e(TAG, "get IID/token failed", task.getException());
}
}
});

Resources