Note that there are some explanatory texts on larger screens.

plurals
  1. POThe Android service is not responding
    primarykey
    data
    text
    <p>I am developing an Android anti-theft application and would like to implement the backup contacts function. I am not expert in Android and quite new in it, so I just simply implement a simple coding in retrieving the contact names together with the phone numbers and attach all these into a string message and send it over the email. I would like this to be one of my services in the application, meaning user is not informed with this backup contact service. I did run this function independently as an application and it works. But when i move this to my anti-theft apps as a service, it does not work... kindly need help from everyone to help me check out the problem... thanks...</p> <pre><code>public class BackupContacts extends Service{ /** Called when the activity is first created. */ String msg = "**********Backup Phone Contacts**********\n\n"; SharedPreferences pref; public static String filenames = "AntiTheft"; String email; @Override public IBinder onBind(Intent intent){ // TODO Auto-generated method stub return null; } @Override public void onCreate(){ // TODO Auto-generated method stub pref = getSharedPreferences(filenames, 0); email = pref.getString("keyemail", ""); super.onCreate(); } @Override public void onStart(Intent intent, int startId){ // TODO Auto-generated method stub super.onStart(intent, startId); } public int onStartCommand(Intent intent, int flags, int startId){ retrieveContacts(); return START_STICKY; } public void retrieveContacts(){ ContentResolver cr = getContentResolver(); Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); while(cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndexOrThrow(Phone.DISPLAY_NAME)); String number = cursor.getString(cursor.getColumnIndexOrThrow(Phone.NUMBER)); msg += name + "\t\t\t\t\t" + number + "\n"; } try{ sendMail(); } catch(MessagingException e){ e.printStackTrace(); } } public void sendMail() throws MessagingException{ // Recipient's email ID needs to be mentioned. String to = email; // Sender's email ID needs to be mentioned String from = "testing@gmail.com"; // Email Password String password = "abc123"; // Assuming you are sending email from GMail SMTP String host = "smtp.gmail.com"; // Get system properties Properties properties = System.getProperties(); // Setup mail server //properties.setProperty("mail.smtp.host", host); properties.put("mail.smtp.host", host); properties.put("mail.smtps.auth", "true"); properties.put("mail.smtp.starttls.enable", "true"); // Get the default Session object. Session session = Session.getDefaultInstance(properties, null); // Create a default MimeMessage object. MimeMessage message = new MimeMessage(session); // Set From: header field of the header. message.setFrom(new InternetAddress(from)); // Set To: header field of the header. message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set Subject: header field message.setSubject("This is the Backup Contacts Email!"); // Now set the actual message //message.setText(msg.toString()); //BodyPart always come along with MultiPart (For sending attachment in email) //Create the message part BodyPart msgBodyPart = new MimeBodyPart(); //Fill up the message msgBodyPart.setText(msg); //Create a multipart message Multipart multipart = new MimeMultipart(); //Set the text message part multipart.addBodyPart(msgBodyPart); //Set the complete message part message.setContent(multipart); // Send message //Transport.send(message); try{ Transport transport = session.getTransport("smtps"); transport.connect(host, from, password); transport.sendMessage(message, message.getAllRecipients()); System.out.println("Sent message successfully...."); transport.close(); }catch (MessagingException mex) { mex.printStackTrace(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload