How to invoke the Android calculator from an app
Asked Answered
E

2

8

I want to invoke the android calculator from within my app. I don't want to pass anything to it, just provide a quick launch button to it. Can anyone show me how or point me to an example of how to do this?

Eyeful answered 18/6, 2010 at 12:34 Comment(0)
P
13

Taken from here:

Intent i = new Intent();
i.setClassName("com.android.calculator2",
               "com.android.calculator2.Calculator");
startActivity(i); 
Plato answered 18/6, 2010 at 12:41 Comment(1)
Better catch a PackageManager.NameNotFoundException for Samsung phones. See https://mcmap.net/q/792784/-how-to-call-android-calculator-on-my-app-for-all-phonesFief
G
0

Try this if you don't know the package name of your Calculator App:

First load all apps to Array

// Declare universal if you want Access any where from scope

ArrayList<HashMap<String,Object>> items;
    PackageManager pm ;    
List<PackageInfo> packs;

    // initialise From Oncreate if you want
    items =new  ArrayList<HashMap<String,Object>>(); 
    pm = getPackageManager();
    packs = pm.getInstalledPackages(0);  
        for (PackageInfo pi : packs)
 {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("appName", pi.applicationInfo.loadLabel(pm));
            map.put("packageName", pi.packageName);
            items.add(map); 
 }

Then traverse through all apps to get App Named or Matches "Calculator"

public void opencalculator(){
  int d=0;
  if(items.size()>=1){
  int j=0;
 for(j=0;j<items.size();j++){
 String AppName = (String) items.get(j).get("appName");
// Log.w("Name",""+AppName);
    if(AppName.matches("Calculator"))
                     {
                            d=j;
                            break;
                     }
                    }
                    String packageName = (String) items.get(d).get("packageName");

                    Intent i = pm.getLaunchIntentForPackage(packageName);
                    if (i != null){
                        Toast.makeText(getContext(),"Starting Calculator",Toast.LENGTH_SHORT).show();

                        startActivity(i);}
                    else {
                        Toast.makeText(getContext(),"Error opening Calculator!",Toast.LENGTH_SHORT).show();

                    }
                }


                else{
                    Toast.makeText(getContext(),"Error starting Calculator!",Toast.LENGTH_SHORT).show();


                }
}

And then call the function

opencalculator();
Guarani answered 2/4, 2022 at 18:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.