我创建了两个活动。 Activity Main有按钮,点击这个按钮我调用另一个类的方法,扩展到AppCompActivity。方法名称为mailconfig,如下所示。机密信息已从参数中删除。
public class ButtonActionFrontPage extends AppCompatActivity{
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
}
public void mailconfig(String message) throws EmailException {
String username = "";
String password = "";
String from = "";
String replyto = "";
String mailto = "";
String subject = "";
Email email = new SimpleEmail();
email.setSSLOnConnect(true);
email.isStartTLSEnabled();
email.setHostName("");
email.setSmtpPort(26);
email.setSubject(subject);
email.addReplyTo(replyto);
email.setFrom(from);
email.setAuthenticator(new DefaultAuthenticator(username, password));
email.setMsg(message);
email.addTo(mailto);
email.send();
Toast.makeText(ButtonActionFrontPage.this,"Thanks for submitting ",Toast.LENGTH_SHORT).show();
System.out.println("Sent");
}
}
我使用下面的代码来调用上面的方法。
feedbackbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
ButtonActionFrontPage buttonActionFrontPage = new ButtonActionFrontPage();
String message = quickfbet.getText().toString();
buttonActionFrontPage.mailconfig(message);
} catch (EmailException e) {
e.printStackTrace();
}
}
});
此代码有什么问题,为什么不执行。
答案 0 :(得分:0)
类就像这样
public class ButtonActionFrontPage {
public void mailconfig(Context context,String message) throws EmailException {
String username = "";
String password = "";
String from = "";
String replyto = "";
String mailto = "";
String subject = "";
Email email = new SimpleEmail();
email.setSSLOnConnect(true);
email.isStartTLSEnabled();
email.setHostName("");
email.setSmtpPort(26);
email.setSubject(subject);
email.addReplyTo(replyto);
email.setFrom(from);
email.setAuthenticator(new DefaultAuthenticator(username, password));
email.setMsg(message);
email.addTo(mailto);
email.send();
Toast.makeText(context,"Thanks for submitting ",Toast.LENGTH_SHORT).show();
System.out.println("Sent");
}
}
并且像这样调用函数
feedbackbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
ButtonActionFrontPage buttonActionFrontPage = new ButtonActionFrontPage();
String message = quickfbet.getText().toString();
buttonActionFrontPage.mailconfig(getApplicationContext(),message);
} catch (EmailException e) {
e.printStackTrace();
}
}
});
答案 1 :(得分:0)
public class ButtonActionFrontPage extends AppCompatActivity{
static ButtonActionFrontPage instance;
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
instance = this;
}
public static ButtonActionFrontPage getInstance() {
return instance;
}
@Override
protected void onDestroy() {
super.onDestroy();
instance = null;
}
}
并调用函数:
feedbackbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
ButtonActionFrontPage buttonActionFrontPage = ButtonActionFrontPage.getInstance(); String message = quickfbet.getText().toString();
buttonActionFrontPage.mailconfig(message);
} catch (EmailException e) {
e.printStackTrace();
}
}
});
答案 2 :(得分:0)
Java类与Android
Activity
不同。由于Android Activity有一个名为life cycle的东西。
如果必须实施某些功能,您甚至不能创建活动。只是一个普通的Java类就足够了。
当存在用户交互时,可以使用活动(事实上并非总是如此,但纯粹取决于业务逻辑)。为了启动活动,使用Intent。这将通过内存分配和其他相关功能启动活动。
对于您的情况,按钮的启动应该在ButtonActionFrontPage的onCreate
中完成,并通过点击监听器完成,如下所示
Button feedbackbtn;
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
feedbackbtn=(Button)findViewById(R.id.button_ID);
feedbackbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new PlainJavaClass().mailconfig("msg",ButtonActionFrontPage.class);
}
});
}
对于业务逻辑,只需使用PlainJavaClass方法和context
,如果您必须显示任何Toast
/ Dialog
/ ProgressBar
class PlainJavaClass{
public void mailconfig(String message, Context context) {
Log.v("TAG","mailconfig with message="+message);
//Your logic
Toast.makeText(context,"Thanks for submitting ",Toast.LENGTH_SHORT).show();
}
}