我正在尝试创建一个能够轻松打开Gmail的应用程序。我使用两个函数:startApplication和launchApp:
public void startApplication(String packageName){
try
{
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
List<ResolveInfo> resolveInfoList = getPackageManager().queryIntentActivities(intent, 0);
for(ResolveInfo info : resolveInfoList)
if(info.activityInfo.packageName.equalsIgnoreCase(packageName))
{
toast = Toast.makeText(MainActivity.this, "Gmail opened", Toast.LENGTH_LONG);
toast.show();
launchApp(info.activityInfo.packageName, info.activityInfo.name);
return;
}
}
catch (Exception e)
{
toast = Toast.makeText(MainActivity.this, "Can't find Gmail", Toast.LENGTH_LONG);
toast.show();
}
}
这是launchApp函数:
private void launchApp(String packageName, String name)
{
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.setComponent(new ComponentName(packageName, name));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
我从MainActivity的onCreate函数调用
startApplication( “com.gmail”);
我尝试在模拟器和智能手机上运行此应用程序,但程序崩溃。错误是什么?
答案 0 :(得分:2)
最后,我解决了使用startApplication("com.gmail")
startApplication("com.google.android.gm")
的问题