2016-03-04 37 views
2

我通過使用android.permission.GET_ACCOUNTS權限獲取電子郵件ID。獲取用戶的Gmail ID在Android 6.0棉花糖

try { 
      Account[] accounts = AccountManager.get(this).getAccountsByType("com.google"); 
      for (Account account : accounts) { 
       emailid = account.name; 
       Log.e("account",emailid); 
      } 
     } catch (Exception e) { 
      Log.i("Exception", "Exception:" + e); 
     } 

此代碼正在所有設備upto棒棒糖5.1。 但它不適用於棉花糖6.0。

任何人都可以幫助我解決這個問題。 我甚至沒有在logcat中發生任何錯誤。

回答

2

此代碼工作,測試在Android 4.4.4,5.0.1,6.0和6.0.1

String possibleEmail = ""; 
    final Account[] accounts = AccountManager.get(context).getAccounts(); 
    //Log.e("accounts","->"+accounts.length); 
    for (Account account : accounts) { 
     if (Patterns.EMAIL_ADDRESS.matcher(account.name).matches()) { 
      possibleEmail = account.name; 
     } 
    } 

possibleEmail是設備的電子郵件。

+0

我是否需要任何運行權限..? –

+0

是的,你做! '<使用權限android:name =「android.permission.GET_ACCOUNTS」/>' –

+1

沒有工作。我認爲@RubinNellikunnathu是正確的。因爲它不是RT,因此上面的行不起作用。 – VVB

12

您需要爲Android 6.0棉花糖添加運行時權限。這是工作代碼。

//檢查我的測試手機操作系統版本,它是棉花糖。在onCreate方法中使用這個。

private static final int REQUEST_GET_ACCOUNT = 112; 

    if(android.os.Build.VERSION.SDK_INT > 22){ 
      if(isGETACCOUNTSAllowed()){ 
       // do your task 

       getMailAddress(); 
       return; 
      }else{ 
       requestGET_ACCOUNTSPermission(); 
      } 

     } 

//檢查你是否已經獲得了棉花糖的運行時權限。

private boolean isGETACCOUNTSAllowed() { 
     //Getting the permission status 
     int result = ContextCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS); 

     //If permission is granted returning true 
     if (result == PackageManager.PERMISSION_GRANTED) 
      return true; 

     //If permission is not granted returning false 
     return false; 
    } 


//if you don't have the permission then Requesting for permission 
    private void requestGET_ACCOUNTSPermission(){ 

     if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.GET_ACCOUNTS)){ 


     } 

     //And finally ask for the permission 
     ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.GET_ACCOUNTS},REQUEST_GET_ACCOUNT); 
    } 

//最後檢查onRequestPermissionsResult @Override方法檢查用戶是否允許或不允許。如果允許則可能電子郵件是您的郵件地址。

@Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 

     //Checking the request code of our request 
     if(requestCode == REQUEST_GET_ACCOUNT){ 

      //If permission is granted 
      if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ 


       Toast.makeText(this,"Thanks You For Permission Granted ",Toast.LENGTH_LONG).show(); 

      getMailAddress(); 

      }else{ 
       //Displaying another toast if permission is not granted 
       Toast.makeText(this,"Oops you just denied the permission",Toast.LENGTH_LONG).show(); 
      } 
     } 

} 


public void getMailAddress(){ 

      String possibleEmail = null; 

      Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+ 
      Account[] accounts = AccountManager.get(context).getAccountsByType(
        "com.google"); 
      for (Account account : accounts) { 
       if (emailPattern.matcher(account.name).matches()) { 
        possibleEmail = account.name; 
        Log.i("MY_EMAIL_count", "" + possibleEmail); 
       } 
      } 

} 
+0

嗨Tariqul,謝謝你的回答。你可以編輯添加一些更多的解釋,說明你的方法在做什麼/你在代碼塊中改變了什麼? –

+0

你好蒂姆·馬龍,對不起,我的英語不好,我英語不太好,已經更新代碼請檢查。如果你不明白,請讓我知道。我會再解釋一下 – Tariqul

+0

如果這個答案對你有幫助,請接受我的回答 – Tariqul

0

作爲單獨服務類的示例,通過調用getGMailAccount方法可以方便地在不同情況下使用。

公共類GMailAccountService {

private static final int MIN_SDK_FOR_REQUESTING_GET_ACCOUNTS = 22; 
private static final int GET_ACCOUNTS_PERMISSION_REQUEST_CODE = 112; 

private Activity activity; 

public GMailAccountService(Activity activity) { 
    this.activity = activity; 
} 

public String getGMailAccount() { 
    if(android.os.Build.VERSION.SDK_INT > MIN_SDK_FOR_REQUESTING_GET_ACCOUNTS){ 
     if(!isGetAccountsPermissionAllowed()){ 
      requestGetAccountsPermission(); 
      return getGMailAccount(); 
     } 
    } 
    return extractAddressFromAccountManager(); 
} 

private boolean isGetAccountsPermissionAllowed() { 
    int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.GET_ACCOUNTS); 
    if (result == PackageManager.PERMISSION_GRANTED) 
     return true; 
    return false; 
} 

private void requestGetAccountsPermission(){ 
    ActivityCompat.shouldShowRequestPermissionRationale(activity, android.Manifest.permission.GET_ACCOUNTS); 
    ActivityCompat.requestPermissions(activity,new String[]{android.Manifest.permission.GET_ACCOUNTS}, GET_ACCOUNTS_PERMISSION_REQUEST_CODE); 
} 


public String extractAddressFromAccountManager(){ 

    Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8 
    Account[] accounts = AccountManager.get(activity).getAccountsByType("com.google"); 
    for (Account account : accounts) { 
     if (emailPattern.matcher(account.name).matches()) { 
      return account.name; 
     } 
    } 
    return null; 
} 

}

+0

必須在AndroidManifest.xml中添加 – stanska