Android 《找朋友》系统详解

第一次做翻译工作,希望能给国内的Android爱好者带来帮助,下面直接进入本次翻译的主题!

 

原文链接:http://www.anddev.org/the_friend_finder_-_mapactivity_using_gps_-_part_i_-_ii-t93.html

What is this: This tutorial shows how to create a relatively complex application, that takes advantage of LocationManager (GPS-Access), to show your contacts on a Map.

 

本指南告诉我们如何创建一个复杂的使用 LocationManager (GPS-Access) 应用程序将你的联系方式显式在地图

 

What you will learn:

  • You will learn how to extend the MapActivity to create a Map-Application.
  • Use the LocationManager to track your GPS-Position.
  • Work with Cursors to grab data from the contents.
  • Override the onFreeze(), onResume(), onXYZ()-methods which are i.e. called, when another activity gets on top of ours.
  • Draw things on the MapView using an OverlayController.
  • How to create menus.
  • Many more.....

我们将学到什么

1.       学习如何扩展 MapActivity 去创建一个地图应用程序

2.       LocationManager 跟踪你的GPS 位置

3.       使用Cursors 去获取数据

4.       Override onFreeze(), onResume(), onXYZ() 方法,当另一个activity 显示在我们所创建的activity 的上层时这些方法将被调用

5.       OverlayController MapView 添加一些新的内容

6.       如何创建menus

7.       其他

 

What it will look like:
1. Always updating Screen
2. FriendFinder-Map toggled to SatelliteView
3. FriendFinder-Map toggled to Streets-View (also updating ^^ own position got outside of the map)

 

 

我们将看到的系统的样子

1.       一直更新的屏幕

2.  FriendFinder-Map 切换到 SatelliteView

3.  FriendFinder-Map 切换到 Streets-View

notelist

 

 

1 2

 


Description:
So the first thing as always is, to think about what we want to do and what we need for that...

  • We want a ListView of our Contacts --> We need a ListView (awesome conclusion... Rolling Eyes )
  • We want to do sth. with geo-Location(GPS) --> We need a LocationManager and his friends Wink
  • We want to get the location of our friends --> We need a place to define them!
  • We want to display a Map --> We need a second (Map)Activity
    a. We need to register that second Activity
  • We want to draw sth over the Map --> We need an OverlayController

描述:

通常我们要做的第一件事情是:考虑什么是我们想做的和什么是我们所需要的

 

我们需要一个 ListView 显示我们的联系方式 -> 所以我们将要要创建一个ListView

我们需要 geo-Location(GPS) 做一些事情-> 我们要用到LocationManager 以及与它相关联的类

我们需要获得我们朋友的地址  > 所以我们需要有一个地方去定义他们!

我们要显示一张地图 -》所以我们要一个实现了MapActivityActivity

我们还要在地图上画一些其他东西-》 我们需要一个OverlayController

 

 

As we need to store the GPS-Location of our contacts somewhere, I chose the Notes-Section in each Contacts-Profile. Saving a GPS-location is pretty simple, as we choose a well-structured style of data: a URI-String, which can easily found and extracted from the Notes-String using Regular Expressions. (all that is explained in details below)

 

当我们需要存储带有我们的联系方式的 GPS-Location 在某个地方的时候,我选择 Notes-Section 在每一个联系方式简介,保存一个 GPS-location 非常简单,当我们选择一个结构良好的数据

一个我们能很容易找到的 URI-String

 

 

As a human you can see the format of the URI: "geo:[-]XXX.XXXXX,[-]XXX.XXXXXX#".
Simply setup two or three sample-contacts (with different GPS-Locations) just like in the image below.
Or take these:
geo:37.402346,-122.075014# (Google-Headquarters, SF, California)
geo:37.444608,-122.216034# (McDonalds,
Rolling Eyes near MountainView, SF, California)
geo:37.41622,-122.089919# (Starbucks, somewhere in SF, California)
or get your own on GoogleMaps (Hover the "Link to this Page"-Link)

 

作为一个人你能明白这样的 URI: "geo:[-]XXX.XXXXX,[-]XXX.XXXXXX#"

简单的设置两三个例子联系方式

geo:37.402346,-122.075014# ( 位于 SF, California Google 总部 )

geo:37.444608,-122.216034# ( Rolling Eyes MountainView, SF, California 附近的一家麦当劳 )

geo:37.41622,-122.089919# ( SF, California 的某个地方的星巴克 )
或者在 GoogleMaps 上获得你自己的位置

3

 

0.                 Create an new Android-Project (HowTo ).
Idea I called it "FriendFinder" as that is an extremely perfect name Rolling Eyes

 

 

创建一个新的 Android 工程

 

我为它命名为 "FriendFinder" 这是一个相等完美的名字

 

 

As we are planning to access the Contacts of our Android-Phone, we make that public in the AndroidManifest.xml . If we wouldn't do that, our Application would 'crash'(Exception followed by App-End) right when we would try to access the Contacts for the first time.

 

当我们打算去访问我们的手机中联系方式,我们使它发布在 AndroidManifest.xml 文件中,如果我们不那样做,当我们第一次去方位联系方式的时候 Application 将立即垮掉,

 

XML:

  ...
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.anddev.android.friendfinder">
    <uses-permission id="android.permission.READ_CONTACTS"/>
    <application android:icon="@drawable/icon">
        <activity class=".FriendFinder" android:label="@string/main_title">
   ...

<?xml version="1.0" encoding="utf-8"?>

 

 

We need to define all the Strings we are going to use in our application in the 'res/values/strings.xml'.
(We could hardcode them too, but that would be very bad Crying or Very sad )

 

我们需要定义所有的我们将用到字符串在我们的应用程序中 位置 'res/values/strings.xml'. (当然我们也可以采用硬编码,但是那样可能非常糟糕)

 

XML:

<resources>
    <string name="main_title">FriendFinder - anddev.org</string>
    <string name="main_menu_open_map">Open visual FriendTracker</string>
    <string name="main_list_format">$name ($distance km)</string>
    <string name="main_list_geo_not_set">not set</string>
    <string name="map_title">FriendFinder - anddev.org</string>
    <string name="map_menu_zoom_in">Zoom in (Key: I)</string>
    <string name="map_menu_zoom_out">Zoom out (Key: O)</string>
    <string name="map_menu_back_to_list">Back to list</string>
    <string name="map_menu_toggle_street_satellite">Toggle View: Street / Satellite (Key: T)</string>
    <string name="map_overlay_own_name">Me</string>
</resources>

We will also have a class: 'Friend ' (in Friend.java) which combines a Name with a Location:

我们还有一个类 Friend Friend.java 文件中,这个类有一个名字和地址

 

1.                 So lets start where all Activities start:

让我们从所有 Activities 开始的地方开始

 

Java:

     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle icicle) {
          super.onCreate(icicle);

          /* The first thing we need to do is to setup our own
           * locationManager, that will support us with our own gps data */
          this.myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

          /* Update the list of our friends once on the start,
           * as they are not(yet) moving, no updates to them are necessary */
          this.refreshFriendsList();
          
          /* Initiate the update of the contactList
           * manually for the first time */
          this.updateList();

          /* Prepare the things, that will give
           * us the ability, to receive Information
           * about our GPS-Position. */
          this.setupForGPSAutoRefreshing();
     }

 

 

 

 

2.                 Lets dig deeper into the refreshFriendsList()-Method, which will grab our Contacts/Friends out of Androids Database using a query/Cursor.
As it is a pretty big function doing some advanced logic, we will split into pieces here.

The first thing we will do is to grab a so called Cursor. I'll try to explain that referring to relational databases and their SQL (Structured Query Language) .
A Cursor is kinda like the result of a SQL-Query. In this case we make a query on 'People.CONTENT_URI ' what would referr to sth. like 'SELECT * FROM 'Contacts' .
The next 3 arguments remain 'null' , we could have filled them stuff like the equivalent to 'WHERE Name='Peter'' ...
The last field contains the Sort-Order we want to receive. Here the similarity to SQL is pretty obvious. We chose sorting the results by People.NAME in an ASC ending order.

 

让我们更深入研究的 refreshFriendsList() 方法 , 这个方法将从 Android 数据库获得我们的朋友的联系方式使用一次查询 .

因为它功能很大逻辑很复杂 , 所以我们将把这个 function 分成小块来讲解 .

我们要做的第一件事是去获取一个被称做 Cursor, 我将试着去解释提到的关系数据库和他们的 SQL, Cursor 有点像 SQL 查询结果 .

 

 

We are now going to loop through every contact that the Cursor found/'is providing' and check whether the Notes-String of that Contact somwhere contains a 'geo:XX...XX# ' within.

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值