using System;
|
using Android;
|
using Android.App;
|
using Android.Content;
|
using Android.Content.PM;
|
using Android.Locations;
|
using Android.OS;
|
using Android.Runtime;
|
using Android.Widget;
|
|
namespace Shared
|
{
|
/// <summary>
|
/// GPS 后台定位
|
/// </summary>
|
[Service ]
|
public class GPSLocationService:Service
|
{
|
public override IBinder OnBind(Intent intent)
|
{
|
return null;
|
}
|
|
public override void OnCreate()
|
{
|
base.OnCreate();
|
if (Shared.Application.IsGpsEnable)
|
{
|
(Shared.Application.Activity as BaseActivity).SetGPSLocationPermission(result =>
|
{
|
if (result)
|
{
|
initLocation();
|
notification();
|
}
|
});
|
}
|
}
|
|
bool IsStopForeground = false;
|
public override void OnDestroy() {
|
|
|
if (IsStopForeground)
|
{
|
if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.O)
|
{
|
IsStopForeground = false;
|
StopForeground(true);
|
|
}
|
}
|
|
base.OnDestroy();
|
}
|
|
|
[Obsolete]
|
void notification()
|
{
|
if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.O)
|
{
|
IsStopForeground = true;
|
var mNotificationUtils = new NotificationUtils(this);
|
var notificationIntent = new Intent(this, typeof(BaseActivity));
|
var pendingIntent = PendingIntent.GetActivity(this, 3,
|
notificationIntent, PendingIntentFlags.CancelCurrent);
|
|
var builder = mNotificationUtils.getAndroidChannelNotification
|
(Language.CurrentLanguage == "Chinese"?"获取定位信息":"Get Location Information", "", pendingIntent);
|
StartForeground(1, builder.Build());//id must not be 0,即禁止是0
|
}
|
}
|
|
class NotificationUtils :Notification.Builder
|
{
|
|
private NotificationManager mManager;
|
public static string channelId = "85521566";
|
public static string channelName = "HDLChanneName";
|
private Context context;
|
|
[Obsolete]
|
public NotificationUtils(Context context) : base(context)
|
{
|
this.context = context;
|
createChannels();
|
}
|
|
public void createChannels()
|
{
|
// create android channel
|
if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.O)
|
{
|
var androidChannel = new NotificationChannel(channelId,
|
context.PackageName, NotificationImportance.High);
|
// Sets whether notifications posted to this channel should display notification lights
|
androidChannel.EnableLights(false);
|
// Sets whether notification posted to this channel should vibrate.
|
androidChannel.EnableVibration(false);
|
androidChannel.SetVibrationPattern(new long[] { 0 });
|
// Sets the notification light color for notifications posted to this channel
|
androidChannel.LightColor = 0x38FF1234;
|
// Sets whether notifications posted to this channel appear on the lockscreen or not
|
androidChannel.LockscreenVisibility = NotificationVisibility.Private;
|
|
getManager().CreateNotificationChannel(androidChannel);
|
}
|
|
}
|
|
private NotificationManager getManager()
|
{
|
if (mManager == null)
|
{
|
mManager = (NotificationManager)context.GetSystemService(NotificationService);
|
}
|
return mManager;
|
}
|
|
public Notification.Builder getAndroidChannelNotification(string title, string body, PendingIntent pendingIntent)
|
{
|
if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.O)
|
{
|
return new Notification.Builder(context, channelId)
|
.SetContentTitle(title)
|
.SetContentText(body)
|
.SetContentIntent(pendingIntent)
|
.SetSmallIcon(Android.Graphics.Drawables.Icon.CreateWithBitmap( ((Android.Graphics.Drawables.BitmapDrawable)context.PackageManager.GetApplicationIcon(context.PackageManager.GetApplicationInfo(context.PackageName,0))).Bitmap))//android.R.drawable.stat_notify_more
|
//.SetWhen(System.DateTime.Now.Ticks)
|
.SetOngoing(true)
|
.SetChannelId(channelId)
|
.SetAutoCancel(true);
|
|
}
|
else
|
{
|
return new Notification.Builder(context, channelId);
|
}
|
|
}
|
}
|
|
|
#region GPS定位
|
|
LocationManager locationManager;
|
/// <summary>
|
/// 初始化定位服务
|
/// </summary>
|
void initLocation()
|
{
|
//GPS初始化
|
locationManager = GetSystemService(LocationService).JavaCast<LocationManager>();
|
// 判断GPS是否正常启动
|
if (!locationManager.IsProviderEnabled(LocationManager.GpsProvider))
|
{
|
if (Language.CurrentLanguage == "Chinese")
|
{
|
Toast.MakeText(this, "请打开GPS...", ToastLength.Long).Show();
|
}
|
else
|
{
|
Toast.MakeText(this, "Please open GPS...", ToastLength.Long).Show();
|
}
|
}
|
else
|
{
|
locationManager.RequestLocationUpdates(LocationManager.GpsProvider, 60 * 1000, 1000, new locationChange { });
|
}
|
}
|
|
class locationChange : Java.Lang.Object, Android.Locations.ILocationListener
|
{
|
public void OnLocationChanged(Location location)
|
{
|
Shared.Application.LocationAction?.Invoke(location.Longitude, location.Latitude);
|
|
}
|
|
public void OnProviderDisabled(string provider)
|
{
|
}
|
|
public void OnProviderEnabled(string provider)
|
{
|
}
|
|
public void OnStatusChanged(string provider, [GeneratedEnum] Availability status, Bundle extras)
|
{
|
}
|
}
|
|
public void OnLocationChanged(Location location)
|
{
|
}
|
|
public void OnProviderDisabled(string provider)
|
{
|
|
}
|
|
public void OnProviderEnabled(string provider)
|
{
|
|
}
|
|
public void OnStatusChanged(string provider, [GeneratedEnum] Availability status, Bundle extras)
|
{
|
|
}
|
#endregion
|
|
public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
|
{
|
return StartCommandResult.Sticky;
|
}
|
}
|
}
|