版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
【移動應用開發(fā)技術】如何在Android中通過網(wǎng)絡獲取定位
如何在Android中通過網(wǎng)絡獲取定位?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。1.activity_main.xml頁面定義TextView顯示城市名。activity_main.xml頁面:<RelativeLayout
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正在定位。。。"
/>
</RelativeLayout>2.新建Common.java頁面,設置公共常量。Common.java頁面:package
mon;
/**
*
公共常量
*
@author
wxy
*
*/
public
class
Common
{
public
static
final
String
LOCATION
=
"location";
public
static
final
String
LOCATION_ACTION
=
"locationAction";
}3.新建LocationSvc.java頁面作為服務進行定位。LocationSvc.java頁面:package
com.sc.demo.locate;
import
mon.Common;
import
android.app.Service;
import
android.content.Intent;
import
android.location.Location;
import
android.location.LocationListener;
import
android.location.LocationManager;
import
android.os.Bundle;
import
android.os.IBinder;
import
android.util.Log;
import
android.widget.Toast;
/**
*
定位服務
*
@author
wxy
*
*/
public
class
LocationSvc
extends
Service
implements
LocationListener
{
private
LocationManager
locationManager;
@Override
public
IBinder
onBind(Intent
intent)
{
return
null;
}
@Override
public
void
onCreate()
{
locationManager
=
(LocationManager)
getSystemService(LOCATION_SERVICE);
}
@Override
public
void
onStart(Intent
intent,
int
startId)
{
if
(locationManager.getProvider(LocationManager.NETWORK_PROVIDER)
!=
null)
locationManager
.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
0,
0,
this);
else
if
(locationManager.getProvider(LocationManager.GPS_PROVIDER)
!=
null)
locationManager
.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0,
0,
this);
else
Toast.makeText(this,
"無法定位",
Toast.LENGTH_SHORT).show();
}
@Override
public
boolean
stopService(Intent
name)
{
return
super.stopService(name);
}
@Override
public
void
onLocationChanged(Location
location)
{
//通知Activity
Intent
intent
=
new
Intent();
intent.setAction(Common.LOCATION_ACTION);
intent.putExtra(Common.LOCATION,
location.toString());
sendBroadcast(intent);
//
如果只是需要定位一次,這里就移除監(jiān)聽,停掉服務。如果要進行實時定位,可以在退出應用或者其他時刻停掉定位服務。
locationManager.removeUpdates(this);
stopSelf();
}
@Override
public
void
onProviderDisabled(String
provider)
{
}
@Override
public
void
onProviderEnabled(String
provider)
{
}
@Override
public
void
onStatusChanged(String
provider,
int
status,
Bundle
extras)
{
}
}4.MainActivity.java頁面獲取經(jīng)緯度,然后根據(jù)經(jīng)緯度獲取城市名。MainActivity.java頁面:package
com.sc.demo;
import
java.io.IOException;
import
java.util.List;
import
mon.Common;
import
com.sc.demo.locate.LocationSvc;
import
android.location.Address;
import
android.location.Geocoder;
import
android.os.Bundle;
import
android.util.Log;
import
android.widget.TextView;
import
android.app.Activity;
import
android.app.ProgressDialog;
import
android.content.BroadcastReceiver;
import
android.content.Context;
import
android.content.Intent;
import
android.content.IntentFilter;
public
class
MainActivity
extends
Activity
{
private
TextView
text;
private
ProgressDialog
dialog;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text
=
(TextView)
findViewById(R.id.text);
//
注冊廣播
IntentFilter
filter
=
new
IntentFilter();
filter.addAction(Common.LOCATION_ACTION);
this.registerReceiver(new
LocationBroadcastReceiver(),
filter);
//
啟動服務
Intent
intent
=
new
Intent();
intent.setClass(this,
LocationSvc.class);
startService(intent);
//
等待提示
dialog
=
new
ProgressDialog(this);
dialog.setMessage("正在定位...");
dialog.setCancelable(true);
dialog.show();
}
private
class
LocationBroadcastReceiver
extends
BroadcastReceiver
{
@Override
public
void
onReceive(Context
context,
Intent
intent)
{
if
(!intent.getAction().equals(Common.LOCATION_ACTION))
return;
String
locationInfo
=
intent.getStringExtra(Common.LOCATION);
double
latitude
=
Double
//截取經(jīng)緯度轉換為double型
.parseDouble(locationInfo.substring(17,
26));
double
longitude
=
Double.parseDouble(locationInfo
.substring(27,
37));
text.setText(getaddress(latitude,
longitude));
dialog.dismiss();
MainActivity.this.unregisterReceiver(this);//
不需要時注銷
}
public
String
getaddress(double
latitude,
double
longitude)
{
String
cityName
=
"";
List<Address>
addList
=
null;
Geocoder
ge
=
new
Geocoder(MainActivity.this);
try
{
addList
=
ge.getFromLocation(latitude,
longitude,
1);
}
catch
(IOException
e)
{
e.printStackTrace();
}
if
(addList
!=
null
&&
addList.size()
>
0)
{
for
(int
i
=
0;
i
<
addList.size();
i++)
{
Address
ad
=
addList.get(i);
cityName
+=
ad.getCountryName()
+
";"
+
ad.getLocality();
}
}
Log.i("wxy",
"city:"
+
cityName);
return
cityName;
}
}
}5.AndroidManifest.xml頁面添加權限并聲明服務。AndroidManifest.xml頁面:<?xml
version="1.0"
encoding="utf-8"?>
<manifest
xmlns:android="/apk/res/android"
package="com.sc.demo"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18"
/>
<uses-permission
android:name="android.permission.ACCESS
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 臨床中期引產(chǎn)護理查房
- 病毒防疫防控服務合同
- 第一醫(yī)院醫(yī)療廢物管理制度
- 新解讀《GB 51205-2016精對苯二甲酸工廠設計規(guī)范 》
- 兩宋的政治和軍事 課件 2024-2025學年高一歷史統(tǒng)編版(2019)必修中外歷史綱要上
- 4.7 相似三角形的性質 北師大版數(shù)學九年級上冊堂堂練(含答案)
- 人教版八年級上冊英語Unit 3 語法 比較級專項訓練
- 電商行業(yè)的消費者購買決策影響因素經(jīng)驗分享
- 江蘇省蘇北地區(qū)2023-2024學年中考數(shù)學全真模擬試卷含解析
- 江蘇省連云港市海州區(qū)新海實驗中學2024屆中考二模數(shù)學試題含解析
- 入團積極分子培養(yǎng)考察表
- 智聯(lián)國企行測筆試真題
- 2024-2030年中國家用(別墅)電梯行業(yè)發(fā)展趨勢及市場前景預測分析研究報告
- 為商品和服務的買賣雙方提供在線市場行業(yè)發(fā)展全景調研與投資趨勢預測研究報告
- 辦公樓室內裝飾工程施工設計方案技術標范本
- 2024年美國遮光窗簾市場現(xiàn)狀及上下游分析報告
- 2023年香港華夏杯六年級競賽初賽數(shù)學試卷
- 2024年無人機巡檢工安全操作技能及理論知識考試題庫(附含答案)
- 2024-2034年中國程序化購買廣告行業(yè)未來趨勢預測分析及投資規(guī)劃研究建議報告
- 外出參加學術會議審批表
- 2022年版義務教育歷史新課程標準試題與答案
評論
0/150
提交評論