-1

I need to launch the default android calculator app. I found similar quesition on this forum, but it doesn't solve my problem. How to call Android calculator on my app for all phones

It seems like this code works for older versions of android OS. But not for android 11 (Probably cause of that https://developer.android.com/about/versions/11/privacy/package-visibility). So i tried to add in the manifest

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" /> 

It doesn't works too. How can i solve this problem?

Thanks.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Ectario
  • 13
  • 3
  • 1
    "For that i've tried this" -- please edit your question and post your code. There are several answers on the question that you linked to. We do not know which of those you are using, and we do not know what modifications you made to what is in the answer that you chose. Note that none of those answers seems to follow [the documented instructions for `CATEGORY_APP_CALCULATOR`](https://developer.android.com/reference/android/content/Intent#CATEGORY_APP_CALCULATOR). – CommonsWare Aug 01 '21 at 18:58
  • Thanks for answering. I tried the most upvoted and the last. The problem with CATEGORY_APP_CALCULATOR is the same than https://stackoverflow.com/questions/42902805/intent-category-app-calculator-activitynotfoundexception and the last answer just restart my activity. – Ectario Aug 01 '21 at 19:18

1 Answers1

0

The makeMainSelectorActivity() approach is the right one, but we can try a direct approach as a fallback:

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    try {
      startActivity(
        Intent.makeMainSelectorActivity(
          Intent.ACTION_MAIN,
          Intent.CATEGORY_APP_CALCULATOR
        )
          .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
      )
    } catch (t: Throwable) {
      try {
        startActivity(
          Intent(Intent.ACTION_MAIN)
            .addCategory(Intent.CATEGORY_APP_CALCULATOR)
            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        )
      } catch (t: Throwable) {
        Toast.makeText(
          this,
          "Sorry, I cannot find a calculator",
          Toast.LENGTH_LONG
        ).show()
      }
    }
  }
}

The Toast would be replaced by whatever you want as an error state if you cannot find a calculator app by either means. Here are the results on a variety of test devices:

  • Pixel 4/Android 11: launches the stock calculator app

  • Samsung Galaxy S8/Android 10: Toast, because Samsung developers did not advertise their calculator app as being a calculator app, so third-party apps like yours have no way to identify it

  • Nokia 7.1/Android 10: launches the stock calculator app

  • LG V20/Android 8: launches the stock calculator app

  • Pixel 3XL/Android 12 Beta: launches the stock calculator app

  • Samsung Galaxy A50/Android 11: Toast, probably for the same reason as the Galaxy S8

You are certainly welcome to try some Intent objects for specific calculator apps, such as Samsung's, in hopes of improving your match rate. But, in the end:

  • Manufacturers do not have to include a calculator app
  • The current user might not have access to the calculator app (e.g., restricted profile)
  • An app that is a calculator might not advertise itself as a calculator app via CATEGORY_APP_CALCULATOR, in which case there is no guaranteed way to find out that the app is a calculator app

So, you are going to need to settle for many, but not all, of your users being able to start the calculator app.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Thank you very much for all these explanations. Since I only need basic operations, I think it makes more sense to code a calculator myself. Thanks a lot. – Ectario Aug 01 '21 at 20:26