3
我正在開發一個Android防盜應用程序,並希望實現備份聯繫人功能。我不是Android的專家,也不是很熟悉,所以我只是簡單地實現一個簡單的編碼,將聯繫人姓名和電話號碼一起檢索,然後將所有這些附加到字符串消息中並通過電子郵件發送。我希望這是我在應用程序中的服務之一,這意味着用戶不會被告知此備份聯繫服務。我確實獨立運行這個函數作爲一個應用程序,它的工作。但是,當我提出這個我防盜應用作爲一種服務,它不能正常工作......懇請需要幫助從大家幫我檢查出的問題...謝謝...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 = "[email protected]";
// 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();
}
}
}
是的......我已經聲明此... – user1782267