简体   繁体   中英

Intent.CATEGORY_APP_CALCULATOR: ActivityNotFoundException

I'm trying to open the default calculator app in a android application. Two calculators are installed in device: default android calculator and 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:

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? 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-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). The problem is an app that wants to start the activity has no control of the manifest in the target activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM