我正在开发一个安卓防盗应用程序,想实现备份联系人功能。我不是 Android 方面的专家,而且对它很陌生,所以我只是简单地实现了一个简单的编码来检索联系人姓名和电话号码,并将所有这些附加到一个字符串消息中并通过电子邮件发送。我希望这是我在应用程序中的一项服务,这意味着用户不会收到此备份联系服务的通知。我确实将这个功能作为一个应用程序独立运行并且它可以工作。但是,当我将此作为服务移至我的防盗应用程序时,它不起作用...请大家帮助我检查问题...谢谢...
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();
}
}
}