How retrieve values from the Android Calculator

拥有回忆 提交于 2019-12-10 14:19:22

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/13239942/how-retrieve-values-from-the-android-calculator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!