启动activity的重载类

这篇博客详细介绍了在Android中启动Activity的多种方法,包括通过Intent、Action、包名+类名、ComponentName以及特定场景的启动方式如选择应用和小部件。提供了静态公共方法供开发者使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

启动activity的方式
 public static boolean startActivity(Context context, Intent intent) {
        if (intent == null) {
            return false;
        }

        try {
            context.startActivity(intent);
            return true;
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean startActivity(Context context, String action) {
        if (action == null || action.length() == 0) {
            return false;
        }

        return startActivity(context, new Intent(action));
    }

    public static boolean startActivity(Context context, String pkg, String cls) {//androidMenifest 中的包名 和 类名
        if (pkg == null || pkg.length() == 0 || cls == null
                || cls.length() == 0) {
            return false;
        }

        final Intent intent = new Intent();
        intent.setClassName(pkg, cls);
        return startActivity(context, intent);
    }

    public static boolean startActivity(Context context, ComponentName cpn) {
        if (cpn == null) {
            return false;
        }

        final Intent intent = new Intent();
        intent.setComponent(cpn);
        return startActivity(context, intent);
    }

    public static boolean startActivity(Context context, AppInfo app) {
        if (app == null) {
            return false;
        }

        return startActivity(context, app.cpnName);
    }

    public static boolean startActivityForPickApp(Context context, int category) {
        try {
            final Intent intent = new Intent(context, AppsActivity.class);
            intent.putExtra(AppsActivity.EXT_MODE,
                    AppsActivity.MODE_PICK_ONE_APP);
            intent.putExtra(AppsActivity.EXT_CATEGORY, category);
            if (context instanceof Activity) {
                ((Activity) context).startActivityForResult(intent,
                        Launcher.REQ_CODE_PICK_ONE_APP);
                return true;
            } else {
                context.startActivity(intent);
                return true;
            }
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean startActivityForPickWidget(Context context,
            int widgetId) {
        try {
            final Intent intent = new Intent(
                    AppWidgetManager.ACTION_APPWIDGET_PICK);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
            if (context instanceof Activity) {
                ((Activity) context).startActivityForResult(intent,
                        Launcher.REQ_CODE_PICK_ONE_WIDGET);
                return true;
            } else {
                context.startActivity(intent);
                return true;
            }
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值