How to Open Calculator App from Another App using Intent

How to Open Calculator App from Another App using Intent

How to call Android calculator on my app for all phones

Only for HTC Mobile

App: Calculator
Version: 7.0 (24)
Languages: 72
Package: com.android.calculator2

  public static final String CALCULATOR_PACKAGE ="com.android.calculator2";
  public static final String CALCULATOR_CLASS ="com.android.calculator2.Calculator";
  Intent intent = new Intent();

    intent.setAction(Intent.ACTION_MAIN);
         intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setComponent(new ComponentName(
         CALCULATOR_PACKAGE,
         CALCULATOR_CLASS));

This below code only works for Samsung Galaxy S3

  public static final String CALCULATOR_PACKAGE ="com.sec.android.app.popupcalculator";
  public static final String CALCULATOR_CLASS ="com.sec.android.app.popupcalculator.Calculator";

But If you need a code that works for all android devices then below code is for you. Here is the example code:

you can try as to Default calculator on all android devices and this code will work perfectly for all android devices:

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);
 }
}

and now you can launch calculator application as:

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
   }

And for Api >= 15 ,You can use

Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_CALCULATOR);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Full Code:

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);
    }
}

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
    Toast.makeText(MainActivity.this, "Calculator not installed", Toast.LENGTH_SHORT).show();
}

Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_CALCULATOR);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

 

If you have any questions, feel free to ask in the comments below. I try my best to respond to every comment that comes my way. If for any reason you don’t get a response, feel free to ask me on TwitterFacebook and if you want to follow me on those social media links as well to see different pictures and just talk about different things going on in the tech world.

Scroll to Top
0 Shares
Share via
Copy link