该应用程序有一个订单按钮,当点击它时应该打开gmail应用程序主题和正文提交,但我的代码中的意图不起作用,应用程序显示一个消息,它不幸停止。我已经尝试了很多方法来实现它,但我无法这样做。
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("masquerade0097@gmail.com"));
sendIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "masquerade0097@gmail.com" });
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));
}
}
答案 0 :(得分:1)
您可以尝试使用此代码。这将打开选择器,您可以从中选择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..."));
答案 1 :(得分:1)
如果您特别想打开gmail应用,请使用此
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("test@gmail.com"));
sendIntent.setClassName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail");
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "test@gmail.com" });
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Test");
sendIntent.putExtra(Intent.EXTRA_TEXT, "Test");
startActivity(sendIntent);
但是如果包名称更改或包名称不存在,此代码可能会失败。更好地使用它(以及意图选择器)
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..."));
答案 2 :(得分:1)
在意图中使用intent.setType("message/rfc822")
答案 3 :(得分:0)
要打开Gmail应用,请使用以下代码:
Intent mailClient = new Intent(Intent.ACTION_VIEW);
mailClient.setClassName("com.google.android.gm", "com.google.android.gm.ConversationListActivity");
startActivity(mailClient);
要预先填写字段,主题和正文,您可以在意图中添加额外内容,如下所示:
mailClient.putExtra(Intent.EXTRA_EMAIL, new String[] { "hello@gmail.com" });
mailClient.putExtra(Intent.EXTRA_SUBJECT, "hello subject");
mailClient.putExtra(Intent.EXTRA_TEXT, "hello message");