santidoo
santidoo

Reputation: 1961

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

Upvotes: 5

Views: 1857

Answers (2)

Michele
Michele

Reputation: 6131

You can start the Calculator with an startActivityForResult to get the Result back.

Here is an Example

Intent myIntent = new Intent();
myIntent.setClassName("com.android.calculator2", "com.android.calculator2.Calculator");
startActivityForResult(myIntent, CALC_INTENT_RETURN);

AND

   /**
     * Framework method called when return from a launched intent is available.
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode,
        Intent data) {
    if (requestCode == CALC_INTENT_RETURN) {
        if (resultCode == RESULT_OK) {
                    mCalculator.setFeatureValue(mCurrentInputField, Double.parseDouble(data.getAction()));
                    resetValue();
        }
    }
}

stripped down from http://code.google.com/p/android-labs/source/browse/trunk/BistroMath/src/com/google/android/bistromath/BistroMath.java?r=6

Upvotes: -1

Joe Malin
Joe Malin

Reputation: 8641

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.

Upvotes: 2

Related Questions