想一次使用2班

时间:2016-08-28 17:08:31

标签: java android

我正在使用Java-Mail API中的一个项目,在那里我有一些字段和一个按钮,当我点击按钮时,邮件将被发送。但问题是我有一个类扩展Fragment另一个扩展Activity的类,我不知道如何嵌入这两个类。

这是第一堂课:

public class Contact_Fragment1 extends Fragment {
    private Activity myActivity;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.tabfragment_sendmail, container, false);

        return v;
    }
}

tabfragment_sendmail我有几个字段和一个按钮,我想在单击按钮时在下面执行。

public class OutsideClass extends Activity implements View.OnClickListener {

    Session session = null;
    ProgressDialog pdialog = null;
    Context context = null;
    EditText reciep, sub, msg;
    String rec, subject, textMessage;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;

        Button login = (Button) findViewById(R.id.buttonSend);
        reciep = (EditText) findViewById(R.id.etEmail);
        sub = (EditText) findViewById(R.id.etSubject);
        msg = (EditText) findViewById(R.id.etMessage);

        login.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        rec = reciep.getText().toString();
        subject = sub.getText().toString();
        textMessage = msg.getText().toString();

        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        session = Session.getDefaultInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("xxxxx@live.com", "xxxxxx");
            }
        });

        pdialog = ProgressDialog.show(context, "", "Sending Mail....", true);

        RetreiveFeedTask task = new RetreiveFeedTask();
        task.execute();
    }

    class RetreiveFeedTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... strings) {

            try {
                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress("rasem2009@live.com"));
                message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(rec));
                message.setSubject(subject);
                message.setContent(textMessage, "text/html; charset=utf-8");

                Transport.send(message);
            } catch (MessagingException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String aVoid) {

            pdialog.dismiss();
            reciep.setText("");
            msg.setText("");
            sub.setText("");
            Toast.makeText(getApplicationContext(), "Message Sent", Toast.LENGTH_LONG).show();
        }
    }
}

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

所以你的问题是你不知道如何在点击Button时开始另一项活动?

你可以这样做

 public void onClick(View v) {
     Intent StartMyActivity = new Intent(this,Your_Activity.class);
     //Enter optional Intent.putExtras here
     start Activity(StartMyActivity);
 }

如果你想从非活动开始,你需要更换&#34;这个&#34;有了背景 (我认为getApplicationContext()应该有用吗?没有尝试过那么艰难)