How to open Calculator using intent?

1

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

Thank you.

    
asked by anonymous 30.05.2016 / 17:52

2 answers

1

Live,  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 - > link

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

link

    
30.05.2016 / 18:51
1

You should use a Intent along with the startActivity () method;

From what you can read in the documentation, to launch the calculator the Intent should 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 does not 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);
    
30.05.2016 / 18:41