Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Good Day Everyone 

i am tracking the users status on the app from the events in app.cs , i use the following tracking like this 


OnStart

protected override async void OnStart()
{
   await Task.Run(() => OnlineStatuspdate(1));
}


OnSleep

protected override async void OnSleep()
               {
                           await Task.Run(() => OnlineStatuspdate(0));
               }



OnResume

protected override async void OnResume()
     {
       await Task.Run(() => OnlineStatuspdate(1));
     }


after all this the status of some users remain online even though the users closed the app. can one advice on this.

Thanks 


What I have tried:

Checked different methods and they dont work
Posted
Updated 16-Oct-22 20:30pm
v6
Comments
Member 15627495 16-Oct-22 10:36am    
use a 'timer' , it's coding an "afk" system. ( "away from keyboard" you know )

when the user is using the app, the afk is off ... and when one minute spent without use of the app, afk become ON.



about your code : onstart , and onresume are equals .
Dave Kreskowiak 16-Oct-22 11:36am    
That depends on what you mean by "online" and "offline". Are you talking about using your app? Connected to internet? Connected to WiFi? You're going to have to be a lot more specific if you want an answer to this.
Vimalsoft(Pty) Ltd 16-Oct-22 12:15pm    
That is correct , if the user is connected to the internet or closed the app the app is not closed just minimized.
Dave Kreskowiak 16-Oct-22 17:06pm    
You might make something work with the NetworkChange Class (System.Net.NetworkInformation)[^]
[no name] 16-Oct-22 12:21pm    
"OnSleep" is not the same as a "shutdown" event ... for which there is none.

1 solution

You need to go through the followings:
The Connectivity class lets you monitor for changes in the device's network conditions, check the current network access, and how it is currently connected: Xamarin.Essentials: Connectivity - Xamarin | Microsoft Learn[^]

High level steps from it:
1. Provide AccessNetworkState permission
2. Add a reference to Xamarin.Essentials in your class
3. With above, you can check network access as:
C#
var current = Connectivity.NetworkAccess;

if (current == NetworkAccess.Internet)
{
    // Connection to internet is available
}

You can also setup for any connectivety change trigger as:
C#
public class ConnectivityTest
{
    public ConnectivityTest()
    {
        // Register for connectivity changes, be sure to unsubscribe when finished
        Connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged;
    }

    void Connectivity_ConnectivityChanged(object sender, ConnectivityChangedEventArgs e)
    {
        var access = e.NetworkAccess;
        var profiles = e.ConnectionProfiles;
    }
}


More details on how to use Xamarin.Essentials: Easily Check Mobile Device Connectivity with Xamarin.Essentials - Xamarin Blog[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900