示例#1
0
            public override int OnStartCommand(Intent intent, int flags, int startId)
            {
                // Tell any local interested parties about the start.
                mLocalBroadcastManager.SendBroadcast(new Intent(ACTION_STARTED));

                // Prepare to do update reports.
                mHandler.RemoveMessages(MSG_UPDATE);
                Message msg = mHandler.ObtainMessage(MSG_UPDATE);

                mHandler.SendMessageDelayed(msg, 1000);
                return(ServiceCompat.START_STICKY);
            }
示例#2
0
        public void OnLocationChanged(Location location)
        {
            var intent = new Intent("testAction");

            intent.PutExtra("x", $"{DateTime.Now.ToString("HH:mm:ss")} - location set");
            //_localBroadcastManager.SendBroadcast(intent);

            _locationService.AddLocation(location);

            _locationUploaderService.UploadLocations();

            Task.Run(() =>
            {
                var model = new LocationChangedModel(location);
                var json  = JsonConvert.SerializeObject(model);

                var intent = new Intent("LocationChanged");
                intent.PutExtra("Location", json);

                _localBroadcastManager.SendBroadcast(intent);
            });

            var settings = _settingsService.GetSettings();

            if (!settings.IsTelegramUploadEnabled)
            {
                return;
            }

            Task.Run(() =>
            {
                try
                {
                    _telegramClient.SendLocation(location.Latitude, location.Longitude);

                    var intentTelegram = new Intent("testAction");
                    intentTelegram.PutExtra("x", $"{DateTime.Now.ToString("HH:mm:ss")} - location sent in Telegram");
                    //_localBroadcastManager.SendBroadcast(intentTelegram);
                }
                catch (Exception ex)
                {
                    var intentTelegram = new Intent("testAction");
                    intentTelegram.PutExtra("x", $"{DateTime.Now.ToString("HH:mm:ss")} - location sending in Telegram failed");
                    _localBroadcastManager.SendBroadcast(intentTelegram);

                    throw;
                }
            });
        }
        private void BroadcastMessage(string message)
        {
            var intent = new Intent(MainActivity.ACTION_NOTIFY);

            intent.PutExtra(MainActivity.EXTRA_MESSAGE, message);
            mBroadcastManager.SendBroadcast(intent);
        }
        public override void OnReceive(Context context, Intent intent)
        {
            Intent responseIntent = new Intent();

            responseIntent.SetAction("LOCAL_ACTION");
            responseIntent.PutExtra("joke", intent.GetStringExtra("joke"));
            LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.GetInstance(context);

            localBroadcastManager.SendBroadcast(responseIntent);
        }
示例#5
0
        public override Result DoWork()
        {
            if (IsRunning)
            {
                return(Result.InvokeSuccess());
            }

            try
            {
                IsRunning = true;

                var settings = _settingsService.GetSettings();

                if (!settings.IsEmailSendingEnabled)
                {
                    return(Result.InvokeSuccess());
                }

                if (!settings.UploadOnMobileNetwork && !CustomNetworkCallback.IsConnected) // TODO
                {
                    return(Result.InvokeSuccess());
                }

                var locations   = _locationService.QueryNotUploadedLocations();
                var networkLogs = _networkLogService.QueryNotUploaded();

                if (!locations.Any() && !networkLogs.Any())
                {
                    return(Result.InvokeSuccess());
                }

                var exporter = new Exporter();

                var json = exporter.CreateJson(locations, networkLogs);

                var stream = new MemoryStream(Encoding.UTF8.GetBytes(json));

                var attachment = new Attachment(stream, "data.json", MediaTypeNames.Application.Json); // TODO: using?

                var emailClient = new EmailClient();

                emailClient.SendMail(
                    settings.SmtpPort,
                    settings.SmtpHost,
                    settings.SmtpUsername,
                    settings.SmtpPassword,
                    settings.SmtpUsername,
                    settings.EmailRecipient,
                    settings.EmailSubject,
                    string.Empty,
                    attachment
                    );

                foreach (var location in locations)
                {
                    location.IsUploaded     = true;
                    location.UploadDateTime = DateTime.UtcNow;
                }

                _locationService.UpdateLocations(locations);

                foreach (var networkLog in networkLogs)
                {
                    networkLog.IsUploaded     = true;
                    networkLog.UploadDateTime = DateTime.UtcNow;
                }

                _networkLogService.Update(networkLogs);

                var intent = new Intent("testAction");
                intent.PutExtra("x", $"{DateTime.Now.ToString("HH:mm:ss")} - email sent: {locations.Count} location(s)");
                _localBroadcastManager.SendBroadcast(intent);

                _locationService.RemoveLocations(settings.KeepLocationsForDays);
                _networkLogService.Remove(settings.KeepLocationsForDays);

                return(Result.InvokeSuccess());
            }
            catch (Exception ex)
            {
                var intent = new Intent("testAction");
                intent.PutExtra("x", $"{DateTime.Now.ToString("HH:mm:ss")} - email sending failed");
                _localBroadcastManager.SendBroadcast(intent);
                return(Result.InvokeSuccess()); // TODO?
            }
            finally
            {
                IsRunning = false;
            }
        }