Real-time access to stock data from the Android app application source sharing _android

Source: Internet
Author: User

Recently Learning Android application development, do not know what to write a program to practice practicing, just recently the stock is very hot, on an app to get stock data real-time, named Mystock. Using the development tools Android Studio, you need to download from the Android website, download the address: http://developer.android.com/sdk/index.html. Unfortunately, Android is Google's, and any Google-related company in the country is not directly accessible, only through VPN access.

The following image opens a screenshot of the project for Android Studio:

Here's a step-by-step introduction to the implementation steps for Mystock.

1. The following are the contents of the Activa_main.xml. The top row is three TextView, respectively used to show the Shanghai Composite Index, Shen, Gem Point. The middle row is a edittext and a button that is used to add stock. Here is a table that shows the list of added stocks.

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:o" rientation= "Vertical" tools:context= ".
    Mainactivity "> <linearlayout android:layout_width=" match_parent "android:layout_height=" Wrap_content " android:orientation= "Horizontal" > <linearlayout android:layout_width= "0dp" android:layout_weight= "0.
      android:layout_height= "wrap_content" android:orientation= "vertical" android:gravity= "center" > <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android: text= "@string/stock_sh_name"/> <textview android:layout_width= "Wrap_content" android:layout_he ight= "Wrap_content" android:id= "@+id/stock_sh_index"/> <textview android:layout_width= "Wrap_co" Ntent "Android:layout_height= "Wrap_content" android:textsize= "12SP" android:id= "@+id/stock_sh_change"/> </Li nearlayout> <linearlayout android:layout_width= "0DP" android:layout_weight= "0.33" Android:layo
        ut_height= "wrap_content" android:orientation= "vertical" android:gravity= "center" > <textview Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/STOCK_SZ
        _name "/> <textview android:layout_width= wrap_content" android:layout_height= "Wrap_content" Android:id= "@+id/stock_sz_index"/> <textview android:layout_width= "Wrap_content" Android : layout_height= "wrap_content" android:textsize= "12SP" android:id= "@+id/stock_sz_change"/> </Line arlayout> <linearlayout android:layout_width= "0DP" android:layout_weight= "0.33" Android:layout
 _height= "Wrap_content"     android:orientation= "vertical" android:gravity= "center" > <textview android:layout_width= "w Rap_content "android:layout_height=" wrap_content "android:text=" @string/stock_chuang_name "/> < TextView android:layout_width= "wrap_content" android:layout_height= wrap_content "android:id=" @+id /stock_chuang_index "/> <textview android:layout_width=" wrap_content "android:layout_height=" WR
  Ap_content "android:textsize=" 12SP "android:id=" @+id/stock_chuang_change "/> </LinearLayout>
    </LinearLayout> <linearlayout android:orientation= "Horizontal" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" > <edittext android:layout_width= "wrap_content" Android:layout_ height= "Wrap_content" android:inputtype= "number" android:maxlength= "6" android:id= "@+id/edittext_stockid "Android:layout_weight= "1"/> <button android:layout_width= "wrap_content" android:layout_height= "Wrap_content"
    android:text= "@string/button_add_label" android:onclick= "Addstock"/> </LinearLayout> <!--ListView Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:id= "@+id/listView"/--> & Lt
      ScrollView android:layout_width= "match_parent" android:layout_height= "wrap_content" > <TableLayout Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:id= "@+id/stock_table" ><

 /tablelayout> </ScrollView> </LinearLayout>

The application screenshot is as follows:

2. Data acquisition, using the interface provided by Sina to get stock data in real time, the code is as follows:

public void Querysinastocks (String list) {
    //instantiate the Requestqueue.
    Requestqueue queue = Volley.newrequestqueue (this);
    String url = "http://hq.sinajs.cn/list=" + list;
    http://hq.sinajs.cn/list=sh600000,sh600536
    //Request A string response from the provided URL.
    Stringrequest stringrequest = new Stringrequest (Request.Method.GET, URL,
        new response.listener<string> () {
          @Override public
          void Onresponse (String response) {
            Updatestocklistview (sinaresponsetostocks ( Response)
          }
        ,
        new Response.errorlistener () {
          @Override public
          void Onerrorresponse ( Volleyerror error) {
          }
        });
    Queue.add (stringrequest);
  }

HTTP requests are sent here using the volley, which requires adding dependencies:compile ' com.mcxiaoke.volley:library:1.0.19 ' inside the build.gradle.

3. Periodically refresh stock data, use timer, send request every two seconds to get the data, the code is as follows:

  Timer timer = new Timer ("Refreshstocks");
    Timer.schedule (New TimerTask () {
      @Override public
      void Run () {
        refreshstocks ();
      }
    }, 0);

  private void Refreshstocks () {
    String ids = "";
    for (String Id:stockids_) {
      ids = = ID;
      ids = + ",";
    }
    Querysinastocks (IDs);
  }

4. Store stock code when the program exits, and the next time you open the app, you can display the last stock list. The code is as follows.

 private void Savestockstopreferences () {
    String ids = "";
    for (String Id:stockids_) {
      ids = = ID;
      ids = + ",";
    }
    Sharedpreferences sharedpref = getpreferences (context.mode_private);
    Sharedpreferences.editor Editor = Sharedpref.edit ();
    Editor.putstring (Stockidskey_, IDS);
    Editor.commit ();
  }
  @Override public
  void OnDestroy () {
    Super.ondestroy ();//Always Call the superclass
    Savestockstopreferences ();
  }

5. Delete the selected stock and add an action in the Menu_main.xml.

<menu xmlns:android= "http://schemas.android.com/apk/res/android"
  xmlns:app= "http://schemas.android.com/ Apk/res-auto "
  xmlns:tools=" Http://schemas.android.com/tools "tools:context=". Mainactivity ">
  <item android:id=" @+id/action_settings "android:title=" @string/action_settings "
    android:orderincategory= "app:showasaction=" "Never"/> <item android:id= "
  @+id/action_delete" Android: title= "@string/action_delete"
    android:orderincategory= "app:showasaction=" Never/>
</menu>

The code responds to the event and deletes:

 @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar would//automatically handle clicks on the Home/up button, so long//As you specify a parent Activ
    ity in Androidmanifest.xml.
    int id = item.getitemid ();
    Noinspection simplifiableifstatement if (id = = r.id.action_settings) {return true;
      else if (id = = R.id.action_delete) {if (Selectedstockitems_.isempty ()) return true;
        for (String selectedid:selectedstockitems_) {stockids_.remove (selectedid);
        Tablelayout table = (tablelayout) Findviewbyid (r.id.stock_table);
        int count = Table.getchildcount ();
          for (int i = 1; i < count; i++) {TableRow row = (TableRow) table.getchildat (i);
          LinearLayout nameId = (linearlayout) row.getchildat (0);
          TextView Idtext = (TextView) nameid.getchildat (1); if (idtext!= null && idtext.gettext (). toString () = =Selectedid) {Table.removeview (row);
          Break
    }} selectedstockitems_.clear ();
  return super.onoptionsitemselected (item);
 }

Screen capture:

6. When a large consignment pending orders, send a message reminder, the code is as follows:

{
...
      String text = "";
      String sbuy = Getresources (). getString (R.string.stock_buy);
      String Ssell = Getresources (). getString (R.string.stock_sell);
      if (double.parsedouble (stock.b1_) >= stocklargetrade_) {text + sbuy + "1:" + stock.b1_ + ",";
      } if (Double.parsedouble (stock.b2_) >= stocklargetrade_) {text + Sbuy + "2:" + stock.b2_ + ",";
      } if (Double.parsedouble (stock.b3_) >= stocklargetrade_) {text + Sbuy + "3:" + stock.b3_ + ",";
      } if (Double.parsedouble (stock.b4_) >= stocklargetrade_) {text + Sbuy + "4:" + stock.b4_ + ",";
      } if (Double.parsedouble (stock.b5_) >= stocklargetrade_) {text + Sbuy + "5:" + stock.b5_ + ",";
      } if (Double.parsedouble (stock.s1_) >= stocklargetrade_) {text + Ssell + "1:" + stock.s1_ + ",";
 } if (Double.parsedouble (stock.s2_) >= stocklargetrade_) {text + Ssell + "2:" + stock.s2_ + ",";     } if (Double.parsedouble (stock.s3_) >= stocklargetrade_) {text + Ssell + "3:" + stock.s3_ + ",";
      } if (Double.parsedouble (stock.s4_) >= stocklargetrade_) {text + Ssell + "4:" + stock.s4_ + ",";
      } if (Double.parsedouble (stock.s5_) >= stocklargetrade_) {text + Ssell + "5:" + stock.s5_ + ",";
if (text.length () > 0) sendnotifation (integer.parseint (SID), stock.name_, text);

  ...
} public void sendnotifation (int id, string title, string text) {Notificationcompat.builder NBuilder = new Notif
    Icationcompat.builder (this);
    Nbuilder.setsmallicon (R.drawable.ic_launcher);
    Nbuilder.setcontenttitle (title);
    Nbuilder.setcontenttext (text);
    Nbuilder.setvibrate (new long[]{100, 100, 100});
    Nbuilder.setlights (color.red, 1000, 1000);
    Notificationmanager notifymgr = (notificationmanager) getsystemservice (Notification_service);
  Notifymgr.notify (ID, nbuilder.build ()); }

Screen capture:


The above through an illustrated way to share a real-time access to the stock data of the Android app application source code, I hope you like.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.