下面列出了android.util.Patterns#EMAIL_ADDRESS 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private ArrayList<String> getUserEmail(){
ArrayList<String> email = new ArrayList<String>();
Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
Account[] accounts = AccountManager.get(VerifyOTP.this).getAccounts();
for (Account account : accounts) {
if (emailPattern.matcher(account.name).matches()) {
String possibleEmail = account.name;
if(possibleEmail != null)
if(possibleEmail.length() !=0 ){
email.add(possibleEmail);
}
}
}
return email;
}
private ArrayList<String> getUserEmail(){
ArrayList<String> email = new ArrayList<String>();
Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
Account[] accounts = AccountManager.get(VerifyOTP.this).getAccounts();
for (Account account : accounts) {
if (emailPattern.matcher(account.name).matches()) {
String possibleEmail = account.name;
if(possibleEmail != null)
if(possibleEmail.length() !=0 ){
email.add(possibleEmail);
}
}
}
return email;
}
@PluginMethod()
public void isAvailable(PluginCall call) {
String app = call.getString("alias", "");
boolean has = checkPermission();
JSObject object = new JSObject();
PackageManager pm = getActivity().getPackageManager();
if (aliases.has(app)) {
app = aliases.getString(app);
}
object.put("hasAccount", false);
if (has) {
AccountManager am;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
am = AccountManager.get(getContext());
} else {
am = AccountManager.get(getContext());
}
Pattern emailPattern = Patterns.EMAIL_ADDRESS;
for (Account account : am.getAccounts()) {
if (emailPattern.matcher(account.name).matches()) {
object.put("hasAccount", true);
}
}
}
try {
ApplicationInfo info = pm.getApplicationInfo(app, 0);
object.put("hasApp", true);
} catch (PackageManager.NameNotFoundException e) {
object.put("hasApp", false);
} finally {
call.success(object);
}
}
private String getSuggestedEmailChecked() {
Pattern emailPattern = Patterns.EMAIL_ADDRESS;
Account[] accounts = AccountManager.get(getActivity()).getAccounts();
for (Account account : accounts) {
if (emailPattern.matcher(account.name).matches()) {
return account.name;
}
}
return null;
}
public static String getUserEmail(Context c) {
Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+
Account[] accounts = AccountManager.get(c).getAccounts();
for (Account account : accounts) {
if (emailPattern.matcher(account.name).matches()) {
return account.name;
}
}
return "";
}
/**
* Returns true if the given string is an email.
*/
private static boolean isEmail(String account) {
if (TextUtils.isEmpty(account)) {
return false;
}
final Pattern emailPattern = Patterns.EMAIL_ADDRESS;
final Matcher matcher = emailPattern.matcher(account);
return matcher.matches();
}
/**
* 验证email地址格式是否正确
*
* @param str
* @return
*/
public static boolean validateEmail(String str) {
if (str == null || "".equals(str)) {
return false;
}
final Pattern p = Patterns.EMAIL_ADDRESS;
final Matcher m = p.matcher(str);
return m.matches();
}
/**
* Triggers a send email intent. If no application has registered to receive these intents,
* this will fail silently.
*
* @param context The context for issuing the intent.
* @param email The email address to send to.
* @param subject The subject of the email.
* @param body The body of the email.
* @param chooserTitle The title of the activity chooser.
* @param fileToAttach The file name of the attachment.
*/
@CalledByNative
static void sendEmail(Context context, String email, String subject, String body,
String chooserTitle, String fileToAttach) {
Set<String> possibleEmails = new HashSet<String>();
if (!TextUtils.isEmpty(email)) {
possibleEmails.add(email);
} else {
Pattern emailPattern = Patterns.EMAIL_ADDRESS;
Account[] accounts = AccountManager.get(context).getAccounts();
for (Account account : accounts) {
if (emailPattern.matcher(account.name).matches()) {
possibleEmails.add(account.name);
}
}
}
Intent send = new Intent(Intent.ACTION_SEND);
send.setType("message/rfc822");
if (possibleEmails.size() != 0) {
send.putExtra(Intent.EXTRA_EMAIL,
possibleEmails.toArray(new String[possibleEmails.size()]));
}
send.putExtra(Intent.EXTRA_SUBJECT, subject);
send.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body));
if (!TextUtils.isEmpty(fileToAttach)) {
File fileIn = new File(fileToAttach);
send.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(fileIn));
}
try {
Intent chooser = Intent.createChooser(send, chooserTitle);
// we start this activity outside the main activity.
chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(chooser);
} catch (android.content.ActivityNotFoundException ex) {
// If no app handles it, do nothing.
}
}
/**
* Triggers a send email intent. If no application has registered to receive these intents,
* this will fail silently.
*
* @param context The context for issuing the intent.
* @param email The email address to send to.
* @param subject The subject of the email.
* @param body The body of the email.
* @param chooserTitle The title of the activity chooser.
* @param fileToAttach The file name of the attachment.
*/
@CalledByNative
static void sendEmail(Context context, String email, String subject, String body,
String chooserTitle, String fileToAttach) {
Set<String> possibleEmails = new HashSet<String>();
if (!TextUtils.isEmpty(email)) {
possibleEmails.add(email);
} else {
Pattern emailPattern = Patterns.EMAIL_ADDRESS;
Account[] accounts = AccountManager.get(context).getAccounts();
for (Account account : accounts) {
if (emailPattern.matcher(account.name).matches()) {
possibleEmails.add(account.name);
}
}
}
Intent send = new Intent(Intent.ACTION_SEND);
send.setType("message/rfc822");
if (possibleEmails.size() != 0) {
send.putExtra(Intent.EXTRA_EMAIL,
possibleEmails.toArray(new String[possibleEmails.size()]));
}
send.putExtra(Intent.EXTRA_SUBJECT, subject);
send.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body));
if (!TextUtils.isEmpty(fileToAttach)) {
File fileIn = new File(fileToAttach);
send.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(fileIn));
}
try {
Intent chooser = Intent.createChooser(send, chooserTitle);
// we start this activity outside the main activity.
chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(chooser);
} catch (android.content.ActivityNotFoundException ex) {
// If no app handles it, do nothing.
}
}
private boolean isEmailCorrect(String email) {
Pattern emailPattern = Patterns.EMAIL_ADDRESS;
return emailPattern.matcher(email).matches();
}
private boolean isEmailCorrect(String email) {
Pattern emailPattern = Patterns.EMAIL_ADDRESS;
return emailPattern.matcher(email).matches();
}
@Override
public void onClick(View v)
{
nameValue = name.getText().toString();
emailvalue = email.getText().toString();
phoneValue = phone.getText().toString();
messageValue = message.getText().toString();
Log.d("mylog",nameValue+messageValue);
int flagName=0, flagEmail=0, flagMsg=0, flagPhone=0;
Pattern emailPattern = Patterns.EMAIL_ADDRESS;
Pattern phonePattern = Patterns.PHONE;
boolean emailVal= emailPattern.matcher(emailvalue).matches();
boolean phoneVal =phonePattern.matcher(phoneValue).matches();
if(emailVal!=true)
{
email.setError("Invalid Email");
flagEmail=0;
}
else
{
flagEmail=1;
}
if(phoneVal==false)
{
phone.setError("Please enter your phone correctly ");
flagPhone=0;
}
else
{
flagPhone=1;
}
if(nameValue==null || nameValue.equals(" ") || nameValue.equals(""))
{
name.setError("Please enter your name");
flagName=0;
}
else
{
flagName=1;
}
if(messageValue.isEmpty())
{
message.setError("Please enter the message");
flagMsg=0;
}
else
{
flagMsg=1;
}
if (flagEmail==1 && flagPhone==1 && flagMsg==1 && flagName==1)
{
String s= String.valueOf(flagMsg+flagEmail+flagName+flagPhone);
//Log.d("hello",s);
FeedBackSend feedBackSend=new FeedBackSend();
feedBackSend.execute();
}
}
@Override
protected boolean isValid(TextView view) {
Pattern emailPattern = Patterns.EMAIL_ADDRESS;
return emailPattern.matcher(view.getText()).matches();
}