我正在开发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 = "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();
}
}
}
答案 0 :(得分:1)
您确定已在Manifest.xml
中将此类声明为服务吗?
在<application>
标记内,输入以下代码:
<service android:name=".BackupContacts" />