Hide app UI after starting Foreground Service - xamarin

I'm developing an Android app with Xamarin (tried Maui and Xamarin.Forms but can't figure out this problem so now trying Xamarin.Android but still struggling). So far I've discovered that my app should invoke a "foreground service", as it needs to constantly monitor the accelerometer. This I can do.
The bit I'm struggling with now is, after invoking the foreground service, how to completely hide the app UI so the foreground service continues to run in the "background"?

Customize a Service and override the method OnStartCommand , use StartForeground method to register Foreground Service, here you need to create a notification to display what you want to monitor.
var notification = new Notification.Builder(this)
.SetContentTitle(Resources.GetString(Resource.String.app_name))
.SetContentText(Resources.GetString(Resource.String.notification_text))
.SetSmallIcon(Resource.Drawable.ic_stat_name)
.SetContentIntent(BuildIntentToShowMainActivity())
.SetOngoing(true)
.AddAction(BuildRestartTimerAction())
.AddAction(BuildStopServiceAction())
.Build();
// Enlist this instance of the service as a foreground service
StartForeground(Constants.SERVICE_RUNNING_NOTIFICATION_ID, notification);
Use StartForegroundService instead of StartService with the service intent(On Android 8.0 and higher ).
Docs : https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/services/foreground-services
Sample : https://learn.microsoft.com/en-us/samples/xamarin/monodroid-samples/applicationfundamentals-servicesamples-foregroundservicedemo/

Related

Xamarin.Forms UI not responding

I'm working on a Xamarin.Forms app using the Prism library. The app has a phone component to it with integration to CallKit and a SIP library. When the device is locked, a call can be received, which results in the native phone UI for iOS. User can answer the call and hangup. Within the app, there is a Xamarin.Forms page to handle calls. There are events from the iOS service that deals with the calls to interact with the "shared project". The issue that I am running in to is that when the user unlocks the device and returns to the app, the UI is non-responsive.
Scenario:
User starts app and logs in.
User locks device
Incoming call received
User answers call
Behind the scenes (i.e. native phone UI), the call service communicates with the "shared project" to display a Call Screen in the app. Since the phone is locked, the user will not see this now.
User hangs up.
When user hangs up, the call service communicates with the "shared project" that the call has been terminated and to return to the previous screen.
User then unlocks the screen
App is on same screen as when user locked device, but UI is not responsive.
To navigate to the call page, I am doing:
await NavigationService.NavigateAsync(targetPage, null, useModalNavigation: null, animated: animated);
and to return to previous page:
await NavigationService.GoBackAsync(animated: false, parameters: parameters);
NavigationService is of type INavigationService from Prism.
The essential question is: what would make the navigation appear to work, but result in a non-responsive UI? I've found in the past that I need to do the navigation on the main thread. Is there anything else I need to look for?
What I understood that you are looking for a method just like the OnAppearing method of forms page.
You need to implement interface INavigatedAware to your ViewModel. By doing this, 2 methods will be added to your ViewModel "NavigatedTo" and "NavigatedFrom". You need to put your logic into the "NavigatedTo" method. This method will be triggered whenever that particular page will appear on the screen.

App close event in Ionic Windows 8.1 App?

I'm building a Windows 8.1 App using Ionic 3. In the app, the user would have to type in a lot of stuff. I want to avoid accidental closure of the app by clicking on 'X' button at the top right corner or by Alt+F4. When the user tries to close the app, is there a way to prevent the default behaviour of closing the app and instead ask a confirmation?
You must override the onbackbutton event to do this.
document.addEventListener('backbutton', function (evt) {
/* BackButton pressed: do nothing */
return;
}, false);
Do NOT use the method shown on that page to exit the app:
throw new Error('Exit'); // This will suspend the app BUT the store will reject it
If you do this to exit your app, it will be rejected by the Microsoft store. To exit the app, remove the event listener and let the backbutton event suspend the app normally.
There's no special event to indicate that the user has closed an app.
App Lifecycle docs for Windows Store Apps.
https://learn.microsoft.com/en-us/previous-versions/windows/apps/hh464925(v=win.10)?redirectedfrom=MSDN#app-close
When the user closes the app or moves the app to background, it enters into a Suspended State. Windows recommends saving user state when the app enters such a state. You can save data in that state thereby achieving the overall goal.

Android Watch/Phone MessageApi

I am working on an app for Android Watch that requires the mobile app to be on or in background. I tried using the MessageApi in order to communicate with the app on the phone, but even if the app is not on, i get a message back like it is on.
What would you suggest to check whether the phone app is running (doesnt matter whether it is foreground/background) before i enable the activity on the Android Watch?
Thanks for the help.
If you have registered a WearableListenerService in your mobile app, that will be invoked by the framework even if your app is not running (and will be invoked if your app is running, too). If, however, you are not using that service, then to receive messages or notification on changes in the wear network in your mobile app, you would need to register one or more callback listeners for the type of messages/data that you are interested in. In that case, that means if your app is not running, you will not be getting those callbacks on your mobile app. To use this, however, be sure to manage the lifecycle of your listeners in your mobile app according to your needs. For example, one can register for such listeners in onResume() of an activity and unregister in onPause(). The consequence of that is if your activity loses focus (but still alive), that activity will not receive the callback (even though as an instance it is still running). You may want to define an Application instance for your app and use that to listen for callbacks but, again, it depends on the exact nature of your needs and app.
You can search for nodes and parse them for needed result, or to check if you have a nearBy node
private Collection<String> getNodes() {
HashSet <String>results = new HashSet<String>();
NodeApi.GetConnectedNodesResult nodes =
Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
for (Node node : nodes.getNodes()) {
results.add(node.getId());
}
return results;
}

How to use Background agent to send toast notification?

I am working on windows development & I am displaying the items on the emulator by using a web service.
Now I want to send a toast notification when an item added to the service. Already I worked with push notifications. But It’s generating push channel URL when app is initialized.
I don’t want to use this channel URL. So I tried Background Agent & Shell Toast to send the notification. Its working fine for static data. How can I add dynamic data to back ground agent which is coming from web service?
One more thing is, there are two things like PeriodicTask & ResourceIntensiveTask. How can I use this to send a toast notification by using Background Agent?
You can refer to this Tutorial From Nokia
http://developer.nokia.com/community/wiki/How_to_create_simple_Background_Agent_in_Windows_Phone
and this from CodeProject
http://www.codeproject.com/Articles/656654/Windows-Phone-Background-Agent-Part-Periodic-T

Notification Service for Blackberry OS 4.5 application

I am developing an application similar to email application.Whenever new message is received my notification service should indicate change to user by updating icon,also, the notification service should continuosly listen to server for incoming events.
I am developing in os version 4.5.
There is no ApplicationIndicator in RIM OS < 4.6 (see bb forum thread)
you have several options to notify the
user (none of them is the indicator,
unfortunately). you can:
play a sound
trigger the vibration
trigger a customizable notification (so the user can decide)
change the application icon
paint a text on the application icon
change the application title
how to show notification icon in v4.2 to v4.5 versions
Play sound with with Alert.startAudio
Change application icon with HomeScreen.updateIcon
Change app name with HomeScreen.setName
Trigger vibration with Alert.startVibrate
And yes, to exchange data between two applications it's better to use GlobalEventListener

Resources