2
I need to call the calculator from the click on a button, how can I do this? I’m having difficulties, I’m a beginner in Android and lost myself completely in the implementation of this code.
2
I need to call the calculator from the click on a button, how can I do this? I’m having difficulties, I’m a beginner in Android and lost myself completely in the implementation of this code.
3
The difficulty is that some manufacturers replace the pure Android standard calculator (package com.android.calculator2
) by their own apps, which stay in a different package, then it becomes difficult to guess which is the calculator package.
But you can try to locate the standard calculator like this:
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 then you can call the app by doing:
if(items.size()>=1){
String packageName = (String) items.get(0).get("packageName");
Intent i = pm.getLaunchIntentForPackage(packageName);
if (i != null) {
startActivity(i);
} else{
// Aplicativo não encontrado
}
}
Source: Soen
3
In this post http://www.intertech.com/Blog/android-intents-for-app-integration-call-a-calculator-play-video-open-an-editor/ found the following code:
You have to use Intent
, add these variables to your class MyActivity
:
private static final String CALCULATOR_PACKAGE_NAME = "com.android.calculator2";
private static final String CALCULATOR_CLASS_NAME = "com.android.calculator2.Calculator";
Add this method to the other methods:
public void launchCalculator()
{
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new Component Name(CALCULATOR_PACKAGE_NAME,
CALCULATOR_CLASS_NAME));
try {
this.start Activity(intent);
} catch (ActivityNotFoundException noSuchActivity) {
// handle exception where calculator intent filter is not registered
}
}
You use launchCalculator
in the event your button click, for example:
final Button button = (Button) findViewById(R.id.ID_DO_SEU_BOTAO);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
launchCalculator(); //Chama o Intent
}
});
For some time I do not work with Java and Android, I may have wrong something in the code, let me know if it fails.
Intent
: http://developer.android.com/reference/android/content/Intent.htmlBrowser other questions tagged android
You are not signed in. Login or sign up in order to post.
It went round and round, thank you.
– fernando menezes carvalho