How to create a app where that app services always runs in background even if the app is closed in Xamarin (Android)? - xamarin

I am creating an application in Xamarin using visual studio,where that app services always should be running in background even if the app is closed and that service should send notification in a interval.
If that app is closed by the system(in the condition when it is taking more memory space) then it should restart the services by showing the status as "0 process and 0 service Restarting.." under running services list in Settings.
Can anyone help me how to achieve that having some reference of sample project.
In BackgroundService.cs
[Service]
public class BackgroundService : Service
{
const int SERVICE_RUNNING_NOTIFICATION_ID = 123;
const string NOTIFICATION_CHANNEL_ID = "com.company.app.channel";
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
Log.Debug("service", "Service Started");
// Check if device is running Android 8.0 or higher and call StartForeground() if so
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
var notification = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.SetContentTitle(Resources.GetString(Resource.String.app_name))
.SetContentText(Resources.GetString(Resource.String.notification_text))
.SetSmallIcon(Resource.Drawable.notification_icon_background)
.SetOngoing(true)
.Build();
var notificationManager =
GetSystemService(NotificationService) as NotificationManager;
var chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "On-going Notification", NotificationImportance.Min);
notificationManager.CreateNotificationChannel(chan);
StartForeground(SERVICE_RUNNING_NOTIFICATION_ID, notification);
}
return StartCommandResult.NotSticky;
}
public override IBinder OnBind(Intent intent)
{
return null;
}
public override void OnTaskRemoved(Intent rootIntent)
{
Intent restartServiceIntent = new Intent(this, this.Class);
restartServiceIntent.SetPackage(this.PackageName);
Log.Debug("service", "Service Restarted");
PendingIntent restartServicePendingIntent = PendingIntent.GetService(this, 1, restartServiceIntent, PendingIntentFlags.OneShot);
AlarmManager alarmService = (AlarmManager)this.GetSystemService(Context.AlarmService);
alarmService.Set(
AlarmType.ElapsedRealtime,
SystemClock.ElapsedRealtime() + 1000,
restartServicePendingIntent);
base.OnTaskRemoved(rootIntent);
}
}
Updtaed MainActivity.cs
public class MainActivity : AppCompatActivity
{
String alarm = Android.Content.Context.AlarmService;
const string NOTIFICATION_CHANNEL_ID = "com.companyname.ServiceSample3";
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.abc_activity_chooser_view);
Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
//var alarmIntent = new Intent(this, typeof(AlarmReceiver));
//alarmIntent.PutExtra("title", "Hello");
//alarmIntent.PutExtra("message", "World!");
Intent intent = new Intent(NOTIFICATION_CHANNEL_ID);
intent.SetClass(this, typeof(AlarmReceiver));
var pending = PendingIntent.GetBroadcast(this, 0, intent, PendingIntentFlags.UpdateCurrent);
var alarmManager = GetSystemService(AlarmService).JavaCast<AlarmManager>();
alarmManager.SetRepeating(AlarmType.ElapsedRealtime, DateTime.Now.Millisecond, 5 * 100, pending);
}
}
Thanks in advance.

Please refer the below sample.You can use alarm manager for restarting the service.
Please follow the steps.
Step 1 :
Create BroadcastReceiver
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace AlarmManagerApp
{
[BroadcastReceiver(Enabled = true)]
[IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
class AlarmReceiver : BroadcastReceiver
{
Context context;
public override void OnReceive(Context context, Intent intent)
{
this.context = context;
Toast.MakeText(context,"Recieved",ToastLength.Long).Show();
Intent background = new Intent(context, typeof(BackgroundService));
context.StartService(background);
}
}
}
Step 2: Add AlarmManager in the MainActivity where you want to call the service
namespace AlarmManagerApp
{
[Activity(Label = "#string/app_name", Theme = "#style/AppTheme.NoActionBar", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
String alarm = Context.AlarmService;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
var alarmIntent = new Intent(this, typeof(AlarmReceiver));
alarmIntent.PutExtra("title", "Hello");
alarmIntent.PutExtra("message", "World!");
var pending = PendingIntent.GetBroadcast(this, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
var alarmManager = GetSystemService(AlarmService).JavaCast<AlarmManager>();
alarmManager.SetRepeating(AlarmType.ElapsedRealtime,DateTime.Now.Millisecond, 5 * 100, pending);
}
}
Step 3: Add you Service
namespace AlarmManagerApp
{
[Service]
public class BackgroundService : Service
{
public override void OnCreate()
{
base.OnCreate();
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
Log.Debug("service","Service Started");
//init the handler in oncreate
System.Timers.Timer Timer1 = new System.Timers.Timer();
Timer1.Start();
Timer1.Interval = 3000;
int a = 0;
Timer1.Enabled = true;
//Timer1.Elapsed += OnTimedEvent;
Timer1.Elapsed += (object sender, System.Timers.ElapsedEventArgs e) =>
{
Timer1.Stop();
Timer1.Start();
a++;
//Delete time since it will no longer be used.
Timer1.Dispose();
};
Timer1.Start();
return StartCommandResult.Sticky;
}
public override IBinder OnBind(Intent intent)
{
return null;
}
public override void OnTaskRemoved(Intent rootIntent)
{
Intent restartServiceIntent = new Intent(this, this.Class);
restartServiceIntent.SetPackage(this.PackageName);
Log.Debug("service", "Service Restarted");
PendingIntent restartServicePendingIntent = PendingIntent.GetService(this, 1, restartServiceIntent, PendingIntentFlags.OneShot);
AlarmManager alarmService = (AlarmManager)this.GetSystemService(Context.AlarmService);
alarmService.SetRepeating(AlarmType.RtcWakeup, SystemClock.CurrentThreadTimeMillis(), 30 * 1000, restartServicePendingIntent);
NotificationManager notificationManager =
(NotificationManager)GetSystemService(NotificationService);
Notification.Builder builder = new Notification.Builder(this);
Intent notificationIntent = new Intent(this, typeof(MainActivity));
PendingIntent contentIntent = PendingIntent.GetActivity(this, 0, notificationIntent, 0);
//set
builder.SetContentIntent(contentIntent);
builder.SetSmallIcon(Resource.Mipmap.ic_launcher);
builder.SetContentText("Contents");
builder.SetContentTitle("title");
builder.SetAutoCancel(true);
builder.SetDefaults(NotificationDefaults.All);
notificationManager.Notify(1, builder.Build());
base.OnTaskRemoved(rootIntent);
}
}
}
This works also if app is closed and device restarted.

Related

create xamarin process never end

I have a plan and want to periodically check a URL every 5 minutes(NOTIFY CENTER SERVER(Listener)).
My problem: Once the program closes, the process closes
Is it possible that the project will not be shut down if the original program is closed ?
My Code After Changed Worked with : Matcha.BackgroundService
using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Matcha.BackgroundService.Droid;
using Matcha.BackgroundService;
using System.Threading.Tasks;
using Android.Util;
using System.Threading;
using AndroidApp = Android.App.Application;
using Android.Content;
using Android.Support.V4.App;
using Android.Graphics;
namespace Solution.Droid
{
[Activity(Label = "Solution", Icon = "#mipmap/icon", Theme = "#style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
private NotificationManager _manager;
private bool _channelInitialized = false;
public const int _pendingIntentId = 0;
public int _channelID = 10001;
private long _mssageID=0;
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
BackgroundAggregator.Init(this);
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
protected override void OnStart()
{
base.OnStart();
//Register Periodic Tasks
var _notifyTASK = new DevinuxTaskPeriodic(10);
_notifyTASK.DoTask += () =>
{
SendNotify("salam", DateTime.Now.ToString());
};
BackgroundAggregatorService.Add(() => _notifyTASK);
BackgroundAggregatorService.StartBackgroundService();
}
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);
}
public int SendNotify(string title, string message)
{
_mssageID++;
if (!_channelInitialized)
{
CreateNotificationChannel();
}
Intent intent = new Intent(AndroidApp.Context, typeof(MainActivity));
PendingIntent pendingIntent = PendingIntent.GetActivity(AndroidApp.Context, _pendingIntentId, intent, PendingIntentFlags.OneShot);
NotificationCompat.Builder builder = new NotificationCompat.Builder(AndroidApp.Context, _channelID.ToString())
.SetContentIntent(pendingIntent)
.SetContentTitle(title)
.SetContentText(message)
.SetLargeIcon(BitmapFactory.DecodeResource(AndroidApp.Context.Resources, Resource.Drawable.notification_template_icon_bg))
.SetSmallIcon(Resource.Drawable.notification_template_icon_bg)
.SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate);
Notification notification = builder.Build();
_manager.Notify((int)_mssageID, notification);
return (int)_mssageID;
}
void CreateNotificationChannel()
{
_manager = (NotificationManager)AndroidApp.Context.GetSystemService(AndroidApp.NotificationService);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
var channelNameJava = new Java.Lang.String("Solution");
var channel = new NotificationChannel(_channelID.ToString(), channelNameJava, NotificationImportance.Default)
{
Description = "My Company Notify Camp."
};
_manager.CreateNotificationChannel(channel);
}
_channelInitialized = true;
}
public class DevinuxTaskPeriodic : IPeriodicTask
{
public bool use { set; get; } = false;
public delegate void DoArgs();
public event DoArgs DoTask;
public DevinuxTaskPeriodic(int seconds)
{
Interval = TimeSpan.FromSeconds(seconds);
}
public TimeSpan Interval { get; set; }
public Task<bool> StartJob()
{
if (!use)
{
Timer tmr = new Timer((o) => {
if (DoTask != null)
{
DoTask();
}
}, null, 0, (int)Interval.TotalSeconds*1000);
}
use = true;
return new Task<bool>(() => true);
}
}
}
}
Yes, it is possible to run processes even when the original program/app is not in the foreground.
You are entering the territory of "backgrounding" which is less simple to do. There isn't an inbuilt/official way of performing backgrounding using Xamarin.Forms, so you will have to either create a dependency service (shown here), or try using Shiny.
If you follow the dependency services route, you just need to follow the official iOS & Android tutorials and implement them in your Native project. Note that if you only need a periodic alarm, Android provides a simpler Alarm/PowerManager that you can use.

Open a Activity from a service

I have a service on my app that starts one activity. When the app is on back or foreground the service can open the activity, but when the app is dead and the service try to open it, nothing hapen. So, the service neeeds to start the activity even that the app is dead. Can somebody help?
That's the class of service:
[Service]
public class TimestampService : Service
{
static readonly string TAG = typeof(TimestampService).FullName;
static readonly int DELAY_BETWEEN_LOG_MESSAGES = 5000; // milliseconds
static readonly int NOTIFICATION_ID = 10000;
static readonly string SERVICE_STARTED_KEY = "has_service_been_started";
UtcTimestamper timestamper;
int hourExecute = 0 ;
int minutoExecute = 0;
bool isStarted;
Handler handler;
Action runnable;
bool opened = false;
public override void OnCreate()
{
base.OnCreate();
Log.Info(TAG, "OnCreate: the service is initializing.");
this.timestamper = new UtcTimestamper();
this.handler = new Handler();
// This Action is only for demonstration purposes.
this.runnable = new Action(() =>
{
this.OpenAtivity();
});
}
private void OpenAtivity()
{
if (this.timestamper != null)
{
Log.Debug(TAG, this.timestamper.GetFormattedTimestamp());
if (DateTime.Now.Hour == 18 && DateTime.Now.Minute == 16 && !this.opened)
{
this.TrySend();
this.opened = true;
}
//}
this.handler.PostDelayed(this.runnable, DELAY_BETWEEN_LOG_MESSAGES);
}
}
private void TrySend()
{
this.StartForeground(0, new Notification());
/*
Intent launchIntent = Application.Context.PackageManager.GetLaunchIntentForPackage(Application.Context.PackageName);
Intent mainIntent = Intent.MakeRestartActivityTask(launchIntent.Component);
StartActivity(mainIntent);
*/
Intent launchIntent = Application.Context.PackageManager.GetLaunchIntentForPackage(Application.Context.PackageName);
Intent mainIntent = Intent.MakeRestartActivityTask(launchIntent.Component);
mainIntent.AddFlags(ActivityFlags.NewTask);
mainIntent.PutExtra(SERVICE_STARTED_KEY, true);
this.StartActivity(mainIntent);
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
if (isStarted)
{
Log.Info(TAG, "OnStartCommand: This service has already been started.");
}
else
{
Log.Info(TAG, "OnStartCommand: The service is starting.");
handler.PostDelayed(runnable, DELAY_BETWEEN_LOG_MESSAGES);
isStarted = true;
}
handler.PostDelayed(runnable, DELAY_BETWEEN_LOG_MESSAGES);
// This tells Android not to restart the service if it is killed to reclaim resources.
return StartCommandResult.NotSticky;
}
public override IBinder OnBind(Intent intent)
{
// Return null because this is a pure started service. A hybrid service would return a binder that would
// allow access to the GetFormattedStamp() method.
return null;
}
public override void OnDestroy()
{
// We need to shut things down.
Log.Debug(TAG, GetFormattedTimestamp());
Log.Info(TAG, "OnDestroy: The started service is shutting down.");
// Stop the handler.
handler.RemoveCallbacks(runnable);
// Remove the notification from the status bar.
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.Cancel(NOTIFICATION_ID);
timestamper = null;
isStarted = false;
base.OnDestroy();
}
/// <summary>
/// This method will return a formatted timestamp to the client.
/// </summary>
/// <returns>A string that details what time the service started and how long it has been running.</returns>
string GetFormattedTimestamp()
{
return timestamper?.GetFormattedTimestamp();
}
}
I gave up by use service to do this job. To solve the problem I created an alarm that opens the app when it's dead.
This is a part of calling the service:
private void StartAlarm()
{
AlarmManager manager = (AlarmManager)GetSystemService(AlarmService);
Intent minhaIntent;
PendingIntent pendingIntent;
minhaIntent = new Intent(this, new BroadCast.OpenApp(this).Class);
pendingIntent = PendingIntent.GetBroadcast(this, 0, minhaIntent, 0);
int hour = 12;
int minute = 25;
DateTime time = new DateTime(2020, 11, 19, hour, minute, 0, DateTimeKind.Utc);
manager.SetRepeating(AlarmType.RtcWakeup, (long)time.ToUniversalTime().Subtract(time).TotalMilliseconds, 60 * 1000, pendingIntent);
}
And this is my class:
public override void OnReceive(Context context, Intent intent)
{
if (this.executed)
return;
else if (DateTime.Now.Minute != 55)
return;
this.executed = true;
Toast.MakeText(context, "Descarregando", ToastLength.Short).Show();
Intent mainIntent = Intent.MakeRestartActivityTask(launchIntent.Component);
mainIntent.PutExtra(SERVICE_STARTED_KEY, true);
context.StartActivity(mainIntent);
}

Android Service binding with MvvmCross

I am developing xamarin.Android app in MvvmCross. I want to call a service even when the App is backgrounded and a user is logged in. The problem is, I want to call this service within every say 2 hours whether the app is in foreground or background, just the user of the App needs to be logged in.
Intent loggedintent = new Intent(this,typeof(DeviceLoginHelper));
loggedintent.PutExtra("LoggedIn", true);
StartService(loggedintent);
I have written an android service:
[Service]
public class DeviceLoginHelper : IntentService
{
protected override void OnHandleIntent(Intent intent)
{
try
{
if(intent.HasExtra("LoggedIn"))
{
}
}
catch(Exception ex) { }
}
}
But how can I implement a timer? Where do I initialise and handle event to the timer. And when timer is elapsed when should I call ?
public override void OnDestroy()
{
try
{
base.OnDestroy();
}
catch(Exception ex){}
}
and when a user loges out i want to stop this service. Where do I put the call StopService() in MvvmCross
I would not use a Timer. Instead you should configure the AlarmManager.
[BroadcastReceiver]
public class AlarmReceiver : BroadcastReceiver
{
private static AlarmManager alarmMgr;
private static PendingIntent alarmIntent;
public const int NOTIFICATION_ID = 1;
public const int IDLE_TIME_MS = 30 * 1000; // 30-seconds (update here)
private NotificationManager mNotificationManager;
Notification.Builder builder;
public override void OnReceive(Context context, Intent intent)
{
// Do something when alarm triggers (here I'm building notification)
BuildNotification(context);
// reschedule alarm
ScheduleAlarm(IDLE_TIME_MS);
}
public static Context ApplicationContext { get; set; }
public static void ScheduleAlarm(int milliseconds)
{
if (milliseconds == 0) return;
alarmMgr = (AlarmManager)ApplicationContext.GetSystemService(Context.AlarmService);
var intent = new Intent(ApplicationContext, typeof(AlarmReceiver));
alarmIntent = PendingIntent.GetBroadcast(ApplicationContext, 0, intent, 0);
alarmMgr.Set(AlarmType.ElapsedRealtimeWakeup,
SystemClock.ElapsedRealtime() + milliseconds, alarmIntent);
}
private void BuildNotification(Context context)
{
mNotificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
var contentIntent = PendingIntent.GetActivity(context, 0, new Intent(context, typeof(MainView)), 0);
var message = $"Time is up";
var mBuilder = new Notification.Builder(context)
.SetAutoCancel(true)
.SetPriority(NotificationCompat.PriorityMax)
.SetDefaults(NotificationDefaults.All)
.SetContentTitle("Time is up")
.SetStyle(new Notification.BigTextStyle()
.BigText(message))
.SetContentText(message)
.SetSmallIcon(Resource.Drawable.ic_launcher);
mBuilder.SetContentIntent(contentIntent);
mNotificationManager.Notify(NOTIFICATION_ID, mBuilder.Build());
}
}
In your startup code, simply call:
AlarmReceiver.ApplicationContext = context;
AlarmReceiver.ScheduleAlarm(timeInMs);

android media picker from Xamarin forms

I am writting an application with xamarin forms for iOS and Android.
I want to pick a photo from image gallery.
I have created an android specific static helper:
var i = new Intent();
i.SetType("*/*");
Forms.Context.StartActivity(Intent.CreateChooser(i, ""));
But i have no way to get the selected picture bytes.
I have seen on android tutorials i should implement onActivityResult, but i am not on an activity, this is a specific static call...
Thanks
Via a Form's dependency service:
Create your dependency interface (IMediaPicker)
Create a Activity subclass (MediaChooserActivityProxy) that will act as your Intent.ActionPick proxy
In your Xamarin.Android implementation of the IMediaPicker, use a AutoResetEvent to convert the Android StartActivityForResult / OnActivityResult callback to an await-able synchronous flow.
Dependency Service Interace:
public interface IMediaPicker
{
Task<string> ChooseAFileAsync();
}
Android Dependency Implementation:
public class MediaPicker : IMediaPicker
{
public static string filePickedPath;
public static AutoResetEvent waitHandle;
public async Task<string> ChooseAFileAsync()
{
waitHandle = new AutoResetEvent(false);
filePickedPath = "";
Forms.Context.StartActivity(typeof(MediaChooserActivityProxy));
await Task.Run(() => waitHandle.WaitOne());
return filePickedPath;
}
}
The Proxy/Pseudo Activity to capture OnActivityResult:
public class MediaChooserActivityProxy : Activity
{
const string mimeType = "image/*";
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
var intent = new Intent(Intent.ActionPick);
intent.SetType(mimeType);
if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
{
intent.PutExtra(Intent.ExtraMimeTypes, mimeType);
}
StartActivityForResult(Intent.CreateChooser(intent, "StackOverflow"), 73);
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
if (requestCode == 73)
if (resultCode == Result.Ok)
{
string[] filePathColumn = { MediaStore.Images.ImageColumns.Data };
var cursor = ContentResolver.Query(data.Data, filePathColumn, null, null, null);
cursor.MoveToFirst();
var colummIndex = cursor.GetColumnIndex(filePathColumn[0]);
MediaPicker.filePickedPath = cursor.GetString(colummIndex);
}
MediaPicker.waitHandle.Set();
Finish();
}
}
Note: This can be implemented on the MainActivity/FormsAppCompatActivity to avoid this additional Activity if desired...
Usage:
var filePath = await DependencyService.Get<IMediaPicker>().ChooseAFileAsync();
System.Diagnostics.Debug.WriteLine(filePath);

How can we make app in kiosk mode using xamarin?

I'm creating new app using xamarin. I have already completed some part using some sample codes. I'm able to disable back buttons, volume buttons and power button.
But when trying to disable home button I'm getting error on debugging.
I'm following this link,Kiosk mode in Andriod.
But when trying to disable home button I'm getting error on debugging.
Since you didn't post your code and your error message, we don't know what happened, I just tried to create such a sample followed the blog your posted and it works fine by my side.
Here is the service:
namespace KioskModeAndroid
{
[Service]
[IntentFilter(new[] { "KioskModeAndroid.KioskService" })]
public class KioskService : Service
{
private static long INTERVAL = Java.Util.Concurrent.TimeUnit.Seconds.ToMillis(2);
private static string TAG = typeof(KioskService).Name;
private static string PREF_KIOSK_MODE = "pref_kiosk_mode";
private Thread t = null;
private Context ctx = null;
private bool running = false;
public override void OnDestroy()
{
Log.Info(TAG, "Stopping service 'KioskService'");
running = false;
base.OnDestroy();
}
[return: GeneratedEnum]
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
{
Log.Info(TAG, "Starting service 'KioskService'");
running = true;
ctx = this;
t = new Thread(() =>
{
while (running)
{
handleKioskMode();
Thread.Sleep(INTERVAL);
}
StopSelf();
});
t.Start();
return StartCommandResult.NotSticky;
}
private void handleKioskMode()
{
if (isKioskModeActive(ctx))
{
}
if (isInBackground())
{
restoreApp();
}
}
private bool isKioskModeActive(Context context)
{
var sp = PreferenceManager.GetDefaultSharedPreferences(context);
return sp.GetBoolean(PREF_KIOSK_MODE, false);
}
private bool isInBackground()
{
var am = ctx.GetSystemService(Context.ActivityService) as ActivityManager;
var processes = am.RunningAppProcesses;
foreach (var process in processes)
{
if (process.Importance == ActivityManager.RunningAppProcessInfo.ImportanceForeground)
{
foreach (var activeprocess in process.PkgList)
{
if (activeprocess == ctx.PackageName)
return false;
}
}
}
return true;
}
private void restoreApp()
{
Intent i = new Intent(ctx, typeof(MainActivity));
i.AddFlags(ActivityFlags.NewTask);
ctx.StartActivity(i);
}
public override IBinder OnBind(Intent intent)
{
return null;
}
}
}
I started this service in the OnCreate of MainActivity:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
StartService(new Intent(this, typeof(KioskService)));
}

Resources