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