Android打开其他应用的Activity的稳妥方法

如题,为了避免异常,也就是说如果那个Activity不存在,或者启动失败,那么会导致异常,稳妥的方式如下:

                Intent intent = new Intent();
                intent.setClassName("setting.ui", "setting.ui.VrActivity");
                if (getPackageManager().resolveActivity(intent,
                        PackageManager.MATCH_DEFAULT_ONLY) != null) {
                    try {
                        startActivity(intent);
                    } catch (ActivityNotFoundException e) {
                        Log.d(TAG, "Can't find activity " + e.getMessage());
                    } catch (Exception e) {
                        Log.d(TAG, "Can't find activity Exception : " + e.getMessage());
                    }
                } else {
                    Log.d(TAG, "Can't find activity");
                }

Tagged: ,

Comments are closed.