我的应用中有4个按钮,其中2个按钮需要检查我要运行的应用程序是否在我的设备上。如果存在,则运行;否则,显示toast提示。
问题是当我显示toast提示时,应用程序会关闭。在startActivity(Intent)中显示Toast提示的最佳方法是什么?
这是代码:
public void onClick(View v) {
Intent LaunchIntent = null;
boolean isTouch = isAppInstalled("com.enflick.android.ping");
boolean isWpress = isAppInstalled("org.wordpress.android");
switch (v.getId()) {
case (R.id.botonPortafoli):
LaunchIntent = new Intent(this, PortafoliActivity.class);
break;
case (R.id.botonSocial):
LaunchIntent = new Intent(this, SocialActivity.class);
break;
case (R.id.botonTouch):
if (isTouch) {
LaunchIntent = getPackageManager().getLaunchIntentForPackage(
"com.enflick.android.ping");
} else {
Toast.makeText(this, R.string.no_inst, Toast.LENGTH_LONG)
.show();
break;
}
break;
case (R.id.botonWpress):
if (isWpress) {
LaunchIntent = getPackageManager().getLaunchIntentForPackage(
"org.wordpress.android");
break;
} else {
Toast.makeText(this, R.string.no_inst, Toast.LENGTH_LONG)
.show();
break;
}
}
startActivity(LaunchIntent);
PackageManager.NameNotFoundException
更改为Exception
,并确保在应用程序未安装时该方法不返回true。 - HericDenis