最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android使用網(wǎng)絡(luò)獲取定位的方法

 更新時(shí)間:2019年05月24日 09:41:16   作者:Vivinia_Vivinia  
這篇文章主要為大家詳細(xì)介紹了Android使用網(wǎng)絡(luò)獲取定位的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android使用網(wǎng)絡(luò)獲取定位的具體代碼,供大家參考,具體內(nèi)容如下

目標(biāo)效果:

 

程序運(yùn)行彈出權(quán)限選擇,選擇運(yùn)行網(wǎng)絡(luò)定位后會(huì)查詢(xún)位置,然后在TextView上顯示當(dāng)前國(guó)家和城市。

1.activity_main.xml頁(yè)面定義TextView顯示城市名。

activity_main.xml頁(yè)面:

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

Common.java頁(yè)面:

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頁(yè)面作為服務(wù)進(jìn)行定位。

LocationSvc.java頁(yè)面:

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, "無(wú)法定位", 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)聽(tīng),停掉服務(wù)。如果要進(jìn)行實(shí)時(shí)定位,可以在退出應(yīng)用或者其他時(shí)刻停掉定位服務(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頁(yè)面獲取經(jīng)緯度,然后根據(jù)經(jīng)緯度獲取城市名。

MainActivity.java頁(yè)面:

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);
 
 // 注冊(cè)廣播
 IntentFilter filter = new IntentFilter();
 filter.addAction(Common.LOCATION_ACTION);
 this.registerReceiver(new LocationBroadcastReceiver(), filter);
 
 // 啟動(dòng)服務(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);// 不需要時(shí)注銷(xiāo)
 }
 
 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頁(yè)面添加權(quán)限并聲明服務(wù)。

AndroidManifest.xml頁(yè)面:

<?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>

6.運(yùn)行就能顯示目標(biāo)效果了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android嵌套滾動(dòng)NestedScroll的實(shí)現(xiàn)了解一下

    Android嵌套滾動(dòng)NestedScroll的實(shí)現(xiàn)了解一下

    嵌套滾動(dòng)已經(jīng)算一個(gè)比較常見(jiàn)的特效了,這篇文章主要介紹了Android嵌套滾動(dòng)NestedScroll的實(shí)現(xiàn)了解一下,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • Android實(shí)現(xiàn)socket通信統(tǒng)一接口的方法

    Android實(shí)現(xiàn)socket通信統(tǒng)一接口的方法

    這篇文章主要介紹了Android實(shí)現(xiàn)socket通信統(tǒng)一接口?,實(shí)現(xiàn)了統(tǒng)一接口之后確實(shí)可以使后續(xù)修改實(shí)現(xiàn)更加方便,程序結(jié)構(gòu)也更加工程化,需要的朋友可以參考下
    2021-12-12
  • 在android中使用緩存和脫機(jī)存儲(chǔ)

    在android中使用緩存和脫機(jī)存儲(chǔ)

    這篇文章主要介紹了在android中使用緩存和脫機(jī)存儲(chǔ),緩存可以加速你的應(yīng)用程序,即使在網(wǎng)絡(luò)不可用時(shí),用戶(hù)能夠更加流暢地使用你的應(yīng)用程序使用緩存是相當(dāng)簡(jiǎn)單的,需要一個(gè)單一的代碼行,下面來(lái)看看文章的詳細(xì)內(nèi)容
    2021-11-11
  • Android 設(shè)置應(yīng)用全屏的兩種解決方法

    Android 設(shè)置應(yīng)用全屏的兩種解決方法

    本篇文章小編為大家介紹,Android 設(shè)置應(yīng)用全屏的兩種解決方法。需要的朋友參考下
    2013-04-04
  • Android應(yīng)用程序窗口(Activity)窗口對(duì)象(Window)創(chuàng)建指南

    Android應(yīng)用程序窗口(Activity)窗口對(duì)象(Window)創(chuàng)建指南

    本文將詳細(xì)介紹Android應(yīng)用程序窗口(Activity)的窗口對(duì)象(Window)的創(chuàng)建過(guò)程,需要了解的朋友可以參考下
    2012-12-12
  • ListView通用泛型適配器

    ListView通用泛型適配器

    今天小編就為大家分享一篇關(guān)于ListView通用泛型適配器,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • Android再探全面屏適配示例詳解

    Android再探全面屏適配示例詳解

    這篇文章主要為大家介紹了Android再探全面屏適配示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Android AIDL實(shí)現(xiàn)進(jìn)程間通信探索

    Android AIDL實(shí)現(xiàn)進(jìn)程間通信探索

    這篇文章主要為大家詳細(xì)介紹了Android AIDL實(shí)現(xiàn)進(jìn)程間通信的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android接收和發(fā)送短信的實(shí)現(xiàn)代碼

    Android接收和發(fā)送短信的實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了Android接收和發(fā)送短信的實(shí)現(xiàn)代碼,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android 中 Activity顯示隱式跳轉(zhuǎn)

    Android 中 Activity顯示隱式跳轉(zhuǎn)

    這篇文章主要介紹了Android 中 Activity顯示隱式跳轉(zhuǎn)的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-02-02

最新評(píng)論

德惠市| 邢台市| 昆山市| 澄城县| 织金县| 伊春市| 侯马市| 克山县| 峨眉山市| 中卫市| 巫溪县| 连州市| 曲松县| 灵石县| 宣化县| 开江县| 布尔津县| 祁阳县| 玉龙| 石家庄市| 民和| 灌南县| 体育| 嵊州市| 呼图壁县| 关岭| 绍兴市| 杭州市| 麻栗坡县| 游戏| 中山市| 朔州市| 磐安县| 乳源| 奎屯市| 中西区| 辽宁省| 即墨市| 阜宁县| 岳普湖县| 五常市|