我对 android 很陌生,并尝试使用第 3 部分 api 发送邮件。我完成了几乎所有的编码,然后我发现 android 3.0+ 不允许在主线程上进行网络操作,唯一的方法是将它添加到 Async.Below将活动扩展到异步后是我的代码。但我收到错误“Void 是变量 onCreate 的无效类型”。我不知道变量 onCreate 的有效类型应该是什么。有人可以解决这个问题吗?谢谢
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
//import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class ZZZ extends Activity{
public class done extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
Button addImage = (Button) findViewById(R.id.send_email);
addImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Mail m = new Mail("my@domain.com", "password");
String[] toArr = {"mail1@domain.com", "mail2@domain.com"};
m.setTo(toArr);
m.setFrom("mymail@gmail.com");
m.setSubject("This is an email sent using my Android device.");
m.setBody("Email body.");
try {
m.addAttachment("/storage/somethinghere");
if(m.send()) {
Toast.makeText(ZZZ.this, "Email was sent successfully.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ZZZ.this, "Email was not sent.", Toast.LENGTH_LONG).show();
}
} catch(Exception e) {
//Toast.makeText(MailApp.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show();
Log.e("MailApp", "Could not send email", e);
}
}
});
}}
}
}