[英]Intent.CATEGORY_APP_CALCULATOR: ActivityNotFoundException
I'm trying to open the default calculator app in a android application. 我正在尝试在Android应用程序中打开默认计算器应用程序。 Two calculators are installed in device: default android calculator and Google Calculator .
设备中安装了两个计算器:默认的Android计算器和Google Calculator 。
Intent calc = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_CALCULATOR);
startActivity(calc);
This code is throwing an ActivityNotFoundException and shows this in logcat: 此代码抛出ActivityNotFoundException并在logcat中显示:
system_process W/IntentResolver: resolveIntent failed: found match, but none with CATEGORY_DEFAULT
This code have the same behavior: 此代码具有相同的行为:
Intent calc = new Intent(Intent.ACTION_MAIN);
calc.addCategory(Intent.CATEGORY_APP_CALCULATOR);
startActivity(calc);
It's a Android bug? 这是一个Android bug? How to open the application chooser, to let user select the default app?
如何打开应用程序选择器,让用户选择默认应用程序?
Stack Trace: 堆栈跟踪:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] sel=act=android.intent.action.MAIN cat=[android.intent.category.APP_CALCULATOR]} }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1805)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1514)
at android.app.Activity.startActivityForResult(Activity.java:3930)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivityForResult(Activity.java:3890)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:4213)
at android.app.Activity.startActivity(Activity.java:4181)
at com.MyActivity.openCalc(MyActivity.java:202)
at com.MyActivity.onOptionsItemSelected(MyActivity.java:191)
at android.app.Activity.onMenuItemSelected(Activity.java:2914)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:408)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195)
at android.app.ActivityThread.main(ActivityThread.java:5461)
It seems like the way, that documentation points to doesn't work. 这似乎是文档指向的方式不起作用。 Nevertheless, this code will normally open default calculator app.
不过,此代码通常会打开默认计算器应用程序。
Intent intent = new Intent();
intent.setClassName("com.android.calculator2", "com.android.calculator2.Calculator");
startActivity(intent);
The above answer works, but it does not answer the question. 上面的答案有效,但它没有回答这个问题。 The question is how to launch a calculator implicitly via a category.
问题是如何通过类别隐式启动计算器。 The stock calculator (not the google play version) has the following in the manifest:
股票计算器(不是谷歌播放版本)在清单中有以下内容:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.APP_CALCULATOR" />
</intent-filter>
The problem is simple. 问题很简单。 If the intent filter is modified as shown below the calculator can be started (tested on Pixel2 running 8.1):
如果如下所示修改了intent过滤器,则可以启动计算器(在运行8.1的Pixel2上测试):
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.APP_CALCULATOR" />
</intent-filter>
The DEFAULT
category is required (and this is clearly documented). DEFAULT
类别是必需的(这是明确记录的)。 The problem is an app that wants to start the activity has no control of the manifest in the target activity. 问题是想要启动活动的应用程序无法控制目标活动中的清单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.