0

Подскажите как отправить сообщение на gmail чтоб вызывалась только именно это приложение gmail

public class EmailActivity extends AppCompatActivity implements View.OnClickListener{
    AppCompatButton send;
    EditText address,emailtext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.emailactivity);
        send = (AppCompatButton) findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener(){
                public void onClick(View arg0){
                    Uri address = Uri.parse("");
                    Intent surf = new Intent(Intent.ACTION_VIEW, address);
                    startActivity(surf);
                }
        });

        send = (AppCompatButton) findViewById(R.id.send);
        address = (EditText) findViewById(R.id.editText);
        emailtext = (EditText) findViewById(R.id.editText1);


        send.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.send:
                final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                emailIntent.setType("plain/text");
                // Кому
                emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] {"******@gmail.com"});
                emailIntent.putExtra(Intent.EXTRA_SUBJECT, address.getText().toString());
                // О чём
                emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,emailtext.getText().toString());
                startActivity(Intent.createChooser(emailIntent, "Отправка письма"));
                break;
        }
    }

}

При отправке выходит много программ спомощью которых можно отправить, как сделать чтобы была именно только gmail?

1 ответ 1

0

Метод для отправки письма:

private void sendUsingGmail(Intent intent) {
    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(intent, 0);

    if (!resInfo.isEmpty()){
        for (ResolveInfo info : resInfo) {
            if (info.activityInfo == null) continue;
            if ("com.google.android.gm".equals(info.activityInfo.packageName)){
                intent.setPackage(info.activityInfo.packageName);
                startActivity(intent);
                return;
            }
        }
    }
    startActivity(Intent.createChooser(emailIntent, "Отправка письма"));
}

Использование:

Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(Intent.EXTRA_EMAIL,new String[] {"******@gmail.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, address.getText().toString());
emailIntent.putExtra(Intent.EXTRA_TEXT,emailtext.getText().toString());
sendUsingGmail(emailIntent);

Пояснение:

Этот метод ищет в установленных приложениях приложение с пакетом com.google.android.gm, который соответствует пакету приложения Gmail. При нахождении использует приложение Gmail, при отсутствии создает стандартный выбор приложения для отправки письма.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.