如何在同一个班级中发送电子邮件和SMS?

时间:2019-03-21 14:08:18

标签: java android javamail alarmmanager

我想使用 AlarmManager 触发一个类,该类会发送短信和电子邮件。截至目前,我只能发送短信,任何人都可以告诉我如何发送电子邮件吗?

设置警报和触发接收器类别的代码:

 btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED) {

                        Intent i = new Intent(Pop_up_2.this, Smscreator.class);
                        pIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
                        aManager.set((AlarmManager.RTC_WAKEUP), time, pIntent);
                        Toast.makeText(getApplicationContext(), "Message scheduled", Toast.LENGTH_LONG).show();



                    } else {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            requestPermissions(new String[]{Manifest.permission.SEND_SMS}, 10);
                        }
                    }

@Override
public void onClick(View v) {


        if (v == btnTimePicker) {
            c = Calendar.getInstance();
            c1 = Calendar.getInstance();
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mHour = c.get(Calendar.HOUR_OF_DAY);
            mMinute = c.get(Calendar.MINUTE);

            TimePickerDialog timePickerDialog = new TimePickerDialog(this,
                    new TimePickerDialog.OnTimeSetListener() {

                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay,
                                              int minute) {

                            txtTime.setText(hourOfDay + ":" + minute);
                            mHour=hourOfDay;
                            mMinute=minute;
                        }
                    }, mHour, mMinute, false);
            timePickerDialog.show();

            }

        }

发送短信的代码:

public class Smscreator extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

        StrictMode.setThreadPolicy(policy);
        if (rb1 != null && rad.isChecked()){
            message=s1;
        }else if(rb1 != null && rad1.isChecked())
        {
            message=item;
        }
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(no, null, message, null, null);
        Toast.makeText(context, "SMS sent.",
                Toast.LENGTH_LONG).show();


    }
}

0 个答案:

没有答案