欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

如何在Android中通過網(wǎng)絡(luò)獲取定位

如何在Android中通過網(wǎng)絡(luò)獲取定位?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、虞城網(wǎng)絡(luò)推廣、成都小程序開發(fā)、虞城網(wǎng)絡(luò)營銷、虞城企業(yè)策劃、虞城品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供虞城建站搭建服務(wù),24小時服務(wù)熱線:18980820575,官方網(wǎng)址:chinadenli.net

1.activity_main.xml頁面定義TextView顯示城市名。

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: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頁面,設(shè)置公共常量。

Common.java頁面:

package com.sc.demo.common;
/**
 * 公共常量
 * @author wxy
 *
 */
public class Common {
 
 public static final String LOCATION = "location";
 public static final String LOCATION_ACTION = "locationAction";
}

3.新建LocationSvc.java頁面作為服務(wù)進行定位。

LocationSvc.java頁面:

package com.sc.demo.locate;
 
import com.sc.demo.common.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;
 
/**
 * 定位服務(wù)
 * @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)聽,停掉服務(wù)。如果要進行實時定位,可以在退出應(yīng)用或者其他時刻停掉定位服務(wù)。
 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 com.sc.demo.common.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);
 
 // 啟動服務(wù)
 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)緯度轉(zhuǎn)換為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頁面添加權(quán)限并聲明服務(wù)。

AndroidManifest.xml頁面:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/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_COARSE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 
  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
      android:name="com.sc.demo.MainActivity"
      android:label="@string/app_name" >
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
 
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
 
    <service android:name=".locate.LocationSvc" />
  </application>
 
</manifest>

看完上述內(nèi)容,你們掌握如何在Android中通過網(wǎng)絡(luò)獲取定位的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)頁題目:如何在Android中通過網(wǎng)絡(luò)獲取定位
當(dāng)前路徑:http://chinadenli.net/article16/gispdg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站網(wǎng)站制作App開發(fā)全網(wǎng)營銷推廣ChatGPT移動網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)