单击按钮从 android 应用程序打开 gmail 时应用程序崩溃
App is Crashing while opening gmail from android app on clicking a button
我有一个 ImageView 按钮,可以在我的 android 应用程序中打开 gmail 撰写邮件选项卡,通过 intent。
以前使用相同的代码打开它。
imageView3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent send=new Intent(Intent.ACTION_VIEW);
send.setType("plain/text");
send.setData(Uri.parse("testmail@gmail.com"));
send.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
startActivity(send);
}
});
现在应用程序在点击按钮后崩溃。
用这个替换你的代码。如果您的应用程序不再崩溃,则表示您的 android 设备中没有 Gmail 应用程序。
imageView3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
try
{
Intent send=new Intent(Intent.ACTION_VIEW);
send.setType("plain/text");
send.setData(Uri.parse("testmail@gmail.com"));
send.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
startActivity(send);
}
catch (ActivityNotFoundException ex)
{
ex.printStackTrace();
}
}
});
终于找到答案了,那段代码真的很忙
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/html");
final PackageManager pm = getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0);
ResolveInfo best = null;
for (final ResolveInfo info : matches) {
if (info.activityInfo.packageName.endsWith(".gm") || info.activityInfo.name.toLowerCase().contains("gmail")) {
best = info;
break;
}
}
if (best != null) {
intent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
}
intent.setData(Uri.parse("mailto:emailto@gmail.com"));
try {
startActivity(intent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "Error Sending Email,Try Later.", Toast.LENGTH_SHORT).show();
}
我有一个 ImageView 按钮,可以在我的 android 应用程序中打开 gmail 撰写邮件选项卡,通过 intent。 以前使用相同的代码打开它。
imageView3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent send=new Intent(Intent.ACTION_VIEW);
send.setType("plain/text");
send.setData(Uri.parse("testmail@gmail.com"));
send.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
startActivity(send);
}
});
现在应用程序在点击按钮后崩溃。
用这个替换你的代码。如果您的应用程序不再崩溃,则表示您的 android 设备中没有 Gmail 应用程序。
imageView3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
try
{
Intent send=new Intent(Intent.ACTION_VIEW);
send.setType("plain/text");
send.setData(Uri.parse("testmail@gmail.com"));
send.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
startActivity(send);
}
catch (ActivityNotFoundException ex)
{
ex.printStackTrace();
}
}
});
终于找到答案了,那段代码真的很忙
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/html");
final PackageManager pm = getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0);
ResolveInfo best = null;
for (final ResolveInfo info : matches) {
if (info.activityInfo.packageName.endsWith(".gm") || info.activityInfo.name.toLowerCase().contains("gmail")) {
best = info;
break;
}
}
if (best != null) {
intent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
}
intent.setData(Uri.parse("mailto:emailto@gmail.com"));
try {
startActivity(intent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "Error Sending Email,Try Later.", Toast.LENGTH_SHORT).show();
}