-1

I am creating an app for currency exchanging. To Convert USD to foreign currencies. I have created an app but the problem is that I have to manually update the rate for the currency. Is their anyway for my app to get the rates to automatically update by itself. Here is my java code for the app. Look in my function for ConvertUSDtoEuro() and ConvertEurotoUSD(). I hope everything was clear.

 package com.example.currencyconverter;

 import android.app.Activity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.EditText;
 import android.widget.RadioButton;

 public class MainActivity extends Activity 
 {
  private EditText text;

  @Override
  public void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text = (EditText) findViewById(R.id.editText1);

  }

  public double ConvertUSDtoEuro(float USD)
  { return ((USD * .667)); }

  public double ConvertEurotoUSD(float Euro)
  { return ((Euro * 1.11)); }     

  public void onClick (View view)
  {
     switch (view.getId())
     {
     case R.id.button1:
         RadioButton USDButton = (RadioButton) findViewById(R.id.radio0);
         RadioButton EuroButton = (RadioButton) findViewById(R.id.radio1);
         float inputvalue = Float.parseFloat(text.getText().toString());

         if (USDButton.isChecked())
         {
             text.setText(String.valueOf(ConvertUSDtoEuro(inputvalue)));
             USDButton.setChecked(true);
             EuroButton.setChecked(false);   
         }

          else 
          {
              text.setText(String.valueOf(ConvertEurotoUSD(inputvalue)));
              USDButton.setChecked(false);
              EuroButton.setChecked(true); 
          }

          break;    
     }
  }

}  

2 Answers2

5

You will need to create a Service which can get this info using Google Finance API or Yahoo Finance api . Check this : How do I get currency exchange rates via an API such as Google Finance?

Community
  • 1
  • 1
DntFrgtDSemiCln
  • 1,259
  • 2
  • 16
  • 35
0

You can use push notification and get notified when update has been made. You can look into Android Cloud to Device messaging. link

Alternative solution is every time user launch app, update values from a service using available API.

If you are developing an application which heavily rely on this currency update, and you somewhat need a real time update value then go with Push service. if your app keep checking update on website it will consume too much resources which is not very good idea. But if it is just another currency converter use a background service.

minhaz
  • 4,233
  • 3
  • 33
  • 49