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

the above works for HTC only

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

the above works for S3

I need a code that works for all of them. Someone with any clue ?Please give an example code

6
  • try this example myandroidsolutions.blogspot.in/2012/06/… Dec 1, 2012 at 18:46
  • 1
    @kumaand This is basically the same...
    – Ahmad
    Dec 1, 2012 at 18:47
  • @kumaand that only works for HTC but not S3. I have independent codes for both HTC and S3 and both are working. I need one that will work for all of them. Dec 1, 2012 at 18:52
  • best way is use PackageManager to get the internal application package name and find calculator in all Strings if found then extract main class name or package for starting it.from this way you will support all device or tablets for launching default calculator Dec 1, 2012 at 18:57
  • @imran Khan I had the same idea but the implementation is an issue. Please help me with an example. It will give me a head start. Thank you Dec 1, 2012 at 19:04

4 Answers 4

39

you can try as to Default calculator on 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);
6
  • i'm sure this code will work on all devices for launching calculator application Dec 1, 2012 at 19:29
  • let me try it will tell u in a min Dec 1, 2012 at 19:47
  • Thank you big !! it worked man Thanks alot it is working for all phones Dec 1, 2012 at 19:53
  • 1
    I have tested on both HTC ONE X and Sam Sung S3 devices. You really saved me alot !! :) Dec 1, 2012 at 19:59
  • On my device, launched the "Solution Calculator" chemistry application.
    – Noumenon
    Jul 25, 2013 at 15:16
4
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_APP_CALCULATOR);
startActivity(i);
1

You'll have to check if the default calculator package name(com.android.calculator2) is available with this:

try{
     ApplicationInfo info = getPackageManager()
                             .getApplicationInfo("com.android.calculator2", 0 );

    } catch( PackageManager.NameNotFoundException e ){
     //application doesn't exist
}

Samsung uses a lot of apps preinstalled with TouchWiz which aren't available in stock Android. Therefore if you get an Exception in the above you can check if Samsung's calculator is available. Btw I think com.android.calculator2 is not HTC specific.

2
  • @Ahmed everything is ok with HTC One X now how do i get both HTC and S3 working. am not getting any exception. Do you have any idea of how to go about it. How do i get the package and the class of the calculator Dec 1, 2012 at 19:32
  • Samsung's calculator on my phone is com.sec.android.app.popupcalculator .
    – Noumenon
    Jul 25, 2013 at 15:32
0

Well this is a Modified Answer of @ρяσѕρєя K becouse it works fine in samsung mobiles and only packages have "calc" but not all mobiles like HTC AND LENOVO ETC

And for Api >= 15 ,You can use BUT !!!

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

THIS MAY CAN CAUSE ERROR LIKE THIS

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.MAIN cat=[android.intent.category.APP_CALCULATOR] flg=0x10000000 }

SO LETS TAKE A LOOK AT THIS

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

THIS IS TRICK PART We are Traversing 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",Toast.LENGTH_SHORT).show();

                        startActivity(i);}
                    else {
                        Toast.makeText(getContext(),"SORRY I CANT OPEN CALCULATOR :(",Toast.LENGTH_SHORT).show();

                    }
                }


                else{
                    Toast.makeText(getContext(),"SORRY I CANT START CALCULATOR :(",Toast.LENGTH_SHORT).show();


                }
}

CALL OPENCALCULATOR

opencalculator();

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.