Masquerade
Masquerade

Reputation: 3890

Intent for opening gmail app in android unfortunately stops

The app has an order button when it's clicked the gmail app should be opened with subject and body filed desirably but the intent for it in my code is not working and the app shows a message that it is unfortunately stopping. I've tried many ways to implement this but i'm unable to do so.

package com.example.android.justjava;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import java.text.NumberFormat;

import static android.R.attr.value;
import static android.R.id.checkbox;
import static android.R.id.message;

/**
 * This app displays an order form to order coffee.
 */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    int topping_price = 0;
    /**
     * This method is called when the order button is clicked.
     */
    public void submitOrder(View view) {
        String variable1,variable2,variable3;
        CheckBox cream = (CheckBox) findViewById(R.id.checkbox);
        CheckBox choco = (CheckBox) findViewById(R.id.checkbox_choco);
        boolean value1 = cream.isChecked();
        boolean value2 = choco.isChecked();

        if(value1 == true) {
            variable1 = "Yes";
            topping_price += 1;
        }
        else
            variable1 = "No" ;

        if(value2 == true) {
            variable2 = "Yes";
            topping_price += 2;
        }

        else
            variable2 = "No" ;

        EditText input_name = (EditText) findViewById(R.id.name);
        variable3 = input_name.getText().toString();

        if((quantity + count) == 0){
            topping_price = 0;
            variable1 = "No";
            variable2 = "No";
        }

        String price_message = "Whipped cream topping: " + variable1 + "\nChocolate topping: " + variable2 +"\nName: " + variable3 +"\nQuantity: " + (quantity + count) +"\nTotal $" + (( quantity + count ) * 10 + topping_price ) + "\nThank You";
        displayMessage(price_message);
        topping_price = 0 ;

        Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
        sendIntent.setType("*/*");
        sendIntent.setData(Uri.parse("[email protected]"));
        sendIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
        sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" });
        sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Just Java order for " + variable3);
        sendIntent.putExtra(Intent.EXTRA_TEXT,"Whipped cream topping: " + variable1 + "\nChocolate topping: " + variable2 +"\nName: " + variable3 +"\nQuantity: " + (quantity + count) +"\nTotal $" + (( quantity + count ) * 10 + topping_price ) + "\nThank You");
//        startActivity(sendIntent);
        if (sendIntent.resolveActivity(getPackageManager()) != null) {
            startActivity(sendIntent);
        }    
    }

    /**
     * This method displays the given text on the screen.
     */
    private void displayMessage(String message) {
        TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
        priceTextView.setText(message);
    }

    int quantity = 1;
    int count = 0;

    public void increment(View view){
        if((quantity + count) < 101)
            count = count + 1;
        display(quantity + count );
        displayPrice( (quantity + count) * 10);
    }

    public void decrement(View view){

        if((quantity + count) != 0)
            count = count - 1;

        display(quantity + count);
        displayPrice((quantity + count) * 10);

    }
    /**
     * This method displays the given quantity value on the screen.
     */
    private void display(int number) {
        TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
        quantityTextView.setText("" + number);
    }

    private void displayPrice(int number){
        TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
        priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
    }
}

Upvotes: 2

Views: 4498

Answers (4)

Qwerty
Qwerty

Reputation: 11

use intent.setType("message/rfc822") in the Intent

Upvotes: 1

Rajesh Gosemath
Rajesh Gosemath

Reputation: 1872

If you specifically want to open gmail app then use this

Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("[email protected]"));
sendIntent.setClassName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail");
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" });
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Test");
sendIntent.putExtra(Intent.EXTRA_TEXT, "Test");
startActivity(sendIntent);

but this code may fail if the package name changes or if the package name does not exist.So better use this(along with intent chooser)

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
            "mailto", emailId, null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "");
emailIntent.putExtra(Intent.EXTRA_TEXT, "");
startActivity(Intent.createChooser(emailIntent, "Send email..."));

Upvotes: 1

priyanka kataria
priyanka kataria

Reputation: 417

You can try with this code. This will open chooser from where you can select gmail app.

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_TEXT, "Text you want to share");
startActivity(Intent.createChooser(intent, "Send mail..."));

Upvotes: 1

Karan Sharma
Karan Sharma

Reputation: 2599

To open the Gmail App, use the following code:

Intent mailClient = new Intent(Intent.ACTION_VIEW);
mailClient.setClassName("com.google.android.gm", "com.google.android.gm.ConversationListActivity");
startActivity(mailClient);

To have the fields to, subject and body pre-filled, you can put extras in the intent like this:

mailClient.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" });
mailClient.putExtra(Intent.EXTRA_SUBJECT, "hello subject");
mailClient.putExtra(Intent.EXTRA_TEXT, "hello message");

Upvotes: 0

Related Questions