How to open the Calculator using Intent?

Asked

Viewed 1,497 times

1

I would like to know how to call a Native application in my application. I would like to put a calculator menu, when user clicked, open android calculator.

Thank you.

  • I haven’t yet. I’m going to open another topic to explain it better. Can I ? Because I wanted to put parts of my code here to better understand. And by the answer here is character limit.

  • The focus of this question is how to open the calculator, if none of the answers answers should comment on each of them why. You should only create another question if the focus is different. Anyway you can always edit the question and add more information.

2 answers

1

Must use a Intent together with the startActivity method();

From what can be read in the documentation, to launch the calculator Intent shall indicate the category CATEGORY_APP_CALCULATOR:

Intent intent = new Intent();
intent.makeMainSelectorActivity (Intent.ACTION_MAIN,
            Intent.CATEGORY_APP_CALCULATOR);
startActivity(intent);

I tested the code but doesn’t work.

I found this here that I tested and works(at least in the emulator).

Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName("com.android.calculator2",
                                      "com.android.calculator2.Calculator"));
startActivity(intent);

1

Hurrah, the following code ensures that your app opens the calculator on any device.

ArrayList<HashMap<String,Object>> items =new ArrayList<HashMap<String,Object>>();
final PackageManager pm = getPackageManager();
List<PackageInfo> packs = pm.getInstalledPackages(0);  
for (PackageInfo pi : packs) {
if( pi.packageName.toString().toLowerCase().contains("calcul")){
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("appName", pi.applicationInfo.loadLabel(pm));
    map.put("packageName", pi.packageName);
    items.add(map);
 }
}

//You can now open the calculator by running:

if(items.size()>=1){
String packageName = (String) items.get(0).get("packageName");
Intent i = pm.getLaunchIntentForPackage(packageName);
if (i != null)
  startActivity(i);
} 
else{
      // Application not found
}

This info was found here -> https://stackoverflow.com/questions/13662506/how-to-call-android-calculator-on-my-app-for-all-phones

Take a look at the Android documentation in the link below, to learn how to open other native apps.

https://developer.android.com/guide/components/intents-common.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.