wxr
2020-06-15 b8e94316e41eba72d927d5ca7d931b26139ee8ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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;
        }
    }
}