Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android系統教程 >> Android開發教程 >> Android中的Earthquake(地震顯示器) 項目 詳解

Android中的Earthquake(地震顯示器) 項目 詳解

編輯:Android開發教程

環境: Android Studio 0.5.2, Gradle 1.11, kindle fire

時間: 2014-3-24

修正位置信息異常: http://blog.csdn.net/caroline_wendy/article/details/24465487

Earthquake項目, 主要是讀取USGS(United States Geological Survey, 美國地址勘探局)提供的feeds(訂閱源), 進行顯示數據;

需要讀取互聯網的數據, 進行格式解析(parse), 數據類型是atom類型, 類似XML.

訂閱源地址: http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.atom

格式:

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss">  
<title>USGS Magnitude 2.5+ Earthquakes, Past Day</title>  
<updated>2014-03-24T07:56:39Z</updated>  
<author>  
<name>U.S. Geological Survey</name>  
<uri>http://earthquake.usgs.gov/</uri>  
</author>  
<id>  
http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.atom  
</id>  
<link rel="self" href="http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.atom" />  
<icon>http://earthquake.usgs.gov/favicon.ico</icon>  
<entry>  
<id>urn:earthquake-usgs-gov:ci:15479569</id>  
<title>M 2.9 - 9km W of Alberto Oviedo Mota, Mexico</title>  
<updated>2014-03-24T07:48:34.609Z</updated>  
<link rel="alternate" type="text/html" href="http://earthquake.usgs.gov/earthquakes/eventpage/ci15479569" />  
<summary type="html">  
<![CDATA[ 
<p class="quicksummary"><a href="http://earthquake.usgs.gov/earthquakes/eventpage/ci15479569#dyfi" class="mmi-I" title="Did You Feel It? maximum reported intensity (0 reports)">DYFI? - <strong class="roman">I</strong></a></p><dl><dt>Time</dt><dd>2014-03-24 07:38:10 UTC</dd><dd>2014-03-23 23:38:10 -08:00 at epicenter</dd><dt>Location</dt><dd>32.222°N 115.274°W</dd><dt>Depth</dt><dd>14.10 km (8.76 mi)</dd></dl> 
]]>
</summary>  
<georss:point>32.2215 -115.274</georss:point>  
<georss:elev>-14100</georss:elev>  
<category label="Age" term="Past Hour" />  
<category label="Magnitude" term="Magnitude 2" />  
</entry>  
......  
......

Earthquake的具體設計:

新建項目: Earthquake

1. 新建Quake(Quake.java)類, 顯示地震數據.

位置: java->package->Quake

package mzx.spike.earthquake.app;  
      
import android.location.Location;  
      
import java.text.SimpleDateFormat;  
import java.util.Date;  
      
public class Quake {  
    private Date date;  
    private String details;  
    private Location location;  
    private double magnitude;  
    private String link;  
      
    public Date getDate() { return date; }  
    public String getDetails() { return details; }  
    public Location getLocation() { return location; }  
    public double getMagnitude() { return magnitude; }  
    public String getLink() { return link; }  
      
    public Quake(Date _d, String _det, Location _loc, double _mag, String _link) {  
        date = _d;  
        details = _det;  
        location = _loc;  
        magnitude = _mag;  
        link = _link;  
    }  
      
    @Override
    public String toString() {  
        SimpleDateFormat sdf = new SimpleDateFormat("HH.mm");  
        String dateString = sdf.format(date);  
        return dateString + ": " + magnitude + " " + details;  
    }  
      
}

詳解:

1. 顯示的類型: date, 日期; details, 詳細信息, 地點; location, 位置; magnitude, 震級; link, 鏈接;

2. get()方法, 返回信息; 構造函數, 賦初值; toString(), 默認輸出信息;

2. 修改activity_main.xml, 添加fragment.

位置: res->layout->activity_main.xml

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="mzx.spike.earthquake.app.MainActivity">  
      
    <fragment android:name="mzx.spike.earthquake.app.EarthquakeListFragment"
        android:id="@+id/EarthquakeListFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
     />  
</RelativeLayout>

添加Fragment, 指定實現(.java)文件位置.

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved