摘要:在移動(dòng)互聯(lián)網(wǎng)上Android手機(jī)是非常出色的。在Android程序設(shè)計(jì)中,定位服務(wù)很酷,在使用GPS中,可以嵌入你自己的應(yīng)用程序在其中,做一些很有意義的事情。在這篇文章里,我們將快餐服務(wù)放進(jìn)來,從而實(shí)現(xiàn)在Android應(yīng)用程序里使用GPS、Google地圖、xml文件獲取和解析等應(yīng)用,通過編程來達(dá)到我們的目的。
關(guān)鍵詞: GPSAPIKeyXML
中圖分類號(hào):TP393文獻(xiàn)標(biāo)識(shí)碼:A文章編號(hào):1674-098X(2011)07(b)-0000-00
Design Fast food service on Android devices
QI Jing
(Beijing Information Technology College,Beijing,100087,China)
Abstract:In the mobile Internet Android phones are very good. In Android programming, positioning services to cool, in use GPS, can embed your own application in it, do some very meaningful. In this article, we will put in fast food services, thus realize in Android applications use GPS, Google maps, XML file access and analytical applications, programmed to achieve our goal.
Key words:GPS;APIKey;XML
1 引 言
在這篇文章里,講述了快餐服務(wù)在Android程序設(shè)計(jì)中是如何實(shí)現(xiàn)的。文章首先展示了快餐服務(wù)的程序運(yùn)行界面,然后講述了它的設(shè)計(jì)過程,列舉了部分功能的實(shí)現(xiàn)方法。在相關(guān)技術(shù)部分,文章講了如何使用Google地圖,如何使用GPS提供的經(jīng)緯度坐標(biāo)。在應(yīng)用效果部分,我們把程序交給一些人使用,歸納他們的需求,加以實(shí)現(xiàn)。本文的最后對(duì)所講述的內(nèi)容進(jìn)行了小結(jié)。
2 快餐服務(wù)的程序功能
我們?cè)O(shè)計(jì)的快餐服務(wù)程序功能如下:
圖1 程序功能圖2 GPS定位
也可以輸入某個(gè)位置的經(jīng)緯度,會(huì)顯示該地理位置。例如:經(jīng)度 103.85緯度 1.35
會(huì)定位于新加坡宏茂橋附近。點(diǎn)擊“查詢周邊指定快餐”,會(huì)顯示能夠查詢的快餐列表。
圖3 快餐列表和顯示位置
點(diǎn)擊麥當(dāng)勞圖標(biāo)后,選擇Maps,出現(xiàn)標(biāo)示麥當(dāng)勞的地圖,然后點(diǎn)擊McDonald’s后出現(xiàn)圖4。
圖4 三維街道
點(diǎn)擊“指南針”后出現(xiàn)圖5。
圖5 指南針圖6 人氣產(chǎn)品
圖6是人氣產(chǎn)品點(diǎn)擊。點(diǎn)擊“休閑”出現(xiàn)“新聞閱讀”及“天氣預(yù)報(bào)”。
圖7 新聞和天氣
3 設(shè)計(jì)過程
快餐服務(wù)程序由八個(gè)部分組成,它們都是圍繞著快餐服務(wù)展開的。
⑴首先在GpsView程序中列出了對(duì)其他八個(gè)功能程序的調(diào)用:
case 0:intent = new Intent(GpsView.this,MyPositionActivity.class);startActivity(intent);break;
case 1:intent = new Intent(GpsView.this,QueryPositionByLLActivity.class);startActivity(intent);break;
case 2:intent = new Intent(GpsView.this,MyView1.class);startActivity(intent);break;
case 3:intent = new Intent(GpsView.this,MainActivity.class);startActivity(intent);break;
case 4:intent = new Intent(GpsView.this,MainActivity1.class);startActivity(intent);break;
case 5:intent = new Intent(GpsView.this,MyView.class);startActivity(intent);break;
case 6:intent = new Intent(GpsView.this,cameraView.class);startActivity(intent);break;
case 7:Intent mIntent = new Intent();ComponentName ccomp = new ComponentName(\"com.rss_reader\", \"com.logo.LogoActivity\");mIntent.setComponent(ccomp);mIntent.setAction(\"android.intent.action.MAIN\");mIntent.addCategory(\"android.intent.category.LAUNCHER\");startActivity(mIntent);break;
⑵下面這段是地圖放大的Button和縮小的Button的部分代碼:
b1 = (Button)findViewById(R.id.myButton2);
b1.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) {intZoomLevel++; if(intZoomLevel>mMapView01.getMaxZoomLevel()){ intZoomLevel =mMapView01.getMaxZoomLevel(); }mMapController01.setZoom(intZoomLevel);} });
b2 = (Button)findViewById(R.id.myButton3);
b2.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) {intZoomLevel--;
if(intZoomLevel<1) { intZoomLevel = 1; } mMapController01.setZoom(intZoomLevel); } });
⑶下面這段是測(cè)量方向的部分代碼:
final int count = getChildCount();int maxHeight = 0; int maxWidth = 0;
for (int i = 0; i < count; i++) {final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
measureChild(child, widthMeasureSpec, heightMeasureSpec); } }
maxWidth += getPaddingLeft() + getPaddingRight();
maxHeight += getPaddingTop() + getPaddingBottom();
Drawable drawable = getBackground();
if (drawable != 1) { maxHeight = Math.max(maxHeight, drawable.getMinimumHeight());
maxWidth = Math.max(maxWidth, drawable.getMinimumWidth());}
setMeasuredDimension(resolveSize(maxWidth, widthMeasureSpec),
resolveSize(maxHeight, heightMeasureSpec));
⑷下面這段是下載新聞數(shù)據(jù)的部分代碼:
ListView itemlist = (ListView) findViewById(R.id.itemlist);
itemlist.setCacheColorHint(0);
if (queryItem().getCount() == 0) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(\"第一次使用此功能,請(qǐng)下載! \").setCancelable(1).setPositiveButton(\"確定\",new DialogInterface .OnClickListener() {public voidonClick(DialogInterface dialog,int which) {if (!rssthread.isAlive()) {rssthread.start();}
if (!progressthread.isAlive()) {showDialog(i++);progressthread.start();}}}).create().show();
} else {Cursor cursor = queryItem();
SimpleCursorAdapter adapter1 = new SimpleCursorAdapter(this,R.layout.list_view, cursor, new String[] { \"title\",\"pubDate\" }, new int[] { R.id.title, R.id.date });
itemlist.setAdapter(adapter1);itemlist.setOnItemClickListener(this);itemlist.setSelection(0);}}
⑸下面這段是得到和顯示天氣的部分代碼:
SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
rpc.addProperty(\"theCityName\", cityName);
AndroidHttpTransport ht = new AndroidHttpTransport(URL);
ht.debug = true;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11); envelope.bodyOut = rpc;
envelope.dotNet = true;envelope.setOutputSoapObject(rpc);
ht.call(SOAP_ACTION, envelope);
debug(LOG_TAG, \"DUMP>> \" + ht.requestDump);
debug(LOG_TAG, \"DUMP<< \" + ht.responseDump);
SoapObject result = (SoapObject) envelope.bodyIn;
SoapObject detail = (SoapObject) result.getProperty(\"getWeatherbyCityNameResult\");
parseWeather(detail);
String date = detail.getProperty(6).toString();
weatherToday = \"今天:\" + date.split(\" \")[0];
weatherToday = weatherToday + \"\天氣:\" + date.split(\" \")[1];
weatherToday = weatherToday + \"\氣溫:\"+ detail.getProperty(5).toString();
weatherToday = weatherToday + \"\風(fēng)力:\"+ detail.getProperty(7).toString() + \"\\";
iconToday[0] = parseIcon(detail.getProperty(8).toString());
iconToday[1] = parseIcon(detail.getProperty(9).toString());
weatherCurrent = detail.getProperty(10).toString();
date = detail.getProperty(13).toString();
weatherTomorrow = \"明天:\" + date.split(\" \")[0];
weatherTomorrow = weatherTomorrow + \"\天氣:\" + date.split(\" \")[1];
weatherTomorrow = weatherTomorrow + \"\氣溫:\"+ detail.getProperty(12).toString();
weatherTomorrow = weatherTomorrow + \"\風(fēng)力:\"+ detail.getProperty(14).toString();
iconTomorrow[0] = parseIcon(detail.getProperty(15).toString());
iconTomorrow[1] = parseIcon(detail.getProperty(16).toString());
date = detail.getProperty(18).toString();
weatherAfterday = \"后天:\" + date.split(\" \")[0];
weatherAfterday = weatherAfterday + \"\天氣:\" + date.split(\" \")[1];
weatherAfterday = weatherAfterday + \"\氣溫:\"+ detail.getProperty(17).toString();
weatherAfterday = weatherAfterday + \"\風(fēng)力:\"+ detail.getProperty(19).toString() + \"\\";
iconAfterday[0] = parseIcon(detail.getProperty(20).toString());
iconAfterday[1] = parseIcon(detail.getProperty(21).toString());
4 相關(guān)技術(shù)
⑴ 首先從Android SDK v1.0開始,在Android應(yīng)用使用Google地圖之前,需要申請(qǐng)一個(gè)免費(fèi)的Google地圖的API key。可以按照下面的步驟:
如果在Android仿真器上測(cè)試應(yīng)用程序,SDK調(diào)試證書在默認(rèn)文件夾“ C:\\Documents and Settings\\
keytool -list -alias androiddebugkey -keystore E:\\android-sdk-windows \\.android\\ debug.keystore -storepass android -keypass android,然后復(fù)制這個(gè)MD5證書指紋到 http://code.google.com/android/maps-api-signup.html就可以得到Google API key:
android:apiKey=\"0l4sCTTyRmXTNo7k8DREHvEaLar2UmHGwnhZVHQ\"。還需要修改AndroidManifest.xml文件,添加
⑵ 這一部分是根據(jù)手機(jī)GPS提供的經(jīng)緯度坐標(biāo),在地圖中顯示用戶當(dāng)前的位置,并在此基礎(chǔ)上添加Overlay標(biāo)記。通過getSystemService()方法獲得LocationManager對(duì)象,該對(duì)象通過getLastKnownLocation()方法獲得當(dāng)前位置對(duì)象Location,最后animateTo()方法具體完成定位。
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = LocationManager.GPS_PROVIDER;
LocationProvider provider1=locationManager.getProvider(provider);
provider1.requiresSatellite();
Location location = locationManager.getLastKnownLocation(provider);
Lat=location.getLatitude();Lng=location.getLongitude();
GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
controller.animateTo(point);
⑶ 新聞的XML文件是從\"http://news.qq.com/newsgn/rss_newsgn.xml\"下載的,然后進(jìn)行編碼轉(zhuǎn)換。過程是首先建立要返回的xml單一輸入源,這是通過數(shù)組存儲(chǔ)輸入流信息,用字符串存儲(chǔ)xml內(nèi)容,然后對(duì)xml文件中的編碼進(jìn)行轉(zhuǎn)換,再將xml內(nèi)容轉(zhuǎn)換成字節(jié)數(shù)組,寫進(jìn)輸出緩存,將輸出緩存轉(zhuǎn)換成輸入緩存,最后將輸入緩存轉(zhuǎn)換成輸入流。
InputSource newis = new InputSource();
InputStream in = is.getByteStream();
byte[] by = new byte[5096];String theString = \"\";
try {int length = in.read(by, 0, by.length);
while (length > 0) {
String temp = EncodingUtils.getString(by, 0, length, \"gb2312\");
theString = theString + temp;
length = in.read(by, 0, by.length);}
theString = theString.replaceFirst(oldCharet, newCharet);
theString = theString.substring(0, theString.lastIndexOf(\">\") + 1);
ByteArrayOutputStream out = new ByteArrayOutputStream();
by = new byte[5096];by = theString.getBytes(\"UTF-8\");
out.write(by);
ByteArrayInputStream bin = new ByteArrayInputStream(out.toByteArray());
in = (InputStream) bin;newis = new InputSource(in);return newis;
5 應(yīng)用效果
在開發(fā)手機(jī)快餐服務(wù)程序的過程中,我們多次將程序安裝在設(shè)備中交給不同的人使用,然后讓他們提出建議,我們加以改進(jìn),現(xiàn)在使用人員反映良好。在程序中我們預(yù)留了一些接口,可以將新功能隨時(shí)添加進(jìn)去。在程序中我們還設(shè)計(jì)了聯(lián)系方式,可以將建議隨時(shí)發(fā)給我們。今后還會(huì)有更多基于Google地圖和基于位置服務(wù)的需求,有更多有趣的項(xiàng)目可以開發(fā),那就讓我們學(xué)習(xí)更多這方面的知識(shí)吧。
6 結(jié)語
這篇文章講述了開發(fā)手機(jī)快餐服務(wù)程序的過程,在這里我列舉了一些實(shí)現(xiàn)方法,從而更好的讓大家了解整個(gè)實(shí)現(xiàn)過程。快餐服務(wù)的內(nèi)容可以包括很多,也很容易擴(kuò)展到其他各方面,關(guān)于Google地圖和基于位置的服務(wù)也有很多,這里只是拋磚引玉。