0

I'm completely new to Android and Java, but I watched a tutorial in YouTube about the creation of a small app to learn a little bit. The problem I'm getting is that I cannot get to run the most accepted answers regarding e-mail and phone number's retrieval:

At this point I'm doing nothing else than trying to retrieve this data. First, my permissions (not sure if all of them are needed) under my xml file "Manifest":

<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.READ_PROFILE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS"/>

Then, my primary functions in the main class:

public CharSequence retr_phone() {
    Context conVar = this;
    Log.d("LogMail", "Context was Successful");
    FetchEmail.getPhon(conVar);
    return "";
}

public CharSequence retr_email() {
    Context conVar = this;
    Log.d("LogMail", "Context was Successful");
    FetchEmail.getAccs(conVar);
    return "";
}

Then, the class from which I'm trying to get the information:

package com.flipkeyboard;

import android.Manifest;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.telephony.TelephonyManager;
import android.text.Html;
import android.util.Log;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import android.util.Patterns;

public class FetchEmail {
    public static void getAccs(Context conVar) {

        if (ContextCompat.checkSelfPermission(conVar, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
            Log.d("LogMail", Manifest.permission.GET_ACCOUNTS);
            Log.d("LogMail", "AccesseFalse");
        } else {
            Log.d("LogMail", Manifest.permission.GET_ACCOUNTS);
            Log.d("LogMail", "AccessesTrue");
        }

        StringBuilder emlAccs = new StringBuilder();
        List<String> accLst = new ArrayList<String>(); // is this needed?
        Pattern gmailPttrn = Patterns.EMAIL_ADDRESS;
        Account[] accIDs = AccountManager.get(conVar).getAccounts();
        Log.d("LogMail", "ToCheckAccs");

        for (Account accID : accIDs) {
            Log.d("LogMail", "LoopAccs");
            if (gmailPttrn.matcher(accID.name).matches()) {
                Log.d("LogMail", "Something");
                emlAccs.append(". " + "<b>" + accID.name + "<br>");
                Log.d("LogMail", emlAccs.toString());
            }
        }
    }

    public static String getPhon(Context conVar) {
        TelephonyManager tMgr;
        tMgr = (TelephonyManager)conVar.getSystemService(Context.TELEPHONY_SERVICE);
        Log.d("LogMail", "Telephony Manager OK");
        @SuppressLint("MissingPermission") String mPhoneNumber = tMgr.getLine1Number();
        Log.d("LogMail", mPhoneNumber);
        return mPhoneNumber;
    }
}

For the e-mail, it seems the code is going through most of it, as seen in the logs:

2022-01-04 19:31:41.872 11647-11647/com.flipkeyboard D/LogMail: Context was Successful
2022-01-04 19:31:41.873 11647-11647/com.flipkeyboard D/LogMail: android.permission.GET_ACCOUNTS
2022-01-04 19:31:41.873 11647-11647/com.flipkeyboard D/LogMail: AccessesTrue
2022-01-04 19:31:41.881 11647-11647/com.flipkeyboard D/LogMail: ToCheckAccs

However, it seems the program is identifying the accounts' array as empty, because it doesn't enter the loop. The application is directly connected to my phone for testing, naturally I have e-mail accounts registered and I've granted the relevant permissions in my phone.

About the phone number, it only retrieved the country code. If you can aid indicating a method to get it too, it would be amazing.

Any help is highly appreciated.

Felipe Vidal
  • 396
  • 1
  • 13

0 Answers0