我正在构建一个Android应用程序,通过它我想立即邮寄从设备相机拍摄的照片。 我希望这可以在后台完成,所以我使用了JAVA Mail API,我正在启动一个服务来邮寄图像。
到目前为止,我的代码工作正常,图片< 50 KB,但当我尝试邮件图像> = 500 KB时,它会抛出奇怪的错误。 现在我正在使用文件和用户名,密码的硬编码值。
public class BgMailer extends Service{
private ArrayList<String>Recievers ;
private String msg,sub;
public void onCreate() {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "service created",5000).show();
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Toast.makeText(this, DataHolder., duration)
//sendMail();
Notification note = new Notification( 0, null, System.currentTimeMillis() );
note.flags |= Notification.FLAG_NO_CLEAR;
startForeground( 42, note );
Recievers = intent.getStringArrayListExtra("recievers");
sub = intent.getStringExtra("sub");
msg = intent.getStringExtra("msg");
sendMail(Recievers, sub, msg);
// stopForeground(true);
this.stopSelf();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "service destroyed",5000).show();
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void sendMail(ArrayList<String>recvr,String sub,String msg){
Mail m = new Mail("r4jiv007@gmail.com", "<password>");
String [] address = new String[recvr.size()];
address = recvr.toArray(address);
m.set_to(address);
m.set_from("r4jiv007@gmail.com");
m.set_subject(sub);
m.setBody(msg);
try {
m.addAttachment("/sdcard/1.jpg");
//m.addAttachment("/sdcard/1.txt");
// m.addAttachment("/sdcard/bday.jpg");
if(m.send()) {
Toast.makeText(getApplicationContext(), "Email was sent successfully.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Email was not sent.", Toast.LENGTH_LONG).show();
}
} catch(Exception e) {
Log.e("MailApp", "Could not send email", e);
}
}
}
LogCat中的错误值: -
10-20 01:34:13.690: W/ActivityManager(134): Timeout executing service:
ServiceRecord{40ae01d0 my.com.cnm/.BgMailer}
10-20 01:34:13.800: I/Process(134): Sending signal. PID: 1845 SIG: 3
10-20 01:34:13.800: I/dalvikvm(1845): threadid=4: reacting to signal 3
10-20 01:34:13.820: I/dalvikvm(1845): Wrote stack traces to '/data/anr/traces.txt'
10-20 01:34:13.830: I/Process(134): Sending signal. PID: 134 SIG: 3
10-20 01:34:13.830: I/dalvikvm(134): threadid=4: reacting to signal 3
10-20 01:34:13.870: I/dalvikvm(134): Wrote stack traces to '/data/anr/traces.txt'
10-20 01:34:13.870: I/Process(134): Sending signal. PID: 235 SIG: 3
10-20 01:34:13.870: I/dalvikvm(235): threadid=4: reacting to signal 3
10-20 01:34:13.880: I/dalvikvm(235): Wrote stack traces to '/data/anr/traces.txt'
10-20 01:34:13.880: I/Process(134): Sending signal. PID: 239 SIG: 3
10-20 01:34:13.880: I/dalvikvm(239): threadid=4: reacting to signal 3
10-20 01:34:13.900: I/dalvikvm(239): Wrote stack traces to '/data/anr/traces.txt'
10-20 01:34:13.900: I/Process(134): Sending signal. PID: 208 SIG: 3
10-20 01:34:13.900: I/dalvikvm(208): threadid=4: reacting to signal 3
10-20 01:34:13.900: I/dalvikvm(208): Wrote stack traces to '/data/anr/traces.txt'
10-20 01:34:14.481: D/dalvikvm(134): GC_CONCURRENT freed 1427K, 32% free 9016K/13191K, external 607K/1119K, paused 8ms+16ms
10-20 01:34:14.821: D/dalvikvm(134): GC_EXPLICIT freed 379K, 32% free 9072K/13191K, external 607K/1119K, paused 171ms
10-20 01:34:15.432: I/Process(134): Sending signal. PID: 1657 SIG: 3
10-20 01:34:15.432: I/dalvikvm(1657): threadid=4: reacting to signal 3
10-20 01:34:15.432: I/dalvikvm(1657): Wrote stack traces to '/data/anr/traces.txt'
10-20 01:34:15.442: E/ActivityManager(134): ANR in my.com.cnm:Bgmailer time=4035592
10-20 01:34:15.442: E/ActivityManager(134): Reason: Executing service my.com.cnm/.BgMailer
10-20 01:34:15.442: E/ActivityManager(134): Load: 3.78 / 3.97 / 3.79
10-20 01:34:15.442: E/ActivityManager(134): CPU usage from 13492ms to 0ms ago with 99% awake:
10-20 01:34:15.442: E/ActivityManager(134): 1.4% 230/com.htc.android.htcime: 1.3% user + 0% kernel / faults: 746 minor 3 major
10-20 01:34:15.442: E/ActivityManager(134): 4% 1845/my.com.cnm:Bgmailer: 3.7% user + 0.3% kernel / faults: 63 minor
10-20 01:34:15.442: E/ActivityManager(134): 2.2% 239/android.process.acore: 2.1% user + 0% kernel / faults: 12 minor
10-20 01:34:15.442: E/ActivityManager(134): 1.5% 272/dhd_dpc: 0% user + 1.5% kernel
10-20 01:34:15.442: E/ActivityManager(134): 1.2% 134/system_server: 0.7% user + 0.5% kernel
10-20 01:34:15.442: E/ActivityManager(134): 0.8% 81/akmd: 0% user + 0.8% kernel
10-20 01:34:15.442: E/ActivityManager(134): 0.6% 1657/my.com.cnm: 0.3% user + 0.2% kernel / faults: 4 minor
10-20 01:34:15.442: E/ActivityManager(134): 0.5% 424/com.android.htccontacts: 0.5% user + 0% kernel / faults: 54 minor
10-20 01:34:15.442: E/ActivityManager(134): 0.1% 208/com.android.systemui: 0% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 0% 271/dhd_watchdog: 0% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 0% 5/events/0: 0% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 0% 14/kondemand/0: 0% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 0% 64/kgsl-3d0/0: 0% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 0% 83/adbd: 0% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 0% 291/wpa_supplicant: 0% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 0% 552/com.ebuddy.android.xms: 0% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 0% 753/com.google.android.apps.maps:NetworkLocationService: 0% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 0.1% 848/com.google.android.apps.maps:LocationFriendService: 0% user + 0% kernel + 0% iowait
10-20 01:34:15.442: E/ActivityManager(134): 19% TOTAL: 16% user + 2.3% kernel + 0.3% iowait + 0.1% softirq
10-20 01:34:15.442: E/ActivityManager(134): CPU usage from 1133ms to 1688ms later:
10-20 01:34:15.442: E/ActivityManager(134): 8.9% 134/system_server: 0% user + 8.9% kernel
10-20 01:34:15.442: E/ActivityManager(134): 5.3% 157/ActivityManager: 0% user + 5.3% kernel
10-20 01:34:15.442: E/ActivityManager(134): 1.7% 140/SurfaceFlinger: 1.7% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 1.7% 81/akmd: 1.7% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 3.5% 1722/akmd: 3.5% user + 0% kernel
10-20 01:34:15.442: E/ActivityManager(134): 1.2% 1657/my.com.cnm: 1.2% user + 0% kernel / faults: 2 minor
10-20 01:34:15.442: E/ActivityManager(134): 14% TOTAL: 5.3% user + 8.9% kernel
10-20 01:34:15.452: I/Process(134): Sending signal. PID: 1845 SIG: 9
10-20 01:34:15.452: I/ActivityManager(134): Skipping background ANR: my.com.cnm:Bgmailer time=4035592
10-20 01:34:15.462: I/ActivityManager(134): Process my.com.cnm:Bgmailer (pid 1845) has died.
这里有任何与优先级或服务相关的东西,我甚至试图将我的服务作为前台应用程序运行,但没有用......我无法弄清楚如何解决这个问题。 我将不胜感激任何帮助!!
谢谢:)
非常感谢有关此问题的建议,我刚刚创建了一个帖子并通过它邮寄了图像并且它成功了..但我希望在邮件过程成功完成后显示祝酒,但我收到以下错误: -
E/MailApp(2131): Could not send email
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.widget.Toast.<init>(Toast.java:68)
at android.widget.Toast.makeText(Toast.java:231)
at my.com.cnm.BgMailer.sendMail(BgMailer.java:86)
at my.com.cnm.BgMailer$1.run(BgMailer.java:47)
at java.lang.Thread.run(Thread.java:1027)
答案 0 :(得分:1)
由于服务也在进程主线程中运行,您可能会尝试在服务中创建单独的线程来发送电子邮件..
以下内容来自official doc:
请注意,服务与其他应用程序对象一样,在其托管进程的主线程中运行。这意味着,如果您的服务要进行任何CPU密集型(例如MP3播放)或阻止(例如网络)操作,它应该生成自己的线程来执行该工作。
查看以下帖子中接受的答案,以便从后台主题中显示Toast:
Android: How can i show a toast from a thread running in a remote service?
答案 1 :(得分:0)
看起来你得到的是ANR
,因为它花了这么长时间,也许你把发送代码放在AsyncTask
中,所以它在后台线程上运行它不会被检测为{ {1}}。
http://developer.android.com/reference/android/os/AsyncTask.html