Android 确定当前设备上是否存在活动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2878694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:42:07  来源:igfitidea点击:

Determining if an Activity exists on the current device?

androidandroid-activity

提问by stormin986

Is there a way to check and see if an Activity exists on your device? If I have a youtube video link I want to specify it open in the YouTube PlayerActivity. However, I don't want to crash if for some reason they don't have it.

有没有办法检查您的设备上是否存在活动?如果我有一个 youtube 视频链接,我想指定它在 YouTube PlayerActivity 中打开。但是,如果由于某种原因他们没有它,我不想崩溃。

Is there a way to check and see if the activity exists? I don't think I can catch the runtime exception since startActivity() doesn't throw it.

有没有办法检查活动是否存在?我认为我无法捕获运行时异常,因为 startActivity() 不会抛出它。

采纳答案by yanchenko

I don't think I can catch the runtime exception

我认为我无法捕获运行时异常

Actually, this works:

实际上,这是有效的:

try {
    startActivity(new Intent(..));
} catch (ActivityNotFoundException e) {
    Toast.makeText(this, "Not installed.", LENGTH_SHORT).show();
}

回答by Samuh

You could create an Intent object with necessary component info and then check if the intent is callable or not.I stumbled upon this snippet here on SO, don't have the link to the actual thread.

您可以使用必要的组件信息创建一个 Intent 对象,然后检查该意图是否可调用。我在 SO 上偶然发现了这个片段,没有指向实际线程的链接。

private boolean isCallable(Intent intent) {
        List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, 
            PackageManager.MATCH_DEFAULT_ONLY);
        return list.size() > 0;
}

回答by Malcolm

This is the simplest way to do this:

这是执行此操作的最简单方法:

boolean activityExists = intent.resolveActivityInfo(getPackageManager(), 0) != null;

It is also the one recommended by Google:

这也是谷歌推荐的

To first verify that an app exists to receive the intent, call resolveActivity()on your Intent object. If the result is non-null, there is at least one app that can handle the intent and it's safe to call startActivity(). If the result is null, you should not use the intent and, if possible, you should disable the feature that invokes the intent.

要首先验证应用程序是否存在以接收 Intent,请调用resolveActivity()您的 Intent 对象。如果结果不为空,则至少有一个应用程序可以处理该意图,并且可以安全地调用startActivity(). 如果结果为空,则不应使用该意图,如果可能,应禁用调用该意图的功能。

回答by user979247

I ended up doing:

我最终做了:

        Intent intent = new Intent();
        intent.setClassName( "com.google.android.gsf", "com.google.android.gsf.login.AccountIntroActivity" );

        if(getContext().getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
            getContext().startActivity( intent );
        } else {
            getContext().startActivity(new Intent(Settings.ACTION_ADD_ACCOUNT));
        }

This ensures that the google-specific Add Account intent exists, and if not, falls back on the general more general ACTION_ADD_ACCOUNTS.

这确保了特定于 google 的添加帐户意图存在,如果不存在,则依靠更通用的 ACTION_ADD_ACCOUNTS。

回答by Sakiboy

Here's how I check if an Activityis available on the device:

这是我检查Activity设备上是否可用的方法:

        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tell//:" + phoneNumber));

        PackageManager manager = context.getPackageManager();
        List<ResolveInfo> activities = manager.queryIntentActivities(
                intent, 0);
        if (!manager.hasSystemFeature(
                PackageManager.FEATURE_TELEPHONY) || activities == null || activities
                .size() < 1) {
            Toast.makeText(
                    context,
                    "Sorry, there were no apps that worked with that request.",
                    Toast.LENGTH_SHORT).show();
        } else {
            context.startActivity(intent);
        }