Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How retrieve values from the Android Calculator

I am developing an application which the user taps on EditText, it displays the android calculator, so the user is able to do arithmetic operations.

I would like to retrieve the final value when the user presses the equal button in the android calculator and display it. Is that possible using the standard android calculator?

The code to open the calculator is the following:

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

where,

String CALCULATOR_PACKAGE ="com.android.calculator2";
String CALCULATOR_CLASS ="com.android.calculator2.Calculator";

Any ideas or suggestion about how to retrieve the final value from the calculator?
Thanks

like image 487
santidoo Avatar asked Nov 13 '22 18:11

santidoo


1 Answers

It's not possible using the standard calculator, but that calculator is open source, so you could create your own calculator. The source contains a class called Logic (Logic.java); if you update the method evaluateAndShowResult() to automatically paste the result to the clipboard, and then do a paste in your app, you'd be ready.

As an alternative, if you really don't need the calculator's UI, you could just take Logic and trash the rest.

like image 146
Joe Malin Avatar answered Dec 15 '22 13:12

Joe Malin