android 获取GPS定位
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yanlei.yl5" >
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_MMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.NFC"/>
<uses-permission android:name="android.permission.TRANSMIT_IR"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<!-- For android.media.audiofx.Visualizer -->
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<!-- We will request access to the camera, saying we require a camera of some sort but not one with autofocus capability. -->
<uses-permission android:name="android.permission.CAMERA"/> <!-- 连接<a href="http://www.it165.net/news/nhlw/" target="_blank" class="keylink">互联网</a>Internet权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GPS定位权限 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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> </application> </manifest>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" > <TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="经纬度信息:"
android:textColor="#660000"
android:textSize="20sp" /> </LinearLayout>
MainActivity.java
package com.example.yanlei.yl5; import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView; //import java.util.logging.Handler; public class MainActivity extends AppCompatActivity { private static final String TAG = MainActivity.class.getSimpleName();
private double latitude = 0.0;
private double longitude = 0.0;
private TextView info;
private LocationManager locationManager; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
info = (TextView) findViewById(R.id.tv);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
getLocation();
//gps已打开
} else {
toggleGPS();
new Handler() {}.postDelayed(new Runnable() {
@Override
public void run() {
getLocation();
}
}, 2000); }
} private void toggleGPS() {
Intent gpsIntent = new Intent();
gpsIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
gpsIntent.setData(Uri.parse("custom:3"));
try {
PendingIntent.getBroadcast(this, 0, gpsIntent, 0).send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
Location location1 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location1 != null) {
latitude = location1.getLatitude(); // 经度
longitude = location1.getLongitude(); // 纬度
}
}
} private void getLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
} else { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
}
info.setText("纬度:" + latitude + "\n" + "经度:" + longitude);
} LocationListener locationListener = new LocationListener() {
// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
} // Provider被enable时触发此函数,比如GPS被打开
@Override
public void onProviderEnabled(String provider) {
Log.e(TAG, provider);
} // Provider被disable时触发此函数,比如GPS被关闭
@Override
public void onProviderDisabled(String provider) {
Log.e(TAG, provider);
} // 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
@Override
public void onLocationChanged(Location location) {
if (location != null) {
Log.e("Map", "Location changed : Lat: " + location.getLatitude() + " Lng: " + location.getLongitude());
latitude = location.getLatitude(); // 经度
longitude = location.getLongitude(); // 纬度
}
}
}; // 打开和关闭gps第二种方法
private void openGPSSettings() {
//获取GPS现在的状态(打开或是关闭状态)
boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER);
if (gpsEnabled) {
//关闭GPS
Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, false);
} else {
//打开GPS
Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, true);
}
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId(); //noinspection SimplifiableIfStatement
if (id == R.id.action_settings) { return true;
} return super.onOptionsItemSelected(item);
}
}
android 获取GPS定位的更多相关文章
- [置顶]
xamarin android使用gps定位获取经纬度
看了文章你会得出以下几个结论 1.android定位主要有四种方式GPS,Network(wifi定位.基站定位),AGPS定位 2.绝大部分android国产手机使用network进行定位是没有作用 ...
- GPS(2)关于位置的3个示例,实时获取GPS定位数据,求两个经纬点距离,邻近某个区域圆时警告
实时获取GPS定位数据 import android.app.Activity; import android.content.Context; import android.location.Loc ...
- Android中GPS定位的简单应用
在Android中通过GPS获得当前位置,首先要获得一个LocationManager实例,通过该实例的getLastKnownLocation()方法获得第一个的位置,该方法的说明如下: void ...
- Android开发——GPS定位
1.LocationManager LocationManager系统服务是位置服务的核心组件,它提供了一系列方法来处理与位置相关的问题. 与LocationManager相关的两个知识点: 1.1 ...
- 【Android】GPS定位基本原理浅析
位置服务已经成为越来越热的一门技术,也将成为以后所有移动设备(智能手机.掌上电脑等)的标配.而定位导航技术中,目前精度最高.应用最广泛的,自然非GPS莫属了.网络上介绍GPS原理的专业资料很多,而本文 ...
- Arcgis API for Android之GPS定位
欢迎大家增加Arcgis API for Android的QQ交流群:337469080 先说说写这篇文章的原因吧,在群内讨论的过程中,有人提到了定位的问题,刚好,自己曾经在做相关工作的时候做过相关的 ...
- Arcgis For Android之GPS定位实现
翻开曾经做的东西,看了看,非常多从逻辑上比較乱,对之做了改动,完毕后实现的效果为: MapActivity源码例如以下: package com.lzugis.map; import java.io. ...
- Android之GPS定位详解
一.LocationManager LocationMangager,位置管理器.要想操作定位相关设备,必须先定义个LocationManager.我们可以通过如下代码创建LocationManger ...
- Android中GPS类及方法简介
GPS是Global Positioning System(全球定位系统)的简称,它的作用就是为全球的物体提供定位功能.GPS定位是一门高新技术,但对于Android程序员来说,开发GPS功能的应用程 ...
随机推荐
- RSS列表
博客园 http://feed.cnblogs.com/blog/sitehome/rss
- 15年多校第一场七题hdu5294
要做这题,先要明白图的割,说白了就是 为了让原点无法到汇点要删几条边(之所以叫割,就是在图面上切一刀,减掉最小的边是原点和汇点成为两个集合),想到了割先放着一会用. 题中说只有沿最短路走才有可能追上, ...
- HDU 4866 Shooting 扫描线 + 主席树
题意: 在二维平面的第一象限有\(n(1 \leq n \leq 10^5)\)条平行于\(x\)轴的线段,接下来有\(m\)次射击\(x \, a \, b \, c\). 每次射击会获得一定的分数 ...
- launchMode
launchMode在多个Activity跳转的过程中扮演着重要的角色,它可以决定是否生成新的Activity实例,是否重用已存在的Activity实例,是否和其他Activity实例公用一个task ...
- luogu2763 试题库问题
倘若某个试题已经被选到某个类型里了,那么它就不可再被选进别的类型了. 所以,对于每个类型,我们将其与汇连边,权值是它的要求的题目数量. 对于每个题目,我们将源与其连边,权值是1,代表只能用一次.然后再 ...
- IE6 下绝对定位position:absolute 与浮动不显示 (IE6 下拉菜单显示)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> <HEAD& ...
- Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 2)
A. Splits time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...
- WebApplicationContextUtils源码
package org.springframework.web.context.support; import javax.servlet.ServletContext; import javax.s ...
- C# 条件与&&与条件或||的使用总结
CSDN说明: 条件“或”运算符 (||) 执行 bool 操作数的逻辑“或”运算,但仅在必要时才计算第二个操作数. 件“与”运算符 (&&) 执行其 bool 操作数的逻辑“与”运算 ...
- 设计模式(二 & 三)工厂模式:2-工厂方法模式
模拟场景: 沿用 设计模式(二)工厂模式:1-简单工厂模式 中关于运算器 Operation 的例子. 思想: 针对在 Easy Factory 中提出的,破坏“开-闭原则”的问题,Factory M ...