Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
Updated Sample. Related to #100
Browse files Browse the repository at this point in the history
* Changed minSDK, targetSDK, compileSDK
* Add runtime permission check
* Use AppCompatActivity
  • Loading branch information
topgenorth committed Aug 1, 2018
1 parent 01d8d66 commit 8a0c32c
Show file tree
Hide file tree
Showing 12 changed files with 530 additions and 431 deletions.
77 changes: 54 additions & 23 deletions BackgroundLocationDemo/BackgroundLocationDemo.sln
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27703.2042
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Location.iOS", "Location.iOS\Location.iOS.csproj", "{D2D08A68-F3B6-4730-96F9-9053CD76B2A5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Location.Droid", "Location.Android\Location.Droid.csproj", "{89E83FD6-327A-4976-B720-E28B0970F516}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Ad-Hoc|Any CPU = Ad-Hoc|Any CPU
Ad-Hoc|iPhone = Ad-Hoc|iPhone
Ad-Hoc|iPhoneSimulator = Ad-Hoc|iPhoneSimulator
AppStore|Any CPU = AppStore|Any CPU
AppStore|iPhone = AppStore|iPhone
AppStore|iPhoneSimulator = AppStore|iPhoneSimulator
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
Debug|iPhoneSimulator = Debug|iPhoneSimulator
Release|iPhoneSimulator = Release|iPhoneSimulator
Debug|iPhone = Debug|iPhone
Debug|iPhoneSimulator = Debug|iPhoneSimulator
Release|Any CPU = Release|Any CPU
Release|iPhone = Release|iPhone
Ad-Hoc|iPhone = Ad-Hoc|iPhone
AppStore|iPhone = AppStore|iPhone
Release|iPhoneSimulator = Release|iPhoneSimulator
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{89E83FD6-327A-4976-B720-E28B0970F516}.Ad-Hoc|iPhone.ActiveCfg = Release|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Ad-Hoc|iPhone.Build.0 = Release|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.AppStore|iPhone.ActiveCfg = Release|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.AppStore|iPhone.Build.0 = Release|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Debug|Any CPU.Build.0 = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Debug|iPhone.Build.0 = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Release|Any CPU.ActiveCfg = Release|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Release|Any CPU.Build.0 = Release|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Release|iPhone.ActiveCfg = Release|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Release|iPhone.Build.0 = Release|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{D2D08A68-F3B6-4730-96F9-9053CD76B2A5}.Ad-Hoc|Any CPU.ActiveCfg = Ad-Hoc|iPhoneSimulator
{D2D08A68-F3B6-4730-96F9-9053CD76B2A5}.Ad-Hoc|iPhone.ActiveCfg = Ad-Hoc|iPhone
{D2D08A68-F3B6-4730-96F9-9053CD76B2A5}.Ad-Hoc|iPhone.Build.0 = Ad-Hoc|iPhone
{D2D08A68-F3B6-4730-96F9-9053CD76B2A5}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Ad-Hoc|iPhoneSimulator
{D2D08A68-F3B6-4730-96F9-9053CD76B2A5}.Ad-Hoc|iPhoneSimulator.Build.0 = Ad-Hoc|iPhoneSimulator
{D2D08A68-F3B6-4730-96F9-9053CD76B2A5}.AppStore|Any CPU.ActiveCfg = AppStore|iPhoneSimulator
{D2D08A68-F3B6-4730-96F9-9053CD76B2A5}.AppStore|iPhone.ActiveCfg = AppStore|iPhone
{D2D08A68-F3B6-4730-96F9-9053CD76B2A5}.AppStore|iPhone.Build.0 = AppStore|iPhone
{D2D08A68-F3B6-4730-96F9-9053CD76B2A5}.AppStore|iPhoneSimulator.ActiveCfg = AppStore|iPhoneSimulator
{D2D08A68-F3B6-4730-96F9-9053CD76B2A5}.AppStore|iPhoneSimulator.Build.0 = AppStore|iPhoneSimulator
{D2D08A68-F3B6-4730-96F9-9053CD76B2A5}.Debug|Any CPU.ActiveCfg = Debug|iPhoneSimulator
{D2D08A68-F3B6-4730-96F9-9053CD76B2A5}.Debug|Any CPU.Build.0 = Debug|iPhoneSimulator
{D2D08A68-F3B6-4730-96F9-9053CD76B2A5}.Debug|iPhone.ActiveCfg = Debug|iPhone
Expand All @@ -49,6 +45,41 @@ Global
{D2D08A68-F3B6-4730-96F9-9053CD76B2A5}.Release|iPhone.Build.0 = Release|iPhone
{D2D08A68-F3B6-4730-96F9-9053CD76B2A5}.Release|iPhoneSimulator.ActiveCfg = Release|iPhoneSimulator
{D2D08A68-F3B6-4730-96F9-9053CD76B2A5}.Release|iPhoneSimulator.Build.0 = Release|iPhoneSimulator
{89E83FD6-327A-4976-B720-E28B0970F516}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Ad-Hoc|Any CPU.Deploy.0 = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Ad-Hoc|iPhone.ActiveCfg = Release|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Ad-Hoc|iPhone.Build.0 = Release|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Ad-Hoc|iPhoneSimulator.Build.0 = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Ad-Hoc|iPhoneSimulator.Deploy.0 = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.AppStore|Any CPU.Build.0 = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.AppStore|Any CPU.Deploy.0 = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.AppStore|iPhone.ActiveCfg = Release|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.AppStore|iPhone.Build.0 = Release|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.AppStore|iPhoneSimulator.Build.0 = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.AppStore|iPhoneSimulator.Deploy.0 = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Debug|Any CPU.Build.0 = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Debug|iPhone.Build.0 = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Release|Any CPU.ActiveCfg = Release|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Release|Any CPU.Build.0 = Release|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Release|iPhone.ActiveCfg = Release|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Release|iPhone.Build.0 = Release|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{89E83FD6-327A-4976-B720-E28B0970F516}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F4624114-C8F8-4D9B-B634-5315C81F91D7}
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = Location.iOS\Location.iOS.csproj
Expand Down
153 changes: 74 additions & 79 deletions BackgroundLocationDemo/location.Android/App.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Threading;
using System.Threading.Tasks;

using Android.App;
Expand All @@ -9,108 +8,104 @@

using Location.Droid.Services;

/// <summary>
/// Singleton class for Application wide objects.
/// </summary>
namespace Location.Droid
{
public class App
{
// events
public event EventHandler<ServiceConnectedEventArgs> LocationServiceConnected = delegate {};

// declarations
protected readonly string logTag = "App";
protected static LocationServiceConnection locationServiceConnection;

// properties

public static App Current
{
get { return current; }
} private static App current;

public LocationService LocationService
{
get {
if (locationServiceConnection.Binder == null)
throw new Exception ("Service not bound yet");
// note that we use the ServiceConnection to get the Binder, and the Binder to get the Service here
return locationServiceConnection.Binder.Service;
}
}

#region Application context

static App ()
{
current = new App();
}

protected App ()
{
public class App
{
protected static LocationServiceConnection locationServiceConnection;

// declarations
protected readonly string logTag = "App";

static App()
{
Current = new App();
}

protected App()
{
// create a new service connection so we can get a binder to the service
locationServiceConnection = new LocationServiceConnection (null);
locationServiceConnection = new LocationServiceConnection(null);

// this event will fire when the Service connectin in the OnServiceConnected call
locationServiceConnection.ServiceConnected += (object sender, ServiceConnectedEventArgs e) => {
locationServiceConnection.ServiceConnected += (sender, e) =>
{
Log.Debug(logTag, "Service Connected");
// we will use this event to notify MainActivity when to start updating the UI
LocationServiceConnected(this, e);
};
}

// properties

Log.Debug (logTag, "Service Connected");
// we will use this event to notify MainActivity when to start updating the UI
this.LocationServiceConnected ( this, e );
};
}
public static App Current { get; }

public LocationService LocationService
{
get
{
if (locationServiceConnection.Binder == null)
{
throw new Exception("Service not bound yet");
}

// note that we use the ServiceConnection to get the Binder, and the Binder to get the Service here
return locationServiceConnection.Binder.Service;
}
}

// events
public event EventHandler<ServiceConnectedEventArgs> LocationServiceConnected = delegate { };

public static void StartLocationService()
{
// Starting a service like this is blocking, so we want to do it on a background thread
new Task ( () => {

// Start our main service
Log.Debug ("App", "Calling StartService");

// Check if device is running Android 8.0 or higher and if so, use the newer StartForegroundService() method
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
Application.Context.StartForegroundService(new Intent(Application.Context, typeof(LocationService)));
else // For older versions, use the traditional StartService() method
Application.Context.StartService(new Intent(Application.Context, typeof(LocationService)));

// bind our service (Android goes and finds the running service by type, and puts a reference
// on the binder to that service)
// The Intent tells the OS where to find our Service (the Context) and the Type of Service
// we're looking for (LocationService)
Intent locationServiceIntent = new Intent (Android.App.Application.Context, typeof(LocationService));
Log.Debug ("App", "Calling service binding");

// Finally, we can bind to the Service using our Intent and the ServiceConnection we
// created in a previous step.
Android.App.Application.Context.BindService (locationServiceIntent, locationServiceConnection, Bind.AutoCreate);
} ).Start ();
// Starting a service like this is blocking, so we want to do it on a background thread
new Task(() =>
{
// Start our main service
Log.Debug("App", "Calling StartService");

// Check if device is running Android 8.0 or higher and if so, use the newer StartForegroundService() method
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
Application.Context.StartForegroundService(new Intent(Application.Context, typeof(LocationService)));
}
else // For older versions, use the traditional StartService() method
{
Application.Context.StartService(new Intent(Application.Context, typeof(LocationService)));
}

// bind our service (Android goes and finds the running service by type, and puts a reference
// on the binder to that service)
// The Intent tells the OS where to find our Service (the Context) and the Type of Service
// we're looking for (LocationService)
var locationServiceIntent = new Intent(Application.Context, typeof(LocationService));
Log.Debug("App", "Calling service binding");

// Finally, we can bind to the Service using our Intent and the ServiceConnection we
// created in a previous step.
Application.Context.BindService(locationServiceIntent, locationServiceConnection, Bind.AutoCreate);
}).Start();
}

public static void StopLocationService ()
public static void StopLocationService()
{
// Check for nulls in case StartLocationService task has not yet completed.
Log.Debug("App", "StopLocationService");

// Unbind from the LocationService; otherwise, StopSelf (below) will not work:
if (locationServiceConnection != null)
{
{
Log.Debug("App", "Unbinding from LocationService");
Android.App.Application.Context.UnbindService(locationServiceConnection);
Application.Context.UnbindService(locationServiceConnection);
}

// Stop the LocationService:
if (Current.LocationService != null)
{
{
Log.Debug("App", "Stopping the LocationService");
Current.LocationService.StopSelf();
}
}

#endregion

}
}
}


23 changes: 18 additions & 5 deletions BackgroundLocationDemo/location.Android/Location.Droid.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand All @@ -16,7 +16,8 @@
<AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
<AndroidResgenClass>Resource</AndroidResgenClass>
<AssemblyName>location.Androidd</AssemblyName>
<TargetFrameworkVersion>v4.0.3</TargetFrameworkVersion>
<TargetFrameworkVersion>v8.1</TargetFrameworkVersion>
<AndroidUseLatestPlatformSdk>false</AndroidUseLatestPlatformSdk>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>True</DebugSymbols>
Expand Down Expand Up @@ -76,9 +77,21 @@
<AndroidResource Include="..\location.Android\Resources\drawable\ic_launcher.png" />
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\layout\" />
<Folder Include="Resources\drawable\" />
<Folder Include="..\location.Android\Resources\drawable\" />
<PackageReference Include="Xamarin.Android.Support.Compat">
<Version>27.0.2.1</Version>
</PackageReference>
<PackageReference Include="Xamarin.Android.Support.Design">
<Version>27.0.2.1</Version>
</PackageReference>
<PackageReference Include="Xamarin.Android.Support.v7.AppCompat">
<Version>27.0.2.1</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\values\colours.xml" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\values\styles.xml" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
</Project>
Loading

0 comments on commit 8a0c32c

Please sign in to comment.