关键字:Stock,股票,安卓,Android Studio。

OS:Windows 10。

最近学习Android应用开发,不知道写一个什么样的程序来练练手,正好最近股票很火,就一个App来实时获取股票数据,取名为Mystock。使用开发工具Android Studio,需要从Android官网下载,下载地址:http://developer.android.com/sdk/index.html。不幸的是Android是Google公司的,任何和Google公司相关的在国内都无法直接访问,只能通过VPN访问。

下图为Android Studio打开一个工程的截图:

下面按步介绍Mystock的实现步骤。

1.以下是activa_main.xml的内容。上面一排是三个TextView,分别用来显示上证指数,深圳成指,创业板指。中间一排是一个EditText和一个Button,用来添加股票。下面是一个Table,用来显示添加的股票列表。

<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:orientation="vertical"  tools:context=".MainActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><LinearLayoutandroid:layout_width="0dp"android:layout_weight="0.33"android:layout_height="wrap_content"android:orientation="vertical"android:gravity="center" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/stock_sh_name"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/stock_sh_index"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="12sp"android:id="@+id/stock_sh_change"/></LinearLayout><LinearLayoutandroid:layout_width="0dp"android:layout_weight="0.33"android:layout_height="wrap_content"android:orientation="vertical"android:gravity="center" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/stock_sz_name"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/stock_sz_index"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="12sp"android:id="@+id/stock_sz_change"/></LinearLayout><LinearLayoutandroid:layout_width="0dp"android:layout_weight="0.33"android:layout_height="wrap_content"android:orientation="vertical"android:gravity="center" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/stock_chuang_name"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/stock_chuang_index"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="12sp"android:id="@+id/stock_chuang_change"/></LinearLayout></LinearLayout><LinearLayoutandroid:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"><EditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:inputType="number"android:maxLength="6"android:id="@+id/editText_stockId"android:layout_weight="1" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/button_add_label"android:onClick="addStock" /></LinearLayout><!--ListViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/listView" /--><ScrollViewandroid:layout_width="match_parent"android:layout_height="wrap_content"><TableLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/stock_table"></TableLayout></ScrollView>
</LinearLayout>

应用截图如下:

2.数据获取,这里使用sina提供的接口来实时获取股票数据,代码如下:

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>() {@Overridepublic void onResponse(String response) {updateStockListView(sinaResponseToStocks(response));}},new Response.ErrorListener() {@Overridepublic void onErrorResponse(VolleyError error) {}});queue.add(stringRequest);}

这里发送Http请求用到了Volley,需要在build.gradle里面添加dependencies:compile 'com.mcxiaoke.volley:library:1.0.19'。

3.定时刷新股票数据,使用了Timer,每隔两秒发送请求获取数据,代码如下:

        Timer timer = new Timer("RefreshStocks");timer.schedule(new TimerTask() {@Overridepublic void run() {refreshStocks();}}, 0, 2000);private void refreshStocks(){String ids = "";for (String id : StockIds_){ids += id;ids += ",";}querySinaStocks(ids);}

4.在程序退出时存储股票代码,下次打开App时,可以显示上次的股票列表。代码如下。

    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();}@Overridepublic void onDestroy() {super.onDestroy();  // Always call the superclass
saveStocksToPreferences();}

5.删除选中的股票,在menu_main.xml里面添加一个action。

<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="100" app:showAsAction="never" />
    <item android:id="@+id/action_delete" android:title="@string/action_delete"android:orderInCategory="100" app:showAsAction="never" />
</menu>

代码响应事件并删除:

    @Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();//noinspection SimplifiableIfStatementif (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);}

屏幕截图:

6.当有大额委托挂单时,发送消息提醒,代码如下:

{
...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 NotificationCompat.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());}

屏幕截图:

源代码:https://github.com/ldlchina/Mystock

一个实时获取股票数据的安卓应用程序相关推荐

  1. 实时获取股票数据的android app应用程序源码分享

    From:http://www.jb51.net/article/72449.htm 本文我们分享一个实时获取股票数据的android app应用程序源码分享,可以作为学习使用,本文贴出部分重要代码, ...

  2. 构建股票量化数据库一.实时获取股票数据

    实时获取股票数据 (1)实现步骤 1.通过网络爬虫–>爬取所需要的数据->股票实时价,最高价,最低价等等 2.通过python的->pandas库->进行数据整理清洗 (2)实 ...

  3. 实时获取股票数据,免费!——Python爬虫Sina Stock实战

    ​​ 数量技术宅团队在CSDN学院推出了量化投资系列课程 欢迎有兴趣系统学习量化投资的同学,点击下方链接报名: 量化投资速成营(入门课程) Python股票量化投资 Python期货量化投资 Pyth ...

  4. 获取股票数据【实时更新股票数据、创建你的股票数据】、计算交易指标【买入、卖出信号、计算持仓收益、计算累计收益率】

    在上一次获取股票数据[使用JQData查询行情数据.财务指标.估值指标]学习了使用JQData来查询股票相关数据, 这次则开始一点点构建咱们的量化交易系统了. 量化交易平台功能模块了解: 对于一个量化 ...

  5. Python量化交易实战-10实时获取股票的数据函数封装

    B站配套视频教程观看 实时获取股票的数据函数封装 实现股票数据获取的模块及方法 从这节课开始 我们就开始构建所谓的量化交易系统,量化交易平台功能模块. 上面是量化交易系统的功能模块图,主要分为3块,第 ...

  6. 获取股票数据【使用JQData查询行情数据、财务指标、估值指标】

    了解股票: 在上一次量化小科普[什么是量化?常用的股票量化指标.如何搭建量化交易系统]对于量化的概念有了一个基本认识,其中量化的主体在这门课程的学习中是"股票",而当别人问你:&q ...

  7. matlab python 股票,股票行情数据获取-Python获取股票数据?

    Python获取股票数据? 这里推荐一个包―tushare,tushare是一个免费.开源的python财经数据接口包.主要实现了从数据采集.清洗加工到数据存储过程,能够为金融分析人员提供快速.整洁的 ...

  8. R获取股票数据并进行进行可视化分析

    R获取股票数据并进行进行可视化分析 # 加载依赖的包 library(quantmod) library(ggplot2) library(magrittr) library(broom) # 设置计 ...

  9. python tushare获取股票数据_Python 金融: TuShare API 获取股票数据 (1)

    多多教Python 金融 是我为金融同行,自由职业投资人 做的一个专栏.这里包含了我自己作为量化交易员,在做研究时所用到的Python技巧和实用案例.这个栏目专业性会比较强:本人29岁,量化工作5年的 ...

最新文章

  1. 安装Ruby和Rails运行环境
  2. 2016windows(10) wamp 最简单30分钟thrift入门使用讲解,实现php作为服务器和客户端的hello world...
  3. ZooKeeper管理员指南——部署与管理ZooKeeper
  4. php课程实验总结报告_PHP课程总结20161125
  5. MySQL中一个双引号错位引发的血案
  6. Python 头像动漫化,快来生成女朋友的动漫头像
  7. Excel文件读取的两种方式
  8. 以下哪些可以成为html文件的扩展名_今天在我的visual studio code里装了以下插件,现在用着很爽...
  9. 海康摄像机取流RTSP地址规则说明
  10. 人大经济论坛SAS入门到高级教程
  11. 电脑硬盘怎么分区?C盘/D盘/E盘......快来创建自己的DIY磁盘吧!
  12. 1、结构化、面向对象程序设计差别、类基本概念
  13. python 比较数字大小_Python:整数比较大小和输出 | 学步园
  14. Python笔记:数据分列
  15. 自媒体视频去水印工具哪个好
  16. 高等数学:第五章 定积分(4) 定积分的换元法
  17. angularJS学习小结——filter
  18. Web前端——HTML中的列表、表格、表单
  19. SEO像艺术,为自己做站最划算
  20. window xampp php,[XAMPP下载]PHP进阶Window本地安装XAMPP

热门文章

  1. SQL中的escape的用法
  2. python 论坛爬虫代码_python博客文章爬虫实现代码
  3. 瑞萨电子基于RISC-V的电机控制ASSP解决方案
  4. Cloneable接口的作用
  5. 一次疲惫的调试--累了及时透气
  6. Nullable Value Type
  7. 《淘宝网开店 进货 运营 管理 客服 实战200招》——2.9 网上商品的定价策略...
  8. SSH—网上商城之商品图片文件上传
  9. 《jQuery 自定义插件》
  10. mpp新增一个字段_mybatisplus使用@InsertFill和@UpdateFill注解设置自定义sql对字段自动填充...