Click here to Skip to main content
15,076,465 members
Home / Discussions / Android


QuestionText box that take value from other device via Bluetooth Pin
Member 1480532117-Apr-20 9:11
MemberMember 1480532117-Apr-20 9:11 
AnswerRe: Text box that take value from other device via Bluetooth Pin
CHill6017-Apr-20 9:12
mveCHill6017-Apr-20 9:12 
SuggestionRe: Text box that take value from other device via Bluetooth Pin
David Crow17-Apr-20 10:14
MemberDavid Crow17-Apr-20 10:14 
QuestionMy new application with the help of community and their articles - Simple User Feedback Pin
Exoskeletor17-Mar-20 2:32
MemberExoskeletor17-Mar-20 2:32 
AnswerRe: My new application with the help of community and their articles - Simple User Feedback Pin
Afzaal Ahmad Zeeshan10-Apr-20 13:08
mveAfzaal Ahmad Zeeshan10-Apr-20 13:08 
GeneralRe: My new application with the help of community and their articles - Simple User Feedback Pin
Exoskeletor10-Apr-20 13:11
MemberExoskeletor10-Apr-20 13:11 
GeneralMessage Closed Pin
21-Jun-21 3:52
MemberMember 1525637821-Jun-21 3:52 
QuestionAndroid - Running endless code in Foreground while using the app Pin
Exoskeletor12-Mar-20 22:50
MemberExoskeletor12-Mar-20 22:50 
Im making an application that i want to have the ability to send report emails with a frequency. daily, weekly or monthly.

Now i want those emails to be sent only if the app is running and if the user is using it (in my app you use the mobile as a kiosk, you open the app and leave it running all the time, screen will not close)

I have write this code in the MainActivity where if someone has enabled the frequently email feature, the activity will try to send the email (SendStatisticsToEmailBySchedule() will run forever and will use SendStatisticsToEmail() from time to time to send an email):

public async void SendStatisticsToEmail(string subject, string address, int port, string from, string to, bool useSSL, bool saveDateSent = false, string username = "", string password = "", bool sendNowButtonUsed = false)
            var fileName = System.IO.Path.Combine(

            var message = new MimeMessage();
            message.From.Add(new MailboxAddress(from));
            message.To.Add(new MailboxAddress(to));
            message.Subject = subject + " " + fileName;

            var statistics = new Statistics();
            var reportsForXML = statistics.ConvertRecordsForExport(MyData);
            MemoryStream stream = new MemoryStream();

            var builder = new BodyBuilder();
            var image = BitmapFactory.DecodeResource(Resources, Resource.Drawable.get_started);
            byte[] imgbyte;
            using (var bitmap = image)
                var streamByte = new MemoryStream();
                bitmap.Compress(Bitmap.CompressFormat.Png, 100, streamByte);
                imgbyte = streamByte.ToArray();
            var imageAttach = builder.LinkedResources.Add(@"get_started.png", imgbyte);
            imageAttach.ContentId = MimeUtils.GenerateMessageId();
            builder.HtmlBody = string.Format(@"<p align=""center""><center><img height=""150"" width=""328"" src=""cid:{0}""></center></p><p align=""center"">{1}</p><p align=""center"">{2}</p><p align=""center"">{3}</p>", new object[] { imageAttach.ContentId, GetString(Resource.String.EmailExportSettings_MessageBodyLine1), GetString(Resource.String.EmailExportSettings_MessageBodyLine2), GetString(Resource.String.EmailExportSettings_MessageBodyLine3) });

            using (System.IO.StreamWriter writer = new System.IO.StreamWriter(stream))
                reportsForXML.WriteXml(writer, XmlWriteMode.WriteSchema, false);
                stream.Position = 0;
                builder.Attachments.Add(fileName, stream);
            message.Body = builder.ToMessageBody();

                using (var client = new SmtpClient())
                    // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
                    client.ServerCertificateValidationCallback = (s, c, h, e) => true;

                    if (useSSL)
                        await client.ConnectAsync(address, port, SecureSocketOptions.SslOnConnect);
                        await client.ConnectAsync(address, port, false);

                    // Note: only needed if the SMTP server requires authentication
                    if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
                        client.Authenticate(username, password);

                    await client.SendAsync(message);
                    await client.DisconnectAsync(true);
                    if (saveDateSent)
                        Preferences.Set(Settings.ExportToEmailLastEmailSentKey, DateTime.Now);
                            Preferences.Get(Settings.ExportToEmailEmailFrequencyPositionKey, Settings.ExportToEmailEmailFrequencyPositionDefault)
                    if (sendNowButtonUsed)
                        Toast.MakeText(this, GetString(Resource.String.Statistics_EmailSendSuccess), ToastLength.Long).Show();
            catch //(Exception exception)
                if (sendNowButtonUsed)
                    Toast.MakeText(this, GetString(Resource.String.Statistics_EmailSendFailure), ToastLength.Long).Show();

private async void SendStatisticsToEmailBySchedule()
                if (MyData.Count != 0)
                    var currentConnectivity = Connectivity.NetworkAccess;
                    NextScheduledDateForReport = Preferences.Get(Settings.ExportToEmailNextEmailForKey, CurrentDateSettings.ExportToEmailNextEmailForDefault);

                    if (DateTime.Now > NextScheduledDateForReport)
                        if (currentConnectivity == NetworkAccess.Internet)
                                SendStatisticsToEmail(Preferences.Get(Settings.ExportToEmailSubjectKey, Settings.ExportToEmailSubjectDefault),
                                    Preferences.Get(Settings.ExportToEmailServerAddressKey, Settings.ExportToEmailServerAddressDefault),
                                    Preferences.Get(Settings.ExportToEmailPortKey, Settings.ExportToEmailPortDefault),
                                    Preferences.Get(Settings.ExportToEmailFromKey, Settings.ExportToEmailFromDefault),
                                    Preferences.Get(Settings.ExportToEmailToKey, Settings.ExportToEmailToDefault),
                                    Preferences.Get(Settings.ExportToEmailUseSSLKey, Settings.ExportToEmailUseSSLDefault),
                                    Preferences.Get(Settings.ExportToEmailUsernameKey, Settings.ExportToEmailUsernameDefault),
                                    Preferences.Get(Settings.ExportToEmailPasswordKey, Settings.ExportToEmailPasswordDefault)

                await Task.Delay(21600);
            } while (true);

 private void ScheduleNextEmailForSend(DateTime nextScheduledEmailDate, int frequency)
        nextScheduledEmailDate = DateTime.Now;
        frequency = Preferences.Get(Settings.ExportToEmailEmailFrequencyPositionKey, Settings.ExportToEmailEmailFrequencyPositionDefault);
        switch (frequency)

            case 0:
                Preferences.Set(Settings.ExportToEmailNextEmailForKey, nextScheduledEmailDate.AddDays(1));
            case 1:
                Preferences.Set(Settings.ExportToEmailNextEmailForKey, nextScheduledEmailDate.AddDays(7));
            case 2:
                Preferences.Set(Settings.ExportToEmailNextEmailForKey, nextScheduledEmailDate.AddDays(30));

From what i have read here if you want to run some code only while the user is using your app, you should not use a service.

Is my code efficient?

Is there any situation that will stop working or should i somehow check that it is still working?
Nelek17-Mar-20 2:18
protectorNelek17-Mar-20 2:18 
QuestionUnfortunately app has stopped Pin
Kevsh25-Feb-20 7:29
MemberKevsh25-Feb-20 7:29 
AnswerRe: Unfortunately app has stopped Pin
Richard MacCutchan25-Feb-20 8:22
mveRichard MacCutchan25-Feb-20 8:22 
AnswerRe: Unfortunately app has stopped Pin
Exoskeletor12-Mar-20 22:53
MemberExoskeletor12-Mar-20 22:53 
QuestionConversion file backup .csv to file backup Crypt5 or Crypt12 Pin
Oderisio13-Feb-20 5:46
MemberOderisio13-Feb-20 5:46 
Rant[REPOST] Conversion file backup .csv to file backup Crypt5 or Crypt12 Pin
Richard Deeming13-Feb-20 6:46
mveRichard Deeming13-Feb-20 6:46 
QuestiononOptionsItemSelected go to a website Pin
The_Arcaniac1-Feb-20 22:30
MemberThe_Arcaniac1-Feb-20 22:30 
SuggestionRe: onOptionsItemSelected go to a website Pin
David Crow10-Feb-20 16:39
MemberDavid Crow10-Feb-20 16:39 
QuestionHello, I am a newbie here, good to meet yah. Pin
The_Arcaniac1-Feb-20 22:14
MemberThe_Arcaniac1-Feb-20 22:14 
AnswerRe: Hello, I am a newbie here, good to meet yah. Pin
David Crow4-Feb-20 16:24
MemberDavid Crow4-Feb-20 16:24 
AnswerRe: Hello, I am a newbie here, good to meet yah. Pin
Exoskeletor12-Mar-20 22:54
MemberExoskeletor12-Mar-20 22:54 
AnswerRe: Hello, I am a newbie here, good to meet yah. Pin
Seechon Boontharikwong10-Apr-20 6:26
MemberSeechon Boontharikwong10-Apr-20 6:26 
QuestionGlobalization issues with Dates Pin
Vimalsoft(Pty) Ltd29-Dec-19 10:17
professionalVimalsoft(Pty) Ltd29-Dec-19 10:17 
AnswerRe: Globalization issues with Dates Pin
Eddy Vluggen29-Dec-19 12:05
professionalEddy Vluggen29-Dec-19 12:05 
AnswerRe: Globalization issues with Dates Pin
Richard MacCutchan30-Dec-19 0:09
mveRichard MacCutchan30-Dec-19 0:09 
GeneralRe: Globalization issues with Dates Pin
Vimalsoft(Pty) Ltd30-Dec-19 7:35
professionalVimalsoft(Pty) Ltd30-Dec-19 7:35 
SuggestionRe: Globalization issues with Dates Pin
Richard Deeming7-Jan-20 9:16
mveRichard Deeming7-Jan-20 9:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.