實(shí)時(shí)獲取股票數(shù)據(jù)的android app應(yīng)用程序源碼分享
最近學(xué)習(xí)Android應(yīng)用開(kāi)發(fā),不知道寫(xiě)一個(gè)什么樣的程序來(lái)練練手,正好最近股票很火,就一個(gè)App來(lái)實(shí)時(shí)獲取股票數(shù)據(jù),取名為Mystock。使用開(kāi)發(fā)工具Android Studio,需要從Android官網(wǎng)下載,下載地址:http://developer.android.com/sdk/index.html。不幸的是Android是Google公司的,任何和Google公司相關(guān)的在國(guó)內(nèi)都無(wú)法直接訪問(wèn),只能通過(guò)VPN訪問(wèn)。
下圖為Android Studio打開(kāi)一個(gè)工程的截圖:
下面按步介紹Mystock的實(shí)現(xiàn)步驟。
1.以下是activa_main.xml的內(nèi)容。上面一排是三個(gè)TextView,分別用來(lái)顯示上證指數(shù),深圳成指,創(chuàng)業(yè)板指。中間一排是一個(gè)EditText和一個(gè)Button,用來(lái)添加股票。下面是一個(gè)Table,用來(lái)顯示添加的股票列表。
<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"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <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="wrap_content" android:layout_height="wrap_content" android:text="@string/stock_sh_name"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/stock_sh_index"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12sp" android:id="@+id/stock_sh_change"/> </LinearLayout> <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="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"/> </LinearLayout> <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="wrap_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="wrap_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" /--> <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>
應(yīng)用截圖如下:
2.數(shù)據(jù)獲取,這里使用sina提供的接口來(lái)實(shí)時(shí)獲取股票數(shù)據(jù),代碼如下:
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); }
這里發(fā)送Http請(qǐng)求用到了Volley,需要在build.gradle里面添加dependencies:compile 'com.mcxiaoke.volley:library:1.0.19'。
3.定時(shí)刷新股票數(shù)據(jù),使用了Timer,每隔兩秒發(fā)送請(qǐng)求獲取數(shù)據(jù),代碼如下:
Timer timer = new Timer("RefreshStocks"); timer.schedule(new TimerTask() { @Override public void run() { refreshStocks(); } }, 0, 2000); private void refreshStocks(){ String ids = ""; for (String id : StockIds_){ ids += id; ids += ","; } querySinaStocks(ids); }
4.在程序退出時(shí)存儲(chǔ)股票代碼,下次打開(kāi)App時(shí),可以顯示上次的股票列表。代碼如下。
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.刪除選中的股票,在menu_main.xml里面添加一個(gè)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>
代碼響應(yīng)事件并刪除:
@Override public 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 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); }
屏幕截圖:
6.當(dāng)有大額委托掛單時(shí),發(fā)送消息提醒,代碼如下:
{ ... 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()); }
屏幕截圖:
以上通過(guò)圖文并茂的方式給大家分享了一個(gè)實(shí)時(shí)獲取股票數(shù)據(jù)的android app應(yīng)用程序源碼,希望大家喜歡。
- Android獲取app應(yīng)用程序大小的方法
- Android編程之高效開(kāi)發(fā)App的10個(gè)建議
- 使用Chrome瀏覽器調(diào)試Android App詳解
- Android中通過(guò)外部程序啟動(dòng)App的三種方法
- Android 應(yīng)用APP加入聊天功能
- Android App后臺(tái)服務(wù)報(bào)告工作狀態(tài)實(shí)例
- 一看就懂的Android APP開(kāi)發(fā)入門(mén)教程
- Android App數(shù)據(jù)格式Json解析方法和常見(jiàn)問(wèn)題
- android使用webwiew載入頁(yè)面使用示例(Hybrid App開(kāi)發(fā))
- Android編程判斷當(dāng)前指定App是否在前臺(tái)的方法
相關(guān)文章
Android藍(lán)牙聊天開(kāi)源項(xiàng)目
這篇文章主要為大家詳細(xì)介紹了Android藍(lán)牙聊天開(kāi)源項(xiàng)目的開(kāi)發(fā),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Kotlin?Dispatchers協(xié)程調(diào)度器源碼深入分析
Kotlin協(xié)程不是什么空中閣樓,Kotlin源代碼會(huì)被編譯成class字節(jié)碼文件,最終會(huì)運(yùn)行到虛擬機(jī)中。所以從本質(zhì)上講,Kotlin和Java是類似的,都是可以編譯產(chǎn)生class的語(yǔ)言,但最終還是會(huì)受到虛擬機(jī)的限制,它們的代碼最終會(huì)在虛擬機(jī)上的某個(gè)線程上被執(zhí)行2022-11-11TabLayout關(guān)聯(lián)ViewPager后不顯示文字的解決方法
這篇文章主要為大家詳細(xì)介紹了TabLayout關(guān)聯(lián)ViewPager后不顯示文字的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Android Studio 運(yùn)行按鈕灰色的完美解決方法
今天新建項(xiàng)目的時(shí)候突然發(fā)現(xiàn)編譯后運(yùn)行按鈕為灰色,今天小編給大家?guī)?lái)了Android Studio 運(yùn)行按鈕灰色的完美解決方法,非常不錯(cuò),對(duì)大家的需要或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-10-10Android使用觀察者模式Observer實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)監(jiān)聽(tīng)
這篇文章主要為大家詳細(xì)介紹了Android使用觀察者模式Observer實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)監(jiān)聽(tīng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android PreferenceActivity與PreferenceFragment詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android PreferenceActivity與PreferenceFragment詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-12-12android如何取得本地通訊錄的頭像的原圖的實(shí)現(xiàn)代碼
這篇文章主要介紹了android如何取得本地通訊錄的頭像的原圖的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12